ADT
aaron-thesis
arm-eh
ast-experimental
cleanup-dtors
deferred_resn
demangler
enum
forall-pointer-decay
jacob/cs343-translation
jenkins-sandbox
new-ast
new-ast-unique-expr
new-env
no_list
persistent-indexer
pthread-emulation
qualifiedEnum
resolv-new
stuck-waitfor-destruct
with_gc
|
Last change
on this file since d9c44c3 was d9c44c3, checked in by Thierry Delisle <tdelisle@…>, 10 years ago |
|
Implemented coroutine for i386 and added coroutines to tests
|
-
Property mode
set to
100644
|
|
File size:
1.6 KB
|
| Line | |
|---|
| 1 | #include <fstream>
|
|---|
| 2 | #include <threads>
|
|---|
| 3 |
|
|---|
| 4 | struct Fibonacci {
|
|---|
| 5 | coroutine c;
|
|---|
| 6 | coVtable v;
|
|---|
| 7 | int fn; // used for communication
|
|---|
| 8 | };
|
|---|
| 9 |
|
|---|
| 10 | coroutine* this_coroutine(Fibonacci* this);
|
|---|
| 11 | void co_main(Fibonacci* this);
|
|---|
| 12 | coVtable* vtable(Fibonacci* this);
|
|---|
| 13 |
|
|---|
| 14 | void co_main_fib(void* this) {
|
|---|
| 15 | co_main( (Fibonacci*) this );
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | coroutine* this_coroutine_fib(void* this) {
|
|---|
| 19 | return this_coroutine( (Fibonacci*) this);
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | void ?{}(Fibonacci* this) {
|
|---|
| 23 | this->fn = 0;
|
|---|
| 24 | this->v.main = co_main_fib;
|
|---|
| 25 | this->v.this_coroutine = this_coroutine_fib;
|
|---|
| 26 | start(this);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | void co_main(Fibonacci* this) {
|
|---|
| 30 | #ifdef MORE_DEBUG
|
|---|
| 31 | sout | "Starting main of coroutine " | this | endl;
|
|---|
| 32 | sout | "Started from " | this_coroutine(this)->last | endl;
|
|---|
| 33 | #endif
|
|---|
| 34 | int fn1, fn2; // retained between resumes
|
|---|
| 35 | this->fn = 0;
|
|---|
| 36 | fn1 = this->fn;
|
|---|
| 37 | suspend(); // return to last resume
|
|---|
| 38 |
|
|---|
| 39 | this->fn = 1;
|
|---|
| 40 | fn2 = fn1;
|
|---|
| 41 | fn1 = this->fn;
|
|---|
| 42 | suspend(); // return to last resume
|
|---|
| 43 |
|
|---|
| 44 | for ( ;; ) {
|
|---|
| 45 | this->fn = fn1 + fn2;
|
|---|
| 46 | fn2 = fn1;
|
|---|
| 47 | fn1 = this->fn;
|
|---|
| 48 | suspend(); // return to last resume
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | int next(Fibonacci* this) {
|
|---|
| 53 | resume(this); // transfer to last suspend
|
|---|
| 54 | return this->fn;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | coroutine* this_coroutine(Fibonacci* this) {
|
|---|
| 58 | return &this->c;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | coVtable* vtable(Fibonacci* this) {
|
|---|
| 62 | return &this->v;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | int main() {
|
|---|
| 66 | Fibonacci f1, f2;
|
|---|
| 67 | #ifdef MORE_DEBUG
|
|---|
| 68 | sout | "User coroutines : " | &f1 | ' ' | &f1 | endl;
|
|---|
| 69 | #endif
|
|---|
| 70 | for ( int i = 1; i <= 10; i += 1 ) {
|
|---|
| 71 | sout | next(&f1) | ' ' | next(&f2) | endl;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | return 0;
|
|---|
| 75 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.