1 | #include <thread.hfa> |
---|
2 | #include <locks.hfa> |
---|
3 | #include <list.hfa> |
---|
4 | |
---|
5 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
6 | // cofor ( uC++ COFOR ) |
---|
7 | typedef void (*__cofor_body_t)( ssize_t ); |
---|
8 | |
---|
9 | void __Cofor__( ssize_t low, ssize_t high, __cofor_body_t loop_body ); |
---|
10 | |
---|
11 | #define COFOR( lidname, low, high, loopbody ) \ |
---|
12 | { \ |
---|
13 | void __CFA_loopLambda__( ssize_t lidname ) { \ |
---|
14 | loopbody \ |
---|
15 | } \ |
---|
16 | __Cofor__( low, high, __CFA_loopLambda__ ); \ |
---|
17 | } |
---|
18 | |
---|
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 ); |
---|
39 | if ( !node ) |
---|
40 | continue; |
---|
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 | |
---|
69 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
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 | } |
---|