Last change
on this file since 38f6e66 was
b6c3688,
checked in by Peter A. Buhr <pabuhr@…>, 2 years ago
|
rename directory ibm_CASCON19 to IBM_CASCON19
|
-
Property mode set to
100644
|
File size:
1.0 KB
|
Rev | Line | |
---|
[487198c] | 1 | // Original |
---|
| 2 | void sum_many_async(int[] inputs, lambda_t callback); |
---|
| 3 | |
---|
| 4 | // Target |
---|
| 5 | int sum_many (int[] inputs); |
---|
| 6 | |
---|
| 7 | //================================== |
---|
| 8 | |
---|
| 9 | int sum_many(int[] inputs) { |
---|
| 10 | // setup required data |
---|
| 11 | int result; |
---|
| 12 | semaphore_t sem = { 0 }; |
---|
| 13 | |
---|
| 14 | // call async function |
---|
| 15 | sum_many_async( inputs, (int sum) { |
---|
| 16 | result = sum; |
---|
| 17 | V(sem); |
---|
| 18 | }); |
---|
| 19 | |
---|
| 20 | // wait for result |
---|
| 21 | P(sem); |
---|
| 22 | |
---|
| 23 | // return |
---|
| 24 | return result; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | int sum_many_block(int[] inputs); |
---|
| 29 | int sum_many (int[] inputs); |
---|
| 30 | |
---|
| 31 | //================================== |
---|
| 32 | |
---|
| 33 | struct request_t { |
---|
| 34 | int[] inputs; |
---|
| 35 | int result; |
---|
| 36 | semaphore_t sem = { 0 }; |
---|
| 37 | }; |
---|
| 38 | |
---|
| 39 | void * call_sum_many_block(void * args) { |
---|
| 40 | request_t * req = (request_t *) args; |
---|
| 41 | req->result = sum_many_block(req->inputs); |
---|
| 42 | V(req->sem); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | int sum_many(int[] inputs) { |
---|
| 46 | // setup required data |
---|
| 47 | request_t req = { inputs }; |
---|
| 48 | |
---|
| 49 | // create thread to |
---|
| 50 | pthread_t thrd; |
---|
| 51 | int err = pthread_fork(&thrd, 0p, call_sum_many_block, &req); |
---|
| 52 | handle(err); |
---|
| 53 | |
---|
| 54 | // wait |
---|
| 55 | P(req.sem); |
---|
| 56 | |
---|
| 57 | // Cleanup pthread |
---|
| 58 | err = pthread_join(&thrd, 0p); |
---|
| 59 | handle(err); |
---|
| 60 | |
---|
| 61 | // get result |
---|
| 62 | return req.result; |
---|
| 63 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.