[1ed5e9e] | 1 | #include <thread.hfa>
|
---|
[ba0e1bc] | 2 | #include <locks.hfa>
|
---|
| 3 | #include <list.hfa>
|
---|
[1ed5e9e] | 4 |
|
---|
| 5 | //////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 6 | // cofor ( uC++ COFOR )
|
---|
[e4c3819] | 7 | typedef void (*__cofor_body_t)( ssize_t );
|
---|
[1ed5e9e] | 8 |
|
---|
[11ab0b4a] | 9 | void __Cofor__( ssize_t low, ssize_t high, __cofor_body_t loop_body );
|
---|
[1ed5e9e] | 10 |
|
---|
[e4c3819] | 11 | #define COFOR( lidname, low, high, loopbody ) \
|
---|
| 12 | { \
|
---|
| 13 | void __CFA_loopLambda__( ssize_t lidname ) { \
|
---|
| 14 | loopbody \
|
---|
| 15 | } \
|
---|
[11ab0b4a] | 16 | __Cofor__( low, high, __CFA_loopLambda__ ); \
|
---|
[e4c3819] | 17 | }
|
---|
[334e0cf2] | 18 |
|
---|
[ba0e1bc] | 19 | struct runner_node {
|
---|
| 20 | void * value;
|
---|
| 21 | inline dlink(runner_node);
|
---|
| 22 | };
|
---|
| 23 | P9_EMBEDDED( runner_node, dlink(runner_node) )
|
---|
| 24 |
|
---|
| 25 | thread cofor_runner {
|
---|
| 26 | go_mutex mutex_lock; // MX lock
|
---|
| 27 | dlist( runner_node ) items;
|
---|
| 28 | void (*func)(void *);
|
---|
| 29 | volatile bool done;
|
---|
| 30 | };
|
---|
| 31 |
|
---|
| 32 | void ?{}( cofor_runner & this ) { this.done = false; }
|
---|
| 33 |
|
---|
| 34 | void main( cofor_runner & this ) with(this) {
|
---|
| 35 | while ( !done || !items`isEmpty ) {
|
---|
| 36 | lock( mutex_lock );
|
---|
| 37 | runner_node * node = &try_pop_front( items );
|
---|
| 38 | unlock( mutex_lock );
|
---|
[0da9475] | 39 | if ( !node )
|
---|
| 40 | continue;
|
---|
[ba0e1bc] | 41 | func( node->value );
|
---|
| 42 | free( node->value );
|
---|
| 43 | free( node );
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | void start_runners( cofor_runner * thds, unsigned nprocs, void (*func)(void *) ) {
|
---|
| 48 | for ( i; nprocs ) {
|
---|
| 49 | thds[i].func = func;
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | void end_runners( cofor_runner * thds, unsigned nprocs ) {
|
---|
| 54 | for ( i; nprocs ) {
|
---|
| 55 | thds[i].done = true;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | void send_work( cofor_runner * thds, unsigned nprocs, unsigned & curr_proc, void * value ) {
|
---|
| 60 | runner_node * node = malloc();
|
---|
| 61 | (*node){};
|
---|
| 62 | node->value = value;
|
---|
| 63 | lock( thds[curr_proc].mutex_lock );
|
---|
| 64 | insert_last( thds[curr_proc].items, *node );
|
---|
| 65 | unlock( thds[curr_proc].mutex_lock );
|
---|
| 66 | curr_proc = ( curr_proc + 1 ) % nprocs;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[1ed5e9e] | 69 | //////////////////////////////////////////////////////////////////////////////////////////
|
---|
[ee9ad40] | 70 | // corun
|
---|
| 71 |
|
---|
| 72 | //
|
---|
| 73 | typedef void (*__CFA_corun_lambda_t)( void );
|
---|
| 74 |
|
---|
| 75 | // used to run a corun statement in parallel
|
---|
| 76 | thread co_runner {
|
---|
| 77 | __CFA_corun_lambda_t body;
|
---|
| 78 | };
|
---|
| 79 |
|
---|
| 80 | // wraps a co_runner to provide RAII deallocation
|
---|
| 81 | struct runner_block {
|
---|
| 82 | co_runner * runner;
|
---|
| 83 | };
|
---|
| 84 | static inline void ?{}( co_runner & this, __CFA_corun_lambda_t body ) { this.body = body; }
|
---|
| 85 |
|
---|
| 86 | void main( co_runner & this ) with( this ) { body(); }
|
---|
| 87 |
|
---|
| 88 | static inline void ?{}( runner_block & this ) {}
|
---|
| 89 | static inline void ?{}( runner_block & this, __CFA_corun_lambda_t body ) {
|
---|
| 90 | (*(this.runner = malloc())){ body };
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | static inline void ^?{}( runner_block & this ) {
|
---|
| 94 | delete( this.runner );
|
---|
| 95 | }
|
---|