[bb662027] | 1 | #include <stdio.h>
|
---|
| 2 | #include <stdlib.h>
|
---|
[df65c0c] | 3 | #include <clib/cfathread.h>
|
---|
[e0c072c] | 4 | #include <bits/defs.hfa>
|
---|
[df65c0c] | 5 |
|
---|
| 6 | extern "C" {
|
---|
| 7 | void _exit(int status);
|
---|
| 8 | }
|
---|
[bb662027] | 9 |
|
---|
| 10 | thread_local struct drand48_data buffer = { 0 };
|
---|
| 11 | int myrand() {
|
---|
| 12 | long int result;
|
---|
| 13 | lrand48_r(&buffer, &result);
|
---|
| 14 | return result;
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | enum Constants { blocked_size = 20 };
|
---|
| 19 | cfathread_t volatile blocked[blocked_size];
|
---|
| 20 |
|
---|
[df65c0c] | 21 | void * Worker( void * ) {
|
---|
[bb662027] | 22 | for(int i = 0; i < 1000; i++) {
|
---|
| 23 | int idx = myrand() % blocked_size;
|
---|
| 24 | if(blocked[idx]) {
|
---|
| 25 | cfathread_t thrd = __atomic_exchange_n(&blocked[idx], NULL, __ATOMIC_SEQ_CST);
|
---|
| 26 | cfathread_unpark( thrd );
|
---|
| 27 | } else {
|
---|
[df65c0c] | 28 | cfathread_t thrd = __atomic_exchange_n(&blocked[idx], cfathread_self(), __ATOMIC_SEQ_CST);
|
---|
[bb662027] | 29 | cfathread_unpark( thrd );
|
---|
| 30 | cfathread_park();
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 | printf("Done\n");
|
---|
[df65c0c] | 34 | return NULL;
|
---|
[bb662027] | 35 | }
|
---|
| 36 |
|
---|
| 37 | volatile bool stop;
|
---|
[df65c0c] | 38 | void * Unparker( void * ) {
|
---|
[bb662027] | 39 | while(!stop) {
|
---|
| 40 | int idx = myrand() % blocked_size;
|
---|
| 41 | cfathread_t thrd = __atomic_exchange_n(&blocked[idx], NULL, __ATOMIC_SEQ_CST);
|
---|
| 42 | cfathread_unpark( thrd );
|
---|
| 43 | int r = myrand() % 20;
|
---|
| 44 | for( int i = 0; i < r; i++ ) {
|
---|
| 45 | cfathread_yield();
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | printf("Done Unparker\n");
|
---|
[df65c0c] | 49 | return NULL;
|
---|
[bb662027] | 50 | }
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | int main() {
|
---|
| 54 | stop = false;
|
---|
| 55 | for(int i = 0; i < blocked_size; i++) {
|
---|
| 56 | blocked[i] = NULL;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[df65c0c] | 59 | cfathread_cluster_t cl = cfathread_cluster_self();
|
---|
| 60 |
|
---|
| 61 | cfathread_cluster_add_worker( cl, NULL, NULL, NULL );
|
---|
| 62 | cfathread_cluster_add_worker( cl, NULL, NULL, NULL );
|
---|
| 63 | cfathread_cluster_add_worker( cl, NULL, NULL, NULL );
|
---|
[a5e7233] | 64 |
|
---|
| 65 | cfathread_attr_t attr;
|
---|
| 66 | cfathread_attr_init(&attr);
|
---|
| 67 | cfathread_attr_setcluster(&attr, cl);
|
---|
| 68 |
|
---|
[df65c0c] | 69 | cfathread_t u;
|
---|
[a5e7233] | 70 | cfathread_create( &u, &attr, Unparker, NULL );
|
---|
[bb662027] | 71 | {
|
---|
| 72 | cfathread_t t[20];
|
---|
| 73 | for(int i = 0; i < 20; i++) {
|
---|
[a5e7233] | 74 | cfathread_create( &t[i], &attr, Worker, NULL );
|
---|
[bb662027] | 75 | }
|
---|
| 76 | for(int i = 0; i < 20; i++) {
|
---|
[df65c0c] | 77 | cfathread_join( t[i], NULL );
|
---|
[bb662027] | 78 | }
|
---|
| 79 | }
|
---|
| 80 | stop = true;
|
---|
[df65c0c] | 81 | cfathread_join(u, NULL);
|
---|
[a5e7233] | 82 | cfathread_attr_destroy(&attr);
|
---|
[df65c0c] | 83 | fflush(stdout);
|
---|
| 84 | _exit(0);
|
---|
[bb662027] | 85 | }
|
---|