ADT
arm-eh
ast-experimental
enum
forall-pointer-decay
jacob/cs343-translation
jenkins-sandbox
new-ast
new-ast-unique-expr
pthread-emulation
qualifiedEnum
|
Last change
on this file since 9be45a2 was f019069, checked in by Thierry Delisle <tdelisle@…>, 6 years ago |
|
Some more work on suspend_then
|
-
Property mode
set to
100644
|
|
File size:
1.7 KB
|
| Line | |
|---|
| 1 | #include <fstream.hfa>
|
|---|
| 2 | #include <kernel.hfa>
|
|---|
| 3 | #include <monitor.hfa>
|
|---|
| 4 | #include <thread.hfa>
|
|---|
| 5 | #include <time.hfa>
|
|---|
| 6 | #include <stdlib.hfa>
|
|---|
| 7 | #include <string.h>
|
|---|
| 8 |
|
|---|
| 9 | #define __kick_rate 150000ul
|
|---|
| 10 | #include "long_tests.hfa"
|
|---|
| 11 |
|
|---|
| 12 | #ifndef PREEMPTION_RATE
|
|---|
| 13 | #define PREEMPTION_RATE 10`ms
|
|---|
| 14 | #endif
|
|---|
| 15 |
|
|---|
| 16 | Duration default_preemption() {
|
|---|
| 17 | return PREEMPTION_RATE;
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | #ifdef TEST_LONG
|
|---|
| 21 | static const unsigned long N = 600_000ul;
|
|---|
| 22 | #else
|
|---|
| 23 | static const unsigned long N = 1_000ul;
|
|---|
| 24 | #endif
|
|---|
| 25 |
|
|---|
| 26 | #if !defined(TEST_FOREVER)
|
|---|
| 27 | static inline void print(const char * const text ) {
|
|---|
| 28 | write( STDERR_FILENO, text, strlen(text) );
|
|---|
| 29 | }
|
|---|
| 30 | #else
|
|---|
| 31 | static inline void print(Printer & this, const char * const text ) {}
|
|---|
| 32 | #endif
|
|---|
| 33 |
|
|---|
| 34 | coroutine Coroutine {};
|
|---|
| 35 |
|
|---|
| 36 | volatile bool done = false;
|
|---|
| 37 | Coroutine * volatile the_cor = 0p;
|
|---|
| 38 |
|
|---|
| 39 | void store(Coroutine & cor) {
|
|---|
| 40 | __atomic_store_n(&the_cor, &cor, __ATOMIC_SEQ_CST);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | Coroutine * take(void) {
|
|---|
| 44 | Coroutine * val = 0p;
|
|---|
| 45 | Coroutine * ret = __atomic_exchange_n(&the_cor, val, __ATOMIC_SEQ_CST);
|
|---|
| 46 | assert(!ret || !the_cor);
|
|---|
| 47 | return ret;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | void main(Coroutine& this) {
|
|---|
| 51 | suspend();
|
|---|
| 52 | for(int i = 0; TEST(i < N); i++) {
|
|---|
| 53 |
|
|---|
| 54 | print("C - Suspending");
|
|---|
| 55 | void publish() {
|
|---|
| 56 | print("C - Publishing");
|
|---|
| 57 | assert(!the_cor);
|
|---|
| 58 | store( this );
|
|---|
| 59 | }
|
|---|
| 60 | suspend_then(publish);
|
|---|
| 61 | assert(!the_cor);
|
|---|
| 62 | print("Coroutine 2");
|
|---|
| 63 | KICK_WATCHDOG;
|
|---|
| 64 | yield();
|
|---|
| 65 | }
|
|---|
| 66 | done = true;
|
|---|
| 67 | suspend();
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | thread Thread {};
|
|---|
| 71 | void main(Thread & this) {
|
|---|
| 72 | Coroutine * mine = 0p;
|
|---|
| 73 | while(!done) {
|
|---|
| 74 | yield();
|
|---|
| 75 |
|
|---|
| 76 | mine = take();
|
|---|
| 77 | if(!mine) continue;
|
|---|
| 78 |
|
|---|
| 79 | print("T - took");
|
|---|
| 80 | resume(*mine);
|
|---|
| 81 | print("T - back");
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 | int main(int argc, char* argv[]) {
|
|---|
| 87 | processor p[2];
|
|---|
| 88 | Coroutine c;
|
|---|
| 89 | resume(c); // Prime the coroutine to avoid one of the threads being its starter
|
|---|
| 90 | the_cor = &c;
|
|---|
| 91 | {
|
|---|
| 92 | Thread t[2];
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.