Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit160389b

Browse files
author
Alex Belozierov
committed
- add PDispatchGroup to PDispatchQueue async API
1 parent4059eea commit160389b

File tree

5 files changed

+23
-22
lines changed

5 files changed

+23
-22
lines changed

‎PosixDispatch/DispatchQueue/PDispatchConcurrentQueue.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ class PDispatchConcurrentQueue: PDispatchQueueBackend {
110110
lock.unlock()
111111
}
112112

113-
funcasync(flags:DispatchItemFlags, execute work:@escapingBlock){
113+
funcasync(group:PDispatchGroup?=nil,flags:DispatchItemFlags=[], execute work:@escapingBlock){
114114
lock.lock()
115-
blockQueue.push(work)
115+
blockQueue.push(group.block(with:work))
116116
flags.contains(.barrier)
117117
?addBarrierAsyncCount()
118118
:addNotBarrierAsyncCount()

‎PosixDispatch/DispatchQueue/PDispatchQueue.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class PDispatchQueue: PDispatchQueueBackend {
4545
backend.async(execute: work)
4646
}
4747

48-
@inlinablefuncasync(flags:DispatchItemFlags, execute work:@escapingBlock){
49-
backend.async(flags: flags, execute: work)
48+
@inlinablefuncasync(group:PDispatchGroup?=nil,flags:DispatchItemFlags=[], execute work:@escapingBlock){
49+
backend.async(group: group,flags: flags, execute: work)
5050
}
5151

5252
// MARK: - Concurrent Perform

‎PosixDispatch/DispatchQueue/PDispatchQueueBackend.swift‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protocol PDispatchQueueBackend: class {
1515
@discardableResultfunc sync<T>(execute work:()throws->T)rethrows->T
1616
@discardableResultfunc sync<T>(flags:DispatchItemFlags, execute work:()throws->T)rethrows->T
1717

18-
funcasync(flags:DispatchItemFlags, execute work:@escapingBlock)
18+
funcasync(group:PDispatchGroup?,flags:DispatchItemFlags, execute work:@escapingBlock)
1919
funcasync(execute work:@escapingBlock)
2020

2121
}
@@ -35,9 +35,9 @@ extension PDispatchQueueBackend {
3535
}
3636

3737
@discardableResult
38-
funcasync<T>(flags:DispatchItemFlags=[], execute work:@escaping()throws->T)->PDispatchWorkItem<T>{
38+
funcasync<T>(group:PDispatchGroup?=nil,flags:DispatchItemFlags=[], execute work:@escaping()throws->T)->PDispatchWorkItem<T>{
3939
letitem=PDispatchWorkItem(flags: flags, block: work)
40-
async(flags: flags, execute: item.perform)
40+
async(group: group,flags: flags, execute: item.perform)
4141
return item
4242
}
4343

‎PosixDispatch/DispatchQueue/PDispatchSerialQueue.swift‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ class PDispatchSerialQueue: PDispatchQueueBackend {
6464
}
6565
}
6666

67-
funcasync(flags:DispatchItemFlags, execute work:@escapingBlock){
68-
guard flags.contains(.enforceQoS)else{returnasync(execute: work)}
67+
funcasync(group:PDispatchGroup?=nil, flags:DispatchItemFlags=[], execute work:@escapingBlock){
68+
letblock= group.block(with: work)
69+
guard flags.contains(.enforceQoS)else{returnasync(execute: block)}
6970
lock.lock()
70-
queue.insertInStart(.async(.init(work)))
71+
queue.insertInStart(.async(.init(block)))
7172
startAsyncPerforming()
7273
lock.unlock()
7374
}

‎PosixDispatch/PDispatchGroup.swift‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,8 @@ class PDispatchGroup {
1515
self.count= count
1616
}
1717

18-
@inlinablefunc setCount(_ count:Int){
19-
condition.lock()
20-
self.count= count
21-
condition.unlock()
22-
}
23-
2418
@inlinablefunc enter(){
25-
condition.lock()
26-
count+=1
27-
condition.unlock()
19+
condition.lockedPerform{ count+=1}
2820
}
2921

3022
@inlinablefunc leave(){
@@ -35,9 +27,17 @@ class PDispatchGroup {
3527
}
3628

3729
@inlinablefunc wait(){
38-
condition.lock()
39-
condition.wait()
40-
condition.unlock()
30+
condition.lockedPerform{ condition.wait()}
31+
}
32+
33+
}
34+
35+
extensionOptionalwhere Wrapped:PDispatchGroup{
36+
37+
func block(with work:@escapingPThreadPool.Block)->PThreadPool.Block{
38+
guardlet group=selfelse{return work}
39+
group.enter()
40+
return{work(); group.leave()}
4141
}
4242

4343
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp