source: src/libcfa/concurrency/threads.c @ 8f49a54

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 8f49a54 was 8f49a54, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Clean-up thread, kernel and examples

  • Property mode set to 100644
File size: 2.3 KB
Line 
1//                              -*- Mode: CFA -*-
2//
3// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4//
5// The contents of this file are covered under the licence agreement in the
6// file "LICENCE" distributed with Cforall.
7//
8// threads.c --
9//
10// Author           : Thierry Delisle
11// Created On       : Tue Jan 17 12:27:26 2016
12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count     : 0
15//
16
17#include "threads"
18
19#include "kernel"
20#include "libhdr.h"
21
22#define __CFA_INVOKE_PRIVATE__
23#include "invoke.h"
24
25#include <stdlib>
26
27//-----------------------------------------------------------------------------
28// Forward declarations
29forall(otype T | is_thread(T) )
30void start( thread(T)* this );
31
32forall(otype T | is_thread(T) )
33void stop( thread(T)* this );
34
35//-----------------------------------------------------------------------------
36// Thread ctors and dtors
37
38void ?{}(thread_h* this) {
39        (&this->c){};
40}
41
42void ^?{}(thread_h* this) {
43        ^(&this->c){};
44}
45
46forall(otype T | is_thread(T) )
47void ?{}( thread(T)* this ) {
48        (&this->handle){};
49        start(this);
50}
51
52forall(otype T, ttype P | is_thread(T) | { void ?{}(T*, P); } )
53void ?{}( thread(T)* this, P params ) {
54        (&this->handle){ params };
55        start(this);
56}
57
58forall(otype T | is_thread(T) )
59void ^?{}( thread(T)* this ) {
60        stop(this);
61        ^(&this->handle){};
62}
63
64//-----------------------------------------------------------------------------
65// Starting and stopping threads
66extern "C" {
67      forall(dtype T | is_thread(T))
68      void CtxInvokeThread(T * this);
69}
70
71forall(otype T | is_thread(T))
72void start( thread(T)* this ) {
73        T* handle  = &this->handle;
74        coroutine* thrd_c = get_coroutine(handle);
75        thread_h*  thrd_h = get_thread   (handle);
76        thrd_c->last = this_coroutine();
77        current_coroutine = thrd_c;
78
79        // LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", handle, thrd_c, thrd_h);
80
81        create_stack(&thrd_c->stack, thrd_c->stack.size);
82        CtxStart(handle, CtxInvokeThread);
83        CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
84
85        scheduler_add(thrd_h);
86}
87
88forall(otype T | is_thread(T) )
89void stop( thread(T)* this ) {
90        T* handle  = &this->handle;
91        thread_h*  thrd_h = get_thread   (handle);
92        while( thrd_h->c.notHalted ) {
93                suspend();
94        }
95}
96
97// Local Variables: //
98// mode: c //
99// tab-width: 4 //
100// End: //
Note: See TracBrowser for help on using the repository browser.