Skip to content

Commit 0903691

Browse files
committed
add:docs
1 parent a23bf88 commit 0903691

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

Sources/JavaScriptEventLoop/JobQueue.swift

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
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.
23

34
import _CJavaScriptEventLoop
5+
import Foundation
46

7+
/// Represents the state of the job queue.
58
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
69
struct QueueState: Sendable {
10+
/// The head of the job queue.
711
fileprivate var headJob: UnownedJob? = nil
12+
/// Indicates if the queue is actively processing jobs.
813
fileprivate var isSpinning: Bool = false
914
}
1015

1116
@available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
1217
extension JavaScriptEventLoop {
18+
/// A lock to synchronize queue access.
1319
private var queueLock: NSLock {
1420
NSLock()
1521
}
1622

23+
/// Inserts a job into the queue and ensures jobs are processed.
24+
/// - Parameter job: The job to add to the queue.
1725
func insertJobQueue(job newJob: UnownedJob) {
1826
queueLock.lock()
1927
defer { queueLock.unlock() }
@@ -28,6 +36,8 @@ extension JavaScriptEventLoop {
2836
}
2937
}
3038

39+
/// Inserts a job into the queue at the correct priority position.
40+
/// - Parameter job: The job to insert into the queue.
3141
private func insertJob(_ newJob: UnownedJob) {
3242
var current = queueState.headJob
3343
var previous: UnownedJob? = nil
@@ -45,8 +55,9 @@ extension JavaScriptEventLoop {
4555
}
4656
}
4757

58+
/// Processes all jobs in the queue until it is empty.
4859
func runAllJobs() {
49-
assert(queueState.isSpinning)
60+
assert(queueState.isSpinning, "runAllJobs called while queueState.isSpinning is false.")
5061

5162
while let job = claimNextFromQueue() {
5263
executeJob(job)
@@ -55,6 +66,8 @@ extension JavaScriptEventLoop {
5566
queueState.isSpinning = false
5667
}
5768

69+
/// Executes a specific job.
70+
/// - Parameter job: The job to execute.
5871
private func executeJob(_ job: UnownedJob) {
5972
#if compiler(>=5.9)
6073
job.runSynchronously(on: self.asUnownedSerialExecutor())
@@ -63,6 +76,8 @@ extension JavaScriptEventLoop {
6376
#endif
6477
}
6578

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.
6681
func claimNextFromQueue() -> UnownedJob? {
6782
queueLock.lock()
6883
defer { queueLock.unlock() }
@@ -75,28 +90,38 @@ extension JavaScriptEventLoop {
7590

7691
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
7792
fileprivate extension UnownedJob {
93+
/// Converts the job to its internal implementation.
94+
/// - Returns: A raw pointer to the job's internal structure.
7895
private func asImpl() -> UnsafeMutablePointer<_CJavaScriptEventLoop.Job> {
7996
unsafeBitCast(self, to: UnsafeMutablePointer<_CJavaScriptEventLoop.Job>.self)
8097
}
8198

99+
/// The job's priority flags.
82100
var flags: JobFlags {
83101
JobFlags(bits: asImpl().pointee.Flags)
84102
}
85103

86-
var rawPriority: UInt32 { flags.priority }
104+
/// The raw priority value of the job.
105+
var rawPriority: UInt32 {
106+
flags.priority
107+
}
87108

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.
88111
func nextInQueue() -> UnsafeMutablePointer<UnownedJob?> {
89112
withUnsafeMutablePointer(to: &asImpl().pointee.SchedulerPrivate.0) { rawNextJobPtr in
90113
UnsafeMutableRawPointer(rawNextJobPtr).bindMemory(to: UnownedJob?.self, capacity: 1)
91114
}
92115
}
93116
}
94117

118+
/// Represents job flags including priority.
95119
fileprivate struct JobFlags {
96-
var bits: UInt32 = 0
120+
/// The raw bit representation of the flags.
121+
var bits: UInt32 = 0
97122

123+
/// Extracts the priority value from the flags.
98124
var priority: UInt32 {
99125
(bits & 0xFF00) >> 8
100126
}
101-
}
102-
#endif
127+
}

0 commit comments

Comments
 (0)