Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/concurrent/clib.c

    ra5e7233 rbb662027  
     1#include <clib/cfathread.h>
     2
    13#include <stdio.h>
    24#include <stdlib.h>
    3 #include <clib/cfathread.h>
    4 #include <bits/defs.hfa>
    5 
    6 extern "C" {
    7 void _exit(int status);
    8 }
    95
    106thread_local struct drand48_data buffer = { 0 };
     
    1915cfathread_t volatile blocked[blocked_size];
    2016
    21 void * Worker( void * ) {
     17void Worker( cfathread_t this ) {
    2218        for(int i = 0; i < 1000; i++) {
    2319                int idx = myrand() % blocked_size;
     
    2622                        cfathread_unpark( thrd );
    2723                } else {
    28                         cfathread_t thrd = __atomic_exchange_n(&blocked[idx], cfathread_self(), __ATOMIC_SEQ_CST);
     24                        cfathread_t thrd = __atomic_exchange_n(&blocked[idx], this, __ATOMIC_SEQ_CST);
    2925                        cfathread_unpark( thrd );
    3026                        cfathread_park();
     
    3228        }
    3329        printf("Done\n");
    34         return NULL;
    3530}
    3631
    3732volatile bool stop;
    38 void * Unparker( void * ) {
     33void Unparker( cfathread_t this ) {
    3934        while(!stop) {
    4035                int idx = myrand() % blocked_size;
     
    4742        }
    4843        printf("Done Unparker\n");
    49         return NULL;
    5044}
    5145
     
    5751        }
    5852
    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 );
    64 
    65         cfathread_attr_t attr;
    66         cfathread_attr_init(&attr);
    67         cfathread_attr_setcluster(&attr, cl);
    68 
    69         cfathread_t u;
    70         cfathread_create( &u, &attr, Unparker, NULL );
     53        cfathread_setproccnt( 4 );
     54        cfathread_t u = cfathread_create( Unparker );
    7155        {
    7256                cfathread_t t[20];
    7357                for(int i = 0; i < 20; i++) {
    74                         cfathread_create( &t[i], &attr, Worker, NULL );
     58                        t[i] = cfathread_create( Worker );
    7559                }
    7660                for(int i = 0; i < 20; i++) {
    77                         cfathread_join( t[i], NULL );
     61                        cfathread_join( t[i] );
    7862                }
    7963        }
    8064        stop = true;
    81         cfathread_join(u, NULL);
    82         cfathread_attr_destroy(&attr);
    83         fflush(stdout);
    84         _exit(0);
     65        cfathread_join(u);
     66        cfathread_setproccnt( 1 );
    8567}
Note: See TracChangeset for help on using the changeset viewer.