Changeset a362f97 for src/examples


Ignore:
Timestamp:
Jan 27, 2017, 3:27:34 PM (9 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
c0aa336
Parents:
6acb935 (diff), 0a86a30 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg2:software/cfa/cfa-cc

Location:
src/examples
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • src/examples/thread.c

    r6acb935 ra362f97  
     1#include <fstream>
    12#include <kernel>
    23#include <stdlib>
     
    45
    56// Start coroutine routines
    6 extern "C" {
    7       forall(dtype T | is_coroutine(T))
    8       void CtxInvokeCoroutine(T * this);
    9 
    10       forall(dtype T | is_coroutine(T))
    11       void CtxStart(T * this, void ( *invoke)(T *));
    12 
    13         forall(dtype T | is_coroutine(T))
    14       void CtxInvokeThread(T * this);
    15 }
    16 
    177struct MyThread {
    18         thread_h t;
     8        thread t;
    199        unsigned id;
    2010        unsigned count;
    2111};
    2212
     13DECL_THREAD(MyThread)
     14
    2315void ?{}( MyThread * this ) {
    24         this->id = 0;
    25         this->count = 10;
    2616}
    2717
     
    3121}
    3222
    33 void ^?{}( MyThread * this ) {}
    34 
    3523void main(MyThread* this) {
    36         printf("Main called with %p\n", this);
    37         printf("Thread %d : Suspending %d times\n", this->id, this->count);
     24        sout | "Thread" | this->id | " : Suspending" | this->count | "times" | endl;
     25        yield();
    3826
    3927        for(int i = 0; i < this->count; i++) {
    40                 printf("Thread %d : Suspend No. %d\n", this->id, i + 1);
    41                 printf("Back to %p\n", &this->t.c);
    42                 suspend();
     28                sout | "Thread" | this->id | " : Suspend No." | i + 1 | endl;
     29                yield();
    4330        }
    4431}
    4532
    46 thread_h* get_thread(MyThread* this) {
    47         return &this->t;
    48 }
     33int main(int argc, char* argv[]) {
    4934
    50 coroutine* get_coroutine(MyThread* this) {
    51         return &this->t.c;
    52 }
     35        unsigned itterations = 10u;
     36        if(argc == 2) {
     37                int val = ato(argv[1]);
     38                assert(val >= 0);
     39                itterations = val;
     40        }
    5341
    54 int main() {
     42        sout | "User main begin" | endl;
    5543
    56         thread(MyThread) thread1;
    57         thread(MyThread) thread2;
     44        {
     45                processor p;
     46                {
     47                        scoped(MyThread) thread1 = { 1u, itterations };
     48                        scoped(MyThread) thread2 = { 2u, itterations };
     49                }
     50        }
    5851
    59         thread2.handle.id = 1;
    60 
    61         printf("\n\nMain is %p\n", this_coroutine());
    62 
    63         kernel_run();
    64 
    65         printf("Kernel terminated correctly\n");
     52        sout | "User main end" | endl;
    6653
    6754        return 0;
Note: See TracChangeset for help on using the changeset viewer.