1
- // This file contains the job queue implementation which re-order jobs based on their priority.
1
+ // This file contains the job queue implementation for JavaScriptEventLoop.
2
+ // It manages job insertion and execution based on priority.
2
3
3
4
import _CJavaScriptEventLoop
5
+ import Foundation
4
6
7
+ /// Represents the state of the job queue.
5
8
@available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
6
9
struct QueueState : Sendable {
10
+ /// The head of the job queue.
7
11
fileprivate var headJob : UnownedJob ? = nil
12
+ /// Indicates if the queue is actively processing jobs.
8
13
fileprivate var isSpinning : Bool = false
9
14
}
10
15
11
16
@available ( macOS 14 . 0 , iOS 17 . 0 , watchOS 10 . 0 , tvOS 17 . 0 , * )
12
17
extension JavaScriptEventLoop {
18
+ /// A lock to synchronize queue access.
13
19
private var queueLock : NSLock {
14
20
NSLock ( )
15
21
}
16
22
23
+ /// Inserts a job into the queue and ensures jobs are processed.
24
+ /// - Parameter job: The job to add to the queue.
17
25
func insertJobQueue( job newJob: UnownedJob ) {
18
26
queueLock. lock ( )
19
27
defer { queueLock. unlock ( ) }
@@ -28,6 +36,8 @@ extension JavaScriptEventLoop {
28
36
}
29
37
}
30
38
39
+ /// Inserts a job into the queue at the correct priority position.
40
+ /// - Parameter job: The job to insert into the queue.
31
41
private func insertJob( _ newJob: UnownedJob ) {
32
42
var current = queueState. headJob
33
43
var previous : UnownedJob ? = nil
@@ -45,8 +55,9 @@ extension JavaScriptEventLoop {
45
55
}
46
56
}
47
57
58
+ /// Processes all jobs in the queue until it is empty.
48
59
func runAllJobs( ) {
49
- assert ( queueState. isSpinning)
60
+ assert ( queueState. isSpinning, " runAllJobs called while queueState.isSpinning is false. " )
50
61
51
62
while let job = claimNextFromQueue ( ) {
52
63
executeJob ( job)
@@ -55,6 +66,8 @@ extension JavaScriptEventLoop {
55
66
queueState. isSpinning = false
56
67
}
57
68
69
+ /// Executes a specific job.
70
+ /// - Parameter job: The job to execute.
58
71
private func executeJob( _ job: UnownedJob ) {
59
72
#if compiler(>=5.9)
60
73
job. runSynchronously ( on: self . asUnownedSerialExecutor ( ) )
@@ -63,6 +76,8 @@ extension JavaScriptEventLoop {
63
76
#endif
64
77
}
65
78
79
+ /// Removes and returns the next job from the queue.
80
+ /// - Returns: The next job in the queue, or `nil` if the queue is empty.
66
81
func claimNextFromQueue( ) -> UnownedJob ? {
67
82
queueLock. lock ( )
68
83
defer { queueLock. unlock ( ) }
@@ -75,28 +90,38 @@ extension JavaScriptEventLoop {
75
90
76
91
@available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
77
92
fileprivate extension UnownedJob {
93
+ /// Converts the job to its internal implementation.
94
+ /// - Returns: A raw pointer to the job's internal structure.
78
95
private func asImpl( ) -> UnsafeMutablePointer < _CJavaScriptEventLoop . Job > {
79
96
unsafeBitCast ( self , to: UnsafeMutablePointer< _CJavaScriptEventLoop. Job> . self )
80
97
}
81
98
99
+ /// The job's priority flags.
82
100
var flags : JobFlags {
83
101
JobFlags ( bits: asImpl ( ) . pointee. Flags)
84
102
}
85
103
86
- var rawPriority : UInt32 { flags. priority }
104
+ /// The raw priority value of the job.
105
+ var rawPriority : UInt32 {
106
+ flags. priority
107
+ }
87
108
109
+ /// Retrieves a pointer to the next job in the queue.
110
+ /// - Returns: A pointer to the next job, or `nil` if there are no further jobs.
88
111
func nextInQueue( ) -> UnsafeMutablePointer < UnownedJob ? > {
89
112
withUnsafeMutablePointer ( to: & asImpl( ) . pointee. SchedulerPrivate. 0 ) { rawNextJobPtr in
90
113
UnsafeMutableRawPointer ( rawNextJobPtr) . bindMemory ( to: UnownedJob ? . self, capacity: 1 )
91
114
}
92
115
}
93
116
}
94
117
118
+ /// Represents job flags including priority.
95
119
fileprivate struct JobFlags {
96
- var bits : UInt32 = 0
120
+ /// The raw bit representation of the flags.
121
+ var bits : UInt32 = 0
97
122
123
+ /// Extracts the priority value from the flags.
98
124
var priority : UInt32 {
99
125
( bits & 0xFF00 ) >> 8
100
126
}
101
- }
102
- #endif
127
+ }
0 commit comments