source: src/libcfa/concurrency/coroutine.c @ de94a60

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since de94a60 was afd550c, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Some more work on TLS macros

  • Property mode set to 100644
File size: 6.9 KB
RevLine 
[6a3d2e7]1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[75a17f1]7// coroutine.c --
[6a3d2e7]8//
9// Author           : Thierry Delisle
10// Created On       : Mon Nov 28 12:27:26 2016
[6b0b624]11// Last Modified By : Peter A. Buhr
[b10affd]12// Last Modified On : Fri Mar 30 17:20:57 2018
13// Update Count     : 9
[6a3d2e7]14//
15
[75a17f1]16#include "coroutine"
[bd98b58]17
[6a3d2e7]18extern "C" {
19#include <stddef.h>
20#include <malloc.h>
21#include <errno.h>
22#include <string.h>
23#include <unistd.h>
24#include <sys/mman.h>
25}
26
[9cc0472]27#include "kernel_private.h"
[6a3d2e7]28
29#define __CFA_INVOKE_PRIVATE__
30#include "invoke.h"
31
32//-----------------------------------------------------------------------------
33// Global state variables
34
35// minimum feasible stack size in bytes
36#define MinStackSize 1000
37static size_t pageSize = 0;                             // architecture pagesize HACK, should go in proper runtime singleton
38
39//-----------------------------------------------------------------------------
40// Coroutine ctors and dtors
[de6319f]41void ?{}( coStack_t & this, void * storage, size_t storageSize ) with( this ) {
42      size               = storageSize == 0 ? 65000 : storageSize; // size of stack
43      this.storage = storage;                                // pointer to stack
44      limit              = NULL;                                   // stack grows towards stack limit
45      base               = NULL;                                   // base of stack
46      context    = NULL;                                   // address of cfa_context_t
47      top                = NULL;                                   // address of top of storage
48      userStack  = storage != NULL;
[6a3d2e7]49}
50
[de6319f]51void ^?{}(coStack_t & this) {
52      if ( ! this.userStack && this.storage ) {
53            __cfaabi_dbg_debug_do(
54                  if ( mprotect( this.storage, pageSize, PROT_READ | PROT_WRITE ) == -1 ) {
55                        abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) );
56                  }
57            );
58            free( this.storage );
59      }
[6a3d2e7]60}
61
[de6319f]62void ?{}( coroutine_desc & this, const char * name, void * storage, size_t storageSize ) with( this ) {
63      (this.stack){storage, storageSize};
64      this.name = name;
65      errno_ = 0;
66      state = Start;
67      starter = NULL;
68      last = NULL;
[6a3d2e7]69}
70
[242a902]71void ^?{}(coroutine_desc& this) {}
[6a3d2e7]72
73// Part of the Public API
74// Not inline since only ever called once per coroutine
75forall(dtype T | is_coroutine(T))
[83a071f9]76void prime(T& cor) {
[de6319f]77      coroutine_desc* this = get_coroutine(cor);
78      assert(this->state == Start);
[6a3d2e7]79
[de6319f]80      this->state = Primed;
81      resume(cor);
[6a3d2e7]82}
83
[0c92c9f]84// Wrapper for co
[c3acb841]85void CoroutineCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
[14a61b5]86      // Safety note : This could cause some false positives due to preemption
[afd550c]87      verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate );
[de6319f]88      disable_interrupts();
[6a3d2e7]89
[de6319f]90      // set state of current coroutine to inactive
91      src->state = src->state == Halted ? Halted : Inactive;
[6a3d2e7]92
[de6319f]93      // set new coroutine that task is executing
[14a61b5]94      kernelTLS.this_coroutine = dst;
[6a3d2e7]95
[de6319f]96      // context switch to specified coroutine
97      assert( src->stack.context );
98      CtxSwitch( src->stack.context, dst->stack.context );
99      // when CtxSwitch returns we are back in the src coroutine
[6a3d2e7]100
[de6319f]101      // set state of new coroutine to active
102      src->state = Active;
[6a3d2e7]103
[de6319f]104      enable_interrupts( __cfaabi_dbg_ctx );
[14a61b5]105      // Safety note : This could cause some false positives due to preemption
[afd550c]106      verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate );
[6a3d2e7]107} //ctxSwitchDirect
108
[65deb18]109void create_stack( coStack_t* this, unsigned int storageSize ) with( *this ) {
[de6319f]110      //TEMP HACK do this on proper kernel startup
111      if(pageSize == 0ul) pageSize = sysconf( _SC_PAGESIZE );
112
113      size_t cxtSize = libCeiling( sizeof(machine_context_t), 8 ); // minimum alignment
114
115      if ( !storage ) {
116            __cfaabi_dbg_print_safe("Kernel : Creating stack of size %zu for stack obj %p\n", cxtSize + size + 8, this);
[6a3d2e7]117
[de6319f]118            userStack = false;
119            size = libCeiling( storageSize, 16 );
120            // use malloc/memalign because "new" raises an exception for out-of-memory
[6a3d2e7]121
[de6319f]122            // assume malloc has 8 byte alignment so add 8 to allow rounding up to 16 byte alignment
123            __cfaabi_dbg_debug_do( storage = memalign( pageSize, cxtSize + size + pageSize ) );
124            __cfaabi_dbg_no_debug_do( storage = malloc( cxtSize + size + 8 ) );
[47ecf2b]125
[de6319f]126            __cfaabi_dbg_debug_do(
127                  if ( mprotect( storage, pageSize, PROT_NONE ) == -1 ) {
128                        abort( "(uMachContext &)%p.createContext() : internal error, mprotect failure, error(%d) %s.", this, (int)errno, strerror( (int)errno ) );
129                  } // if
130            );
[6a3d2e7]131
[de6319f]132            if ( (intptr_t)storage == 0 ) {
133                  abort( "Attempt to allocate %zd bytes of storage for coroutine or task execution-state but insufficient memory available.", size );
134            } // if
[6a3d2e7]135
[de6319f]136            __cfaabi_dbg_debug_do( limit = (char *)storage + pageSize );
137            __cfaabi_dbg_no_debug_do( limit = (char *)libCeiling( (unsigned long)storage, 16 ) ); // minimum alignment
[6a3d2e7]138
[de6319f]139      } else {
140            __cfaabi_dbg_print_safe("Kernel : stack obj %p using user stack %p(%u bytes)\n", this, storage, storageSize);
[6a3d2e7]141
[de6319f]142            assertf( ((size_t)storage & (libAlign() - 1)) == 0ul, "Stack storage %p for task/coroutine must be aligned on %d byte boundary.", storage, (int)libAlign() );
143            userStack = true;
144            size = storageSize - cxtSize;
[6a3d2e7]145
[de6319f]146            if ( size % 16 != 0u ) size -= 8;
[6a3d2e7]147
[de6319f]148            limit = (char *)libCeiling( (unsigned long)storage, 16 ); // minimum alignment
149      } // if
150      assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %d bytes for a stack.", size, MinStackSize );
[6a3d2e7]151
[de6319f]152      base = (char *)limit + size;
153      context = base;
154      top = (char *)context + cxtSize;
[6a3d2e7]155}
156
[0c92c9f]157// We need to call suspend from invoke.c, so we expose this wrapper that
158// is not inline (We can't inline Cforall in C)
159extern "C" {
[de6319f]160      void __suspend_internal(void) {
161            suspend();
162      }
163
164      void __leave_coroutine(void) {
165            coroutine_desc * src = TL_GET( this_coroutine ); // optimization
166
167            assertf( src->starter != 0,
168                  "Attempt to suspend/leave coroutine \"%.256s\" (%p) that has never been resumed.\n"
169                  "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
170                  src->name, src );
171            assertf( src->starter->state != Halted,
172                  "Attempt by coroutine \"%.256s\" (%p) to suspend/leave back to terminated coroutine \"%.256s\" (%p).\n"
173                  "Possible cause is terminated coroutine's main routine has already returned.",
174                  src->name, src, src->starter->name, src->starter );
175
176            CoroutineCtxSwitch( src, src->starter );
177      }
[0c92c9f]178}
179
[6a3d2e7]180// Local Variables: //
181// mode: c //
182// tab-width: 4 //
[4aa2fb2]183// End: //
Note: See TracBrowser for help on using the repository browser.