medfall

A super great game engine
Log | Files | Refs

mpsc_relacy.cc (1087B)


      1 #include "libs/relacy/relacy_cli.hpp"
      2 #include "mpsc.h"
      3 
      4 #define NUM_PRODUCER_THREADS 4
      5 #define NUM_PUSHES 10
      6 
      7 struct FixedMPSCTest : rl::test_suite< FixedMPSCTest, NUM_PRODUCER_THREADS + 1 > {
      8 	FixedMPSC< int, 4 > mpsc;
      9 
     10 	void thread( unsigned int thread_id ) {
     11 		if( thread_id == 0 ) {
     12 			int threads[ NUM_PRODUCER_THREADS ];
     13 			for( int i = 0; i < NUM_PRODUCER_THREADS; i++ )
     14 				threads[ i ] = 0;
     15 
     16 			while( true ) {
     17 				bool done = true;
     18 				for( int i = 0; i < NUM_PRODUCER_THREADS; i++ ) {
     19 					if( threads[ i ] < NUM_PUSHES ) {
     20 						done = false;
     21 					}
     22 				}
     23 
     24 				if( done )
     25 					break;
     26 
     27 				bool ok = false;
     28 				int x;
     29 				if( mpsc.dequeue( &x ) ) {
     30 					for( int i = 0; i < NUM_PRODUCER_THREADS; i++ ) {
     31 						if( threads[ i ] == x ) {
     32 							threads[ i ]++;
     33 							ok = true;
     34 							break;
     35 						}
     36 					}
     37 
     38 					ATOMIC_ASSERT( ok );
     39 				}
     40 			}
     41 		}
     42 		else {
     43 			int x = 0;
     44 			while( x < NUM_PUSHES ) {
     45 				if( mpsc.enqueue( x ) ) {
     46 					x++;
     47 				}
     48 			}
     49 		}
     50 	}
     51 };
     52 
     53 int main() {
     54 	rl::test_params p;
     55 	p.iteration_count = 200000;
     56 	rl::simulate< FixedMPSCTest >( p );
     57 	return 0;
     58 }
     59