7
7
//
8
8
9
9
10
- import Foundation
11
10
import Dispatch
12
11
13
12
let numberOfPhilosophers = 4
14
13
15
14
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
+
18
17
let leftFork : DispatchSemaphore
19
18
let rightFork : DispatchSemaphore
20
-
19
+
21
20
init ( leftIndex: Int , rightIndex: Int ) {
22
21
//Order forks by index to prevent deadlock
23
22
if leftIndex > rightIndex {
@@ -35,7 +34,7 @@ struct ForkPair {
35
34
leftFork. wait ( )
36
35
rightFork. wait ( )
37
36
}
38
-
37
+
39
38
func putDown( ) {
40
39
//The order does not matter here
41
40
leftFork. signal ( )
@@ -47,61 +46,60 @@ struct ForkPair {
47
46
struct Philosophers {
48
47
let forkPair : ForkPair
49
48
let philosopherIndex : Int
50
-
49
+
51
50
var leftIndex = - 1
52
51
var rightIndex = - 1
53
-
52
+
54
53
init ( philosopherIndex: Int ) {
55
54
leftIndex = philosopherIndex
56
55
rightIndex = philosopherIndex - 1
57
-
56
+
58
57
if rightIndex < 0 {
59
58
rightIndex += numberOfPhilosophers
60
59
}
61
60
62
61
self . forkPair = ForkPair ( leftIndex: leftIndex, rightIndex: rightIndex)
63
62
self . philosopherIndex = philosopherIndex
64
-
63
+
65
64
print ( " Philosopher: \( philosopherIndex) left: \( leftIndex) right: \( rightIndex) " )
66
65
}
67
-
66
+
68
67
func run( ) {
69
68
while true {
70
- print ( " Acquiring lock for Philosofer : \( philosopherIndex) Left: \( leftIndex) Right: \( rightIndex) " )
69
+ print ( " Acquiring lock for Philosopher : \( philosopherIndex) Left: \( leftIndex) Right: \( rightIndex) " )
71
70
forkPair. pickUp ( )
72
71
print ( " Start Eating Philosopher: \( philosopherIndex) " )
73
72
//sleep(1000)
74
- print ( " Releasing lock for Philosofer : \( philosopherIndex) Left: \( leftIndex) Right: \( rightIndex) " )
73
+ print ( " Releasing lock for Philosopher : \( philosopherIndex) Left: \( leftIndex) Right: \( rightIndex) " )
75
74
forkPair. putDown ( )
76
75
}
77
76
}
78
77
}
79
78
80
79
81
- // Layout of the table (P = philosopher, f = fork) for 4 Philosopher
80
+ // Layout of the table (P = philosopher, f = fork) for 4 Philosophers
82
81
// P0
83
82
// f3 f0
84
83
// P3 P1
85
84
// f2 f1
86
85
// P2
87
-
88
86
let globalSem = DispatchSemaphore ( value: 0 )
89
87
90
88
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
+ }
95
95
}
96
96
}
97
97
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 ( )
101
101
}
102
102
103
103
104
- //wait forever
104
+ //Wait forever
105
105
globalSem. wait ( )
106
-
107
-
0 commit comments