-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchameneos.dats
233 lines (208 loc) · 6.32 KB
/
chameneos.dats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
(*
Briefly say what concurrency technique is used in the program header comment:
Semaphone counters are used for entry into the meeting place, while a mutex lock
protects the shared data.
*)
staload "libc/SATS/stdio.sats"
staload "libc/sys/SATS/types.sats"
staload "libc/SATS/unistd.sats"
staload "prelude/DATS/list.dats"
staload UN = "prelude/SATS/unsafe.sats"
//
staload "chameneos.sats"
(* ****** ****** *)
implement
color_to_int (c) = case c of
| blue () => 0
| red () => 1
| yellow () => 2
(* ****** ****** *)
implement
int_to_color (i) =
if (i = 1) then red () else
if (i = 0) then blue () else yellow ()
(* ****** ****** *)
implement
translate_color2 (c1, c2) =
case c1 of
| red () => ( case c2 of
| red () => red()
| blue () => yellow()
| yellow () => blue()
) // end of c1 => red()
| blue () => ( case c2 of
| red () => yellow ()
| blue () => blue ()
| yellow () => red ()
) // end of c1 => blue()
| yellow () => ( case c2 of
| red () => blue()
| blue () => red()
| yellow () => yellow()
) // end of c1 => yellow()
(* ****** ****** *)
/* action of chameneos that entered pool first*/
implement
chameneos_a {n} (pf1, pf2 | n) = let
//
val () = set_shared_color(pf1, pf2 | get_chameneos_color(pf1, pf2 | n))
val () = set_shared_id(pf1, pf2 | n)
val () = wait_for_chameneos() // block until signaled by secound chameneos
val n2 = get_shared_id (pf1, pf2 |)
val c1 = int_to_color(get_chameneos_color(pf1, pf2 | n))
val c2 = int_to_color(get_shared_color(pf1, pf2 |))
val () = register_exchange(pf1, pf2 | n, n2)
val () = reset_pool()
in
translate_color2(c1,c2)
end
(* ****** ****** *)
/* action of chameneos that entered pool secound */
implement
chameneos_b {n} (pf1, pf2 | n) = let
val oc = get_chameneos_color(pf1, pf2 | n)
val c1 = int_to_color(get_shared_color(pf1, pf2 | ))
val n2 = get_shared_id (pf1, pf2 |)
val () = set_shared_color(pf1, pf2 | oc)
val () = set_shared_id(pf1, pf2 | n)
val () = register_exchange(pf1, pf2 | n, n2)
val () = signal_chameneos() // once done, unlock & wake up other chameneos
in
translate_color2(int_to_color(oc),c1)
end
(* ****** ****** *)
implement
meet_chameneos {n} (pf, poolf | n) = let
// check if there already is a chameneos in the pool
// XXX:check occupancy locks
val nc = if (check_occupancy(pf, poolf| (*void*)) = 0)
then chameneos_a(pf, poolf | n)
else chameneos_b(pf, poolf | n)
in
set_chameneos_color(pf, poolf | n, color_to_int(nc))
end
(* ****** ****** *)
implement
chameneos_play {n} (pf | n) = let
fun loop ( pf: chameneos_v | n: int n) : void = let
// checks to prevent chameneos from locking when we've hit our max meetup
val (poolf | pooln) = enter_pool(pf | )
val () = if (pooln = 0 ) then meet_chameneos(pf, poolf | n)
val () = leave_pool(pf, poolf | )
in
/* check if we should loop again */
if (pooln = 1)
then kill_chameneos(pf | n)
else loop (pf | n)
end (* end of [loop] *)
in
loop (pf | n)
end // end of [do_phil]
(* ****** ****** *)
extern fun start_chameneos (n:int, c:int): void = "start_chameneos"
%{$
ats_void_type
start_chameneos ( ats_int_type n, ats_int_type c ) {
// TODO: optimize the pthread stack
// threadstack *p_threadstack ;
// p_threadstack = &threadstackarr[0] ;
// pthread_attr_setstack (&thread_attr, p_threadstack, sizeof(threadstack)) ;
int affinity;
pthread_attr_t thread_attr ;
cpu_set_t mask;
long num_cores = sysconf(_SC_NPROCESSORS_ONLN);
pthread_attr_init (&thread_attr) ;
cgroup[n].color = c;
//
affinity = n % num_cores;
CPU_ZERO( &mask );
CPU_SET( affinity, &mask );
//
pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &mask);
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&cgroup[n].id , &thread_attr, (void* (*)(void*))chameneos_play, (void*)(intptr_t)(n)) ;
// fprintf (stderr, "initialization is done: n = %i\n", n) ;
return;
} // end of [initialization]
%}
(* ****** ****** *)
dynload "chameneos_sync.dats"
(* ****** ****** *)
fun run_test(n:int, c:c2, len:int) : void = let
val() = setup_shared_data(len, n)
fun loop(i:int, c:c2) : void = case+ c of
| chameneos_nil() => () // println!("Chamenoes loaded")
| chameneos_con(x, y) => let
val col = color_to_int(x)
val () = start_chameneos(i,col)
in
loop(i+1, y)
end
//
in
loop(0, c)
end
(* ****** ****** *)
implement main (argc, argv) = let
(* Subfunctions *)
fun print_color (c:color) : void = case c of
| red () => print!("red")
| blue () =>print!("blue")
| yellow () =>print!("yellow")
//
fun print_chameneoses (a1: c2) : void =
case+ a1 of
| chameneos_nil() => println!()
| chameneos_con(x, y) => let
val () = print_color(x)
val () = print!(" ")
in
print_chameneoses(y)
end
//
fun print_color_map() : void = let
fun loop1(i:int) : void = let
fun loop2(i:int, j:int) : void = let
val () = print_color(int_to_color(i))
val () = print!(" + ")
val () = print_color(int_to_color(j))
val () = print!(" -> ")
val () = print_color(translate_color2(int_to_color(i),int_to_color(j)))
val () = println!()
in
if (j < num_colors - 1) then loop2( i, j+1)
end
val () = loop2(i, 0)
in
if (i < num_colors -1 ) then loop1(i+1)
end
in
loop1(0)
end
//
fun group_length(a1:c2) : int = case+ a1 of
| chameneos_con (_,y) => 1 + group_length(y) | chameneos_nil() => 0
(* Run tests *)
// get N (num meet-ups) from command line
val () = assert (argc >= 2)
val n = int1_of_string (argv.[1])
val () = assert (n >= 0)
// define test sets
val group1 = (blue() :: red() :: yellow() :: cnil () )
val group2 = (blue() :: red() :: yellow() :: red() :: yellow() :: blue() :: red() :: yellow() :: red() :: blue() :: cnil() )
val() = print_color_map()
val() = println!("")
// run test #1
val() = print_chameneoses(group1)
val() = run_test(n, group1, group_length(group1))
val() = wait_for_end(group_length(group1))
val() = println!("")
// run test #2
val() = print_chameneoses(group2)
val() = run_test(n, group2, group_length(group2))
val() = wait_for_end(group_length(group2))
in
(*void*)
end // end of [main]
(* ****** ****** *)
////