-
Notifications
You must be signed in to change notification settings - Fork 11
/
CspChan.c
497 lines (441 loc) · 12.8 KB
/
CspChan.c
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* Copyright 2023 Rochus Keller <mailto:me@rochus-keller.ch>
*
* This file may be used under the terms of the GNU Lesser
* General Public License version 2.1 or version 3 as published by the Free
* Software Foundation and appearing in the file LICENSE.LGPLv21 and
* LICENSE.LGPLv3 included in the packaging of this file. Please review the
* following information to ensure the GNU Lesser General Public License
* requirements will be met: https://www.gnu.org/licenses/lgpl.html and
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
*
* Alternatively this file may be used under the terms of the Mozilla
* Public License. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "CspChan.h"
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <pthread.h>
#include <error.h>
#include <unistd.h>
#include <assert.h>
/* TODO: Win32 implementation */
enum { SignalsCount = 3 };
typedef struct Signals
{
pthread_cond_t* sig[SignalsCount];
struct Signals* next;
} Signals;
typedef struct CspChan_t
{
pthread_mutex_t srMtx, observerMtx;
pthread_cond_t condA; /* received | waiting for second thread */
pthread_cond_t condB; /* sent | waiting for channel free */
unsigned short msgLen;
unsigned short closed : 1;
unsigned short unbuffered : 1;
unsigned short barrierPhase : 2;
unsigned short expectingSender : 1;
union
{
struct { unsigned short queueLen, msgCount, rIdx, wIdx; };
void* dataPtr;
};
Signals observer;
unsigned char data[]; /* assumes flexible array members C89 extension, or a C99 compiler, or try with data[0] */
} CspChan_t;
#define CSP_CHECK(call) if( (call)!= 0 ) fprintf(stderr,"error calling " #call " in " __FILE__ " line %d\n", __LINE__);
#define CSP_WARN_CLOSED(c) if( (c)->closed ) fprintf(stderr,"warning: using closed channel in " __FILE__ " line %d\n", __LINE__);
CspChan_t* CspChan_create(unsigned short queueLen, unsigned short msgLen)
{
if( msgLen == 0 )
msgLen = 1;
/* queueLen == 0 is an unbuffered channel */
CspChan_t* c = (CspChan_t*)malloc(sizeof(CspChan_t) + queueLen*msgLen);
c->msgLen = msgLen;
c->closed = 0;
if( queueLen == 0 )
{
c->unbuffered = 1;
c->dataPtr = 0;
c->barrierPhase = 0;
}else
{
c->unbuffered = 0;
c->queueLen = queueLen;
c->msgCount = 0;
c->rIdx = 0;
c->wIdx = 0;
}
memset(&c->observer,0,sizeof(Signals));
CSP_CHECK(pthread_mutex_init(&c->srMtx,0));
CSP_CHECK(pthread_mutex_init(&c->observerMtx,0));
CSP_CHECK(pthread_cond_init(&c->condA,0));
CSP_CHECK(pthread_cond_init(&c->condB,0));
return c;
}
static void signal_all(CspChan_t* c)
{
CSP_CHECK(pthread_mutex_lock(&c->observerMtx));
Signals* s = &c->observer;
while( s )
{
int i;
for( i = 0; i < SignalsCount; i++ )
{
if( s->sig[i] )
CSP_CHECK(pthread_cond_signal(s->sig[i]));
}
s = s->next;
}
CSP_CHECK(pthread_mutex_unlock(&c->observerMtx));
}
void CspChan_dispose(CspChan_t* c)
{
CspChan_close(c);
CSP_CHECK(pthread_cond_destroy(&c->condB));
CSP_CHECK(pthread_cond_destroy(&c->condA));
CSP_CHECK(pthread_mutex_destroy(&c->observerMtx));
CSP_CHECK(pthread_mutex_destroy(&c->srMtx));
Signals* s = c->observer.next;
while(s)
{
Signals* ss = s;
s = s->next;
free(ss);
}
free(c);
}
static int is_full(CspChan_t* c)
{
return c->msgCount == c->queueLen;
}
static int is_empty(CspChan_t* c)
{
return c->msgCount == 0;
}
static void add_observer(CspChan_t* c, pthread_cond_t* sig)
{
CSP_CHECK(pthread_mutex_lock(&c->observerMtx));
Signals* s = &c->observer;
while( s )
{
int i;
for( i = 0; i < SignalsCount; i++ )
{
if( s->sig[i] == 0 )
{
s->sig[i] = sig;
CSP_CHECK(pthread_mutex_unlock(&c->observerMtx));
return;
}
}
s = s->next;
}
s = (Signals*)malloc(sizeof(Signals));
memset(s,0,sizeof(Signals));
s->sig[0] = sig;
s->next = c->observer.next;
c->observer.next = s;
CSP_CHECK(pthread_mutex_unlock(&c->observerMtx));
}
static void remove_observer(CspChan_t* c, pthread_cond_t* sig)
{
CSP_CHECK(pthread_mutex_lock(&c->observerMtx));
Signals* s = &c->observer;
while( s )
{
int i;
for( i = 0; i < SignalsCount; i++ )
{
if( s->sig[i] == sig )
{
s->sig[i] = 0;
CSP_CHECK(pthread_mutex_unlock(&c->observerMtx));
return;
}
}
s = s->next;
}
CSP_CHECK(pthread_mutex_unlock(&c->observerMtx));
}
static void send(CspChan_t* c, void* data)
{
if( c->closed )
return;
memcpy(c->data + c->wIdx * c->msgLen, data, c->msgLen);
c->wIdx = (c->wIdx + 1) % c->queueLen;
c->msgCount++;
}
static void receive(CspChan_t* c, void* data)
{
if( c->closed )
return;
memcpy(data, c->data + c->rIdx * c->msgLen, c->msgLen);
c->rIdx = (c->rIdx + 1) % c->queueLen;
c->msgCount--;
}
static void synctwo(CspChan_t* c, void* dataPtr, int thisIsSender)
{
start:
/* we come here with srMtx already locked */
if( c->closed )
{
CSP_CHECK(pthread_mutex_unlock(&c->srMtx));
return;
}
switch( c->barrierPhase )
{
case 0: /* I'm the first */
c->barrierPhase = 1;
c->expectingSender = !thisIsSender;
c->dataPtr = dataPtr;
signal_all(c);
while( !c->closed && c->barrierPhase != 2 )
CSP_CHECK(pthread_cond_wait(&c->condA,&c->srMtx));
c->barrierPhase = 0;
CSP_CHECK(pthread_mutex_unlock(&c->srMtx));
/* here a third thread can interfere */
CSP_CHECK(pthread_cond_signal(&c->condB));
break;
case 1: /* I'm the second */
if( c->expectingSender != thisIsSender )
{
/* the caller is not the expected one, wait for another and send this one to sleep */
CSP_CHECK(pthread_cond_wait(&c->condB,&c->srMtx));
goto start;
}
if( thisIsSender )
memcpy(c->dataPtr,dataPtr,c->msgLen);
else
memcpy(dataPtr,c->dataPtr,c->msgLen);
c->barrierPhase = 2;
CSP_CHECK(pthread_mutex_unlock(&c->srMtx));
/* here a third thread can interfere */
CSP_CHECK(pthread_cond_signal(&c->condA));
break;
case 2: /* channel occupied, wait */
CSP_CHECK(pthread_cond_wait(&c->condB,&c->srMtx));
goto start;
break;
}
}
void CspChan_send(CspChan_t* c, void* dataPtr)
{
CSP_CHECK(pthread_mutex_lock(&c->srMtx));
CSP_WARN_CLOSED(c); /* TODO: Golang panics in this case */
if( c->unbuffered )
{
synctwo(c,dataPtr,1);
}else
{
while( !c->closed && is_full(c) )
CSP_CHECK(pthread_cond_wait(&c->condA,&c->srMtx));
send(c,dataPtr);
CSP_CHECK(pthread_mutex_unlock(&c->srMtx));
/* NOTE: if the receiver disposes of the channel before the following is complete, a segfault might occur;
the order is relevant; c->sent must be signalled last, because observers usually don't dispose the channel */
signal_all(c);
CSP_CHECK(pthread_cond_signal(&c->condB));
}
}
void CspChan_receive(CspChan_t* c, void* dataPtr)
{
CSP_CHECK(pthread_mutex_lock(&c->srMtx));
if( c->closed )
{
memset(dataPtr,0,c->msgLen);
return;
}
if( c->unbuffered )
{
synctwo(c,dataPtr,0);
}else
{
while( !c->closed && is_empty(c) )
CSP_CHECK(pthread_cond_wait(&c->condB,&c->srMtx));
receive(c,dataPtr);
CSP_CHECK(pthread_mutex_unlock(&c->srMtx));
signal_all(c);
CSP_CHECK(pthread_cond_signal(&c->condA));
}
}
static int anyready(CspChan_t** receiver, unsigned int rCount,
CspChan_t** sender, unsigned int sCount, CspChan_t** ready)
{
int i = 0, n = 0, closed = 0;
while( i < (rCount+sCount) )
{
CspChan_t* c = 0;
if( i < rCount )
c = receiver[i];
else
{
c = sender[i-rCount];
CSP_WARN_CLOSED(c);
}
if( c->closed )
{
ready[i] = 0;
closed++;
}else if( pthread_mutex_trylock(&c->srMtx) == 0 )
{
int ok = 0;
if( c->unbuffered )
{
ok = c->barrierPhase == 1 &&
((c->expectingSender && i >= rCount) || (!c->expectingSender && i < rCount) );
}else
{
if( i < rCount )
ok = !is_empty(c);
else
ok = !is_full(c);
}
if( ok )
{
ready[i] = c;
n++;
}else
{
ready[i] = 0;
CSP_CHECK(pthread_mutex_unlock(&c->srMtx));
}
}else
ready[i] = 0;
i++;
}
if( n == 0 && closed )
n = -1;
return n;
}
static int doselect(int n, void** rData, unsigned int rCount, void** sData, unsigned int sCount, CspChan_t** ready)
{
if( n <= 0 )
return -1;
int candidate = rand() % n;
CspChan_t* c = 0;
int i;
for( i = 0; i < (rCount+sCount); i++ )
{
if( ready[i] )
{
if( candidate == 0 )
{
c = ready[i];
n = i;
}else
CSP_CHECK(pthread_mutex_unlock(&ready[i]->srMtx));
candidate--;
}
}
if( c->unbuffered )
{
if( n >= rCount )
memcpy(c->dataPtr,sData[n-rCount],c->msgLen);
else
memcpy(rData[n],c->dataPtr,c->msgLen);
c->barrierPhase = 2;
CSP_CHECK(pthread_mutex_unlock(&c->srMtx));
signal_all(c);
CSP_CHECK(pthread_cond_signal(&c->condA));
}else
{
if( n < rCount )
{
receive(c,rData[n]);
CSP_CHECK(pthread_mutex_unlock(&c->srMtx));
signal_all(c);
CSP_CHECK(pthread_cond_signal(&c->condA));
}else
{
send(c,sData[n-rCount]);
CSP_CHECK(pthread_mutex_unlock(&c->srMtx));
signal_all(c);
CSP_CHECK(pthread_cond_signal(&c->condB));
}
}
return n;
}
int CspChan_select(CspChan_t** receiver, void** rData, unsigned int rCount,
CspChan_t** sender, void** sData, unsigned int sCount)
{
CspChan_t** ready = (CspChan_t**)malloc(sizeof(CspChan_t*)*(rCount+sCount));
pthread_mutex_t mtx;
pthread_cond_t sig;
CSP_CHECK(pthread_mutex_init(&mtx,0));
CSP_CHECK(pthread_cond_init(&sig,0));
CSP_CHECK(pthread_mutex_lock(&mtx));
int i;
for( i = 0; i < (rCount+sCount); i++ )
{
if( i < rCount )
add_observer(receiver[i], &sig);
else
add_observer(sender[i-rCount], &sig);
}
int n;
while( (n = anyready(receiver, rCount, sender, sCount, ready)) == 0 )
CSP_CHECK(pthread_cond_wait(&sig,&mtx));
n = doselect(n, rData, rCount, sData, sCount, ready );
for( i = 0; i < (rCount+sCount); i++ )
{
if( i < rCount )
remove_observer(receiver[i], &sig);
else
remove_observer(sender[i-rCount], &sig);
}
CSP_CHECK(pthread_mutex_unlock(&mtx));
CSP_CHECK(pthread_cond_destroy(&sig));
CSP_CHECK(pthread_mutex_destroy(&mtx));
free(ready);
return n;
}
int CspChan_nb_select(CspChan_t** receiver, void** rData, unsigned int rCount,
CspChan_t** sender, void** sData, unsigned int sCount)
{
CspChan_t** ready = (CspChan_t**)malloc(sizeof(CspChan_t*)*(rCount+sCount));
int n;
n = anyready(receiver, rCount, sender, sCount, ready);
n = doselect(n, rData, rCount, sData, sCount, ready );
free(ready);
return n;
}
int CspChan_fork(void* (*agent)(void*), void* arg)
{
pthread_t t = 0;
pthread_attr_t attr;
CSP_CHECK(pthread_attr_init( &attr ));
CSP_CHECK(pthread_attr_setdetachstate ( &attr , PTHREAD_CREATE_DETACHED ));
const int res = pthread_create(&t,&attr,agent,arg);
pthread_attr_destroy ( &attr );
if( res != 0 )
{
fprintf(stderr,"error creating pthread: %d %s\n", res, strerror(res));
fflush(stderr);
return 0;
}else
return 1;
}
void CspChan_sleep(unsigned int milliseconds)
{
usleep(milliseconds*1000);
}
void CspChan_close(CspChan_t* c)
{
pthread_mutex_lock(&c->srMtx);
c->closed = 1;
pthread_mutex_unlock(&c->srMtx);
signal_all(c);
pthread_cond_broadcast(&c->condB);
pthread_cond_broadcast(&c->condA);
}
int CspChan_closed(CspChan_t* c)
{
if( c )
return c->closed;
else
return 1;
}