SoFunction
Updated on 2025-04-06

Detailed explanation of the structure level of kotlin source code

Coroutine source code structure

Before studying Kotlin source code, you must first understand the structure distribution of Kotlin source code. Otherwise, you won't find the code where to look. Of course, you must have a goal before looking at the source code. It is best to look at the source code with this goal to be more targeted and grasp the main process, otherwise you may fall into the vast source code details.

Coroutine source code can be divided into 2 warehouses, one is the Kotlin warehouse and the other is the Kotlin coroutine warehouse.

  • Kotlin Warehouse/JetBrains/kotlin
  • Coroutine Warehouse/Kotlin/

The basic elements of coroutines are defined in the Kotlin repository. These basic elements are very critical and are the basis for building coroutines. Coroutine repository is equivalent to using the basic elements defined in the Kotlin repository to implement the logic of coroutines.

Kotlin coroutine source code is divided into 3 layers:

  • Basic layer: Coroutine basic elements defined in the Kotlin library. For example, CancellationException, CombinedContext, Continuation, ContinuationInterceptor, CoroutineContext, SafeContinuation, etc.
  • Intermediate layer: The common logic of coroutine framework in coroutine repository - common. For example, Job, Deferred, Select, Channel, Flow.
  • Platform layer: In the coroutine warehouse, the implementation of coroutines on a specific platform. Such as JVM, JS, Native.

Next, let’s learn more about how these layers are distributed.

Basic layer

In the Kotlin library, the specific location of the basic layer of the coroutine is/kotlin/libraries/stdlib/src/kotlin/coroutines/in. The official puts these basic elements in the standard library for decoupling.

  • Through these basic elements, it can be combined into various coroutine frameworks, although it is currently the best combination of official coroutine frameworks.
  • After the API is defined at the basic layer, the coroutine library can be followed by rules in their respective platform layers, and the platform-related code is developed under these APIs.

Intermediate layer

In the library//kotlinx-coroutines-core/common/Under submodules, there are some common logic. For example, launch, async, withContext, Deferred, Job, NonCancellable, Channel, Flow, AbstractCoroutine, these things are encapsulation of elements in the base layer, making it easier to get started with coroutines.

In the source code of these public logic, platform-related logic is not involved.

Platform layer

Because Kotlin is cross-platform, its platform-related logic is divided into js, ​​jvm, and native. Their directories are on par with common, respectively:

js:kotlinx-coroutines-core/js/

jvm:kotlinx-coroutines-core/jvm/

native:kotlinx-coroutines-core/native/

Let's focus on jvm. The following is the code in the jvm directory. Other platforms also need to implement their own thread pools, event loops, asynchronous tasks, etc.

.
├── 
├── 
├── 
├── 
├── 
├── 
├── 
├── 
├──     //Event loop├── 
├── 
├──      
├── 
├──     //
├── 
├──      //Thread pool├── 
├── channels
│   ├── 
│   └── 
├── debug
│   ├── 
│   └── internal
│       ├── 
│       ├── 
│       ├── 
│       ├── 
│       ├── 
│       ├── 
│       ├── 
│       └── 
├── flow
│   └── internal
│       ├── 
│       └── 
├── internal
│   ├── 
│   ├── 
│   ├── 
│   ├── 
│   ├── 
│   ├── 
│   ├── 
│   ├── 
│   ├── 
│   ├── 
│   ├── 
│   ├── 
│   └──    //
└── scheduling
    ├── 
    ├── 
    ├── 
    ├── 
    └── 

The above is the detailed explanation of the kotlin source code structure level. For more information about the kotlin source code structure, please pay attention to my other related articles!