File tree Expand file tree Collapse file tree 3 files changed +11
-18
lines changed Expand file tree Collapse file tree 3 files changed +11
-18
lines changed Original file line number Diff line number Diff line change @@ -129,24 +129,16 @@ extension ExecutionContextType {
129129 func syncThroughAsync< ReturnType> ( task: ( ) throws -> ReturnType ) throws -> ReturnType {
130130 var result : Result < ReturnType , AnyError > ?
131131
132- let cond = NSCondition ( )
133- var done = false
132+ let sema = Semaphore ( )
134133
135134 async {
136135 defer {
137- cond. lock ( )
138- done = true
139- cond. signal ( )
140- cond. unlock ( )
136+ sema. signal ( )
141137 }
142138 result = materialize ( task)
143139 }
144140
145- cond. lock ( )
146- while !done {
147- cond. wait ( )
148- }
149- cond. unlock ( )
141+ sema. wait ( )
150142
151143 return try result!. dematerializeAnyError ( )
152144 }
Original file line number Diff line number Diff line change 7373
7474 override init ( ) {
7575 var runLoop : RunLoop ?
76- let cond = NSCondition ( )
77- cond . lock ( )
76+ let sema = Semaphore ( )
77+
7878 PThread ( task: {
7979 runLoop = RunLoop . currentRunLoop ( true )
80- cond . signal ( )
80+ sema . signal ( )
8181 RunLoop . run ( )
8282 } ) . start ( )
83- cond. wait ( )
84- cond. unlock ( )
83+
84+ sema. wait ( )
85+
8586 self . rl = runLoop!
8687 }
8788
Original file line number Diff line number Diff line change @@ -40,10 +40,10 @@ public class Semaphore {
4040 self . value = value
4141 }
4242
43- /// Creates a new semaphore with initial value 1
43+ /// Creates a new semaphore with initial value 0
4444 /// This kind of semaphores is useful to protect a critical section
4545 public convenience init ( ) {
46- self . init ( value: 1 )
46+ self . init ( value: 0 )
4747 }
4848
4949 /// returns true on success (false if timeout expired)
You can’t perform that action at this time.
0 commit comments