Skip to content

Commit c0dbd74

Browse files
authored
Merge pull request kodecocodes#402 from TheIronBorn/patch-7
availability check, native array loop for signal
2 parents c4d39ec + 40be0d2 commit c0dbd74

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

DiningPhilosophers/Sources/main.swift

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77
//
88

99

10-
import Foundation
1110
import Dispatch
1211

1312
let numberOfPhilosophers = 4
1413

1514
struct ForkPair {
16-
static let forksSemaphore:[DispatchSemaphore] = Array(repeating: DispatchSemaphore(value: 1), count: numberOfPhilosophers)
17-
15+
static let forksSemaphore: [DispatchSemaphore] = Array(repeating: DispatchSemaphore(value: 1), count: numberOfPhilosophers)
16+
1817
let leftFork: DispatchSemaphore
1918
let rightFork: DispatchSemaphore
20-
19+
2120
init(leftIndex: Int, rightIndex: Int) {
2221
//Order forks by index to prevent deadlock
2322
if leftIndex > rightIndex {
@@ -35,7 +34,7 @@ struct ForkPair {
3534
leftFork.wait()
3635
rightFork.wait()
3736
}
38-
37+
3938
func putDown() {
4039
//The order does not matter here
4140
leftFork.signal()
@@ -47,61 +46,60 @@ struct ForkPair {
4746
struct Philosophers {
4847
let forkPair: ForkPair
4948
let philosopherIndex: Int
50-
49+
5150
var leftIndex = -1
5251
var rightIndex = -1
53-
52+
5453
init(philosopherIndex: Int) {
5554
leftIndex = philosopherIndex
5655
rightIndex = philosopherIndex - 1
57-
56+
5857
if rightIndex < 0 {
5958
rightIndex += numberOfPhilosophers
6059
}
6160

6261
self.forkPair = ForkPair(leftIndex: leftIndex, rightIndex: rightIndex)
6362
self.philosopherIndex = philosopherIndex
64-
63+
6564
print("Philosopher: \(philosopherIndex) left: \(leftIndex) right: \(rightIndex)")
6665
}
67-
66+
6867
func run() {
6968
while true {
70-
print("Acquiring lock for Philosofer: \(philosopherIndex) Left:\(leftIndex) Right:\(rightIndex)")
69+
print("Acquiring lock for Philosopher: \(philosopherIndex) Left:\(leftIndex) Right:\(rightIndex)")
7170
forkPair.pickUp()
7271
print("Start Eating Philosopher: \(philosopherIndex)")
7372
//sleep(1000)
74-
print("Releasing lock for Philosofer: \(philosopherIndex) Left:\(leftIndex) Right:\(rightIndex)")
73+
print("Releasing lock for Philosopher: \(philosopherIndex) Left:\(leftIndex) Right:\(rightIndex)")
7574
forkPair.putDown()
7675
}
7776
}
7877
}
7978

8079

81-
// Layout of the table (P = philosopher, f = fork) for 4 Philosopher
80+
// Layout of the table (P = philosopher, f = fork) for 4 Philosophers
8281
// P0
8382
// f3 f0
8483
// P3 P1
8584
// f2 f1
8685
// P2
87-
8886
let globalSem = DispatchSemaphore(value: 0)
8987

9088
for i in 0..<numberOfPhilosophers {
91-
DispatchQueue.global(qos: .background).async {
92-
let p = Philosophers(philosopherIndex: i)
93-
94-
p.run()
89+
if #available(macOS 10.10, *) {
90+
DispatchQueue.global(qos: .background).async {
91+
let p = Philosophers(philosopherIndex: i)
92+
93+
p.run()
94+
}
9595
}
9696
}
9797

98-
//start the thread signaling the semaphore
99-
for i in 0..<numberOfPhilosophers {
100-
ForkPair.forksSemaphore[i].signal()
98+
//Start the thread signaling the semaphore
99+
for semaphore in ForkPair.forksSemaphore {
100+
semaphore.signal()
101101
}
102102

103103

104-
//wait forever
104+
//Wait forever
105105
globalSem.wait()
106-
107-

0 commit comments

Comments
 (0)