#include #include #include #include #include #include #include size_t total_operations = 0; size_t Processors = 1, Tasks = 1; owner_lock o; // typedef channel_base( int, exp_backoff_then_block_lock ) Channel; typedef channel( int ) Channel; Channel * chain; bool done = false; int id_counter = 0; thread Task { int id; int right; }; static inline void ?{}( Task & this ) with(this) { id = __atomic_fetch_add( &id_counter, 1, __ATOMIC_SEQ_CST ); right = (id + 1) % Tasks; } void main(Task & this) with(this) { size_t runs = 0; int my_id = id; int my_right = right; for ( ;; ) { if ( done ) break; remove( chain[my_id] ); insert( chain[my_right], 0 ); runs++; } lock( o ); total_operations += runs; unlock( o ); } int main( int argc, char * argv[] ) { switch ( argc ) { case 3: if ( strcmp( argv[2], "d" ) != 0 ) { // default ? Tasks = atoi( argv[2] ); if ( Tasks < 1 ) goto Usage; } // if case 2: if ( strcmp( argv[1], "d" ) != 0 ) { // default ? Processors = atoi( argv[1] ); if ( Processors < 1 ) goto Usage; } // if case 1: // use defaults break; default: Usage: sout | "Usage: " | argv[0] | " [ processors (> 0) | 'd' (default " | Processors | ") ] [ channel size (>= 0) | 'd' (default " | Tasks | ") ]" ; exit( EXIT_FAILURE ); } // switch Tasks = Processors; processor proc[Processors - 1]; chain = aalloc( Tasks ); for ( i; Tasks ) { chain[i]{ 3 }; } insert( chain[0], 0 ); { Task t[Tasks]; sleep(10`s); done = true; for ( j; Tasks ) insert( chain[j], 0 ); } sout | total_operations; adelete(chain); return 0; }