source: src/libcfa/concurrency/threads.c @ bd98b58

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since bd98b58 was bd98b58, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Kernel now uses intrusive lists and blocking locks for ready queue management.
Update the plan for concurrency.

  • Property mode set to 100644
File size: 2.6 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
25extern "C" {
26        #include <stddef.h>
27}
28
29/*thread_local*/ extern processor * this_processor;
30
31//-----------------------------------------------------------------------------
32// Forward declarations
33forall(otype T | is_thread(T) )
34void start( T* this );
35
36forall(otype T | is_thread(T) )
37void stop( T* this );
38
39//-----------------------------------------------------------------------------
40// Thread ctors and dtors
41
42void ?{}(thread_h* this) {
43        (&this->c){};
44        (&this->lock){};
45        this->next = NULL;
46}
47
48void ^?{}(thread_h* this) {
49        ^(&this->c){};
50}
51
52forall(otype T | is_thread(T) )
53void ?{}( thread(T)* this ) {
54        (&this->handle){};
55        start(&this->handle);
56}
57
58forall(otype T, ttype P | is_thread(T) | { void ?{}(T*, P); } )
59void ?{}( thread(T)* this, P params ) {
60        (&this->handle){ params };
61        start(&this->handle);
62}
63
64forall(otype T | is_thread(T) )
65void ^?{}( thread(T)* this ) {
66        stop(&this->handle);
67        ^(&this->handle){};
68}
69
70//-----------------------------------------------------------------------------
71// Starting and stopping threads
72extern "C" {
73      forall(dtype T | is_thread(T))
74      void CtxInvokeThread(T * this);
75}
76
77extern void thread_schedule( thread_h * );
78
79forall(otype T | is_thread(T))
80void start( T* this ) {
81        coroutine* thrd_c = get_coroutine(this);
82        thread_h*  thrd_h = get_thread   (this);
83        thrd_c->last = this_coroutine();
84        this_processor->current_coroutine = thrd_c;
85
86        // LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", handle, thrd_c, thrd_h);
87
88        create_stack(&thrd_c->stack, thrd_c->stack.size);
89        CtxStart(this, CtxInvokeThread);
90        CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
91
92        thread_schedule(thrd_h);
93}
94
95forall(otype T | is_thread(T) )
96void stop( T* this ) {
97        thread_h*  thrd = get_thread(this);
98        if( thrd->c.notHalted ) {
99                lock( &thrd->lock );
100        }
101}
102
103void signal_termination( thread_h * this ) {
104        this->c.state = Halt;
105      this->c.notHalted = false;
106        unlock( &this->lock );
107}
108
109void yield( void ) {
110        thread_schedule( this_thread() );
111        suspend();
112}
113
114// Local Variables: //
115// mode: c //
116// tab-width: 4 //
117// End: //
Note: See TracBrowser for help on using the repository browser.