-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchameneos.cats
203 lines (168 loc) · 4.69 KB
/
chameneos.cats
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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <string.h>
#include <limits.h>
#include <sched.h>
#include <unistd.h>
static sem_t pool_entry;
static sem_t first_entry;
static pthread_mutex_t mutex0;
static pthread_cond_t condwait;
// chameneos-specific info
typedef struct {
pthread_t id;
int color;
int matches;
int self_test;
}chameneos_s;
static chameneos_s *cgroup = NULL;
// Info shared between chameneoses
// access of shared should always be w/ a locked thread
typedef struct {
int color;
int id;
} shared;
static shared *cshared = NULL;
static ats_int_type shared_color = 0 ;
static ats_int_type sum_matches = 0;
static ats_int_type num_matches = 0;
static ats_int_type final_match_count = 0;
/* remove pool_v*/
static inline
ats_void_type leave_pool(ats_void_type) {return; };
/*shared data structure */
static inline
ats_void_type
setup_shared_data( ats_int_type n1, ats_int_type n2){
pthread_cond_init(&condwait, NULL);
pthread_mutex_init(&mutex0, NULL);
sem_init(&pool_entry, 0, 2);
sem_init(&first_entry, 0, 1);
cgroup = malloc( sizeof(chameneos_s)*n1);
cshared = malloc(sizeof(shared));
sum_matches = 0;
num_matches = n2;
final_match_count = n2+n2;
return;
}
/* FIXME: This function take from the C implementation on the SHOOTOUT website*/
static inline
char const* spell_number(int n)
{
static char buf [128];
static char const* numbers [] = {
" zero", " one", " two", " three", " four",
" five", " six", " seven", " eight", " nine"};
size_t tokens [32];
size_t token_count;
char const* tok;
char* pos;
token_count = 0;
do
{
tokens[token_count] = n % 10;
token_count += 1;
n /= 10;
}
while (n);
pos = buf;
while (token_count)
{
token_count -= 1;
tok = numbers[tokens[token_count]];
while (tok[0])
pos++[0] = tok++[0];
}
pos[0] = 0;
return buf;
}
static inline
ats_int_type check_match_count (ats_int_type n) {
if (sum_matches >= final_match_count) return 1;
else return 0;
}
static inline
ats_void_type print_match_count () {
printf("%s\n", spell_number(sum_matches));
}
static inline
ats_void_type kill_chameneos(ats_int_type n) {
printf("%d %d %s\n", cgroup[n].matches,sum_matches ,spell_number(cgroup[n].self_test));
return ;
}
static inline
ats_int_type get_shared_color () { return cshared->color ; }
static inline
ats_void_type set_shared_color (ats_int_type n) { cshared->color = n ; return ; }
static inline
ats_int_type get_shared_id () { return cshared->id ; }
static inline
ats_void_type set_shared_id (ats_int_type n) { cshared->id = n ; return ; }
static inline
ats_int_type get_chameneos_color (ats_int_type n) { return cgroup[n].color ; }
static inline
ats_void_type set_chameneos_color (ats_int_type n, ats_int_type c) { cgroup[n].color = c ; return ; }
static inline
ats_void_type monitor_lock_acquire () { pthread_mutex_lock (&mutex0) ; return ; }
static inline
ats_void_type monitor_lock_release () { pthread_mutex_unlock (&mutex0) ; return ; }
/* chameneos enters the pool to 'play' with another chameneoses */
static inline
ats_int_type enter_pool() {
// return immediatly if no more matches are required
int flag;
if (check_match_count(0) == 1) return 1;
flag = sem_trywait(&pool_entry);
if(flag == 0 && check_match_count(0) == 1) /*we've aquried the lock */
{
sem_post(&pool_entry);
return 1;
}
else
return flag;
}
/* chameneos checks if another chameneos is in the pool */
static inline
ats_int_type check_occupancy() { pthread_mutex_lock (&mutex0) ; return sem_trywait(&first_entry); }
/* */
static inline
ats_void_type wait_for_chameneos() { pthread_cond_wait(&condwait, &mutex0); return; }
static inline
ats_void_type do_exchange (ats_int_type n1, ats_int_type c) {
return ;
}
static inline
ats_void_type register_exchange (ats_int_type n1, ats_int_type n2) {
sum_matches++;
cgroup[n1].matches++ ;
if(n1==n2) cgroup[n1].self_test++ ;
return ;
}
/* */
static inline
ats_void_type
signal_chameneos() {
pthread_mutex_unlock (&mutex0) ; pthread_cond_signal(&condwait);
return;
}
/* allow two more chameneos to enter pool*/
static inline
ats_void_type reset_pool() {
sem_post(&first_entry);
sem_post(&pool_entry);
sem_post(&pool_entry);
pthread_mutex_unlock (&mutex0) ;
return;
}
static inline
ats_void_type
wait_for_end (ats_int_type n) {
int i;
for (i=0; i < n; i++)
pthread_join (cgroup[i].id, NULL);
//
printf("%s\n", spell_number(sum_matches));
return ;
} // end