Skip to content

Commit 0a684cd

Browse files
committed
Merge pull request #11 from ypopovych/master
Added locks on run loop creation. Removed logs
2 parents 8eb2723 + bf8d7f9 commit 0a684cd

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

ExecutionContext/ExecutionContext.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,14 @@ extension ExecutionContextType {
130130
var result:Result<ReturnType, AnyError>?
131131

132132
let sema = LoopSemaphore()
133-
let id = NSUUID().UUIDString
134133

135-
print("before async: ", id)
136134
async {
137-
print("started async: ", id)
138135
result = materialize(task)
139-
print("materialized: ", id)
140136
sema.signal()
141-
print("signaled: ", id)
142137
}
143138

144-
print("will wait: ", id)
145-
146139
sema.wait()
147140

148-
print("did wait: ", id)
149-
150141
return try result!.dematerializeAnyError()
151142
}
152143
}

ExecutionContext/PThreadExecutionContext.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
pthread_setspecific(key.key, nil)
7878
} else {
7979
if retain {
80-
print("Pass retained \(obj)")
8180
pthread_setspecific(key.key, UnsafePointer<Void>(Unmanaged.passRetained(obj!).toOpaque()))
8281
} else {
8382
pthread_setspecific(key.key, UnsafePointer<Void>(Unmanaged.passUnretained(obj!).toOpaque()))

ExecutionContext/RunLoop.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ import CoreFoundation
230230
Unmanaged<RunLoop>.fromOpaque(COpaquePointer(loop)).release()
231231
})
232232

233+
private static let threadLocalLock = NSLock()
233234
private static let MainRunLoop = RunLoop.createMainRunLoop()
234235

235236
init(_ cfRunLoop: CFRunLoop) {
@@ -241,11 +242,16 @@ import CoreFoundation
241242
}
242243

243244
private static func createMainRunLoop() -> RunLoop {
245+
defer {
246+
RunLoop.threadLocalLock.unlock()
247+
}
248+
249+
RunLoop.threadLocalLock.lock()
244250
let runLoop = RunLoop(CFRunLoopGetMain())
245251
if runLoop.isCurrent() {
246252
PThread.setSpecific(runLoop, key: RunLoop.threadKey, retain: true)
247253
} else {
248-
let sema = LoopSemaphore()
254+
let sema = Semaphore()
249255
runLoop.addTask({
250256
PThread.setSpecific(runLoop, key: RunLoop.threadKey, retain: true)
251257
sema.signal()
@@ -256,6 +262,10 @@ import CoreFoundation
256262
}
257263

258264
static func currentRunLoop() -> RunLoop {
265+
defer {
266+
RunLoop.threadLocalLock.unlock()
267+
}
268+
RunLoop.threadLocalLock.lock()
259269
guard let loop = PThread.getSpecific(RunLoop.threadKey) else {
260270
let loop = RunLoop(CFRunLoopGetCurrent())
261271
PThread.setSpecific(loop, key: RunLoop.threadKey, retain: true)
@@ -269,7 +279,6 @@ import CoreFoundation
269279
}
270280

271281
func startTaskQueue(priority: Int = 1) {
272-
print("Start queue called")
273282
defer {
274283
taskQueueLock.unlock()
275284
}

0 commit comments

Comments
 (0)