- File:
-
- 1 edited
-
src/libcfa/concurrency/threads.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/threads.c
ree897e4b r8fcbb4c 17 17 #include "threads" 18 18 19 #include "kernel _private.h"19 #include "kernel" 20 20 #include "libhdr.h" 21 21 … … 44 44 (&this->c){}; 45 45 this->c.name = "Anonymous Coroutine"; 46 (&this-> terminated){};46 (&this->lock){}; 47 47 this->next = NULL; 48 48 } … … 72 72 //----------------------------------------------------------------------------- 73 73 // Starting and stopping threads 74 extern "C" { 75 forall(dtype T | is_thread(T)) 76 void CtxInvokeThread(T * this); 77 } 78 79 extern void thread_schedule( thread * ); 80 74 81 forall( dtype T | is_thread(T) ) 75 82 void start( T* this ) { … … 85 92 CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context ); 86 93 87 ScheduleThread(thrd_h); 94 fenv_t envp; 95 fegetenv( &envp ); 96 LIB_DEBUG_PRINTF("Thread : mxcsr %x\n", envp.__mxcsr); 97 LIB_DEBUG_PRINTF("Thread started : %p (t %p, c %p)\n", this, thrd_c, thrd_h); 98 99 thread_schedule(thrd_h); 88 100 } 89 101 90 102 forall( dtype T | is_thread(T) ) 91 103 void stop( T* this ) { 92 wait( & get_thread(this)->terminated ); 104 thread* thrd = get_thread(this); 105 if( thrd->c.notHalted ) { 106 lock( &thrd->lock ); 107 } 93 108 } 94 109 95 110 void yield( void ) { 96 ScheduleInternal( get_this_processor()->current_thread ); 111 get_this_processor()->thread_action = Reschedule; 112 suspend(); 97 113 } 98 114 99 115 void ThreadCtxSwitch(coroutine* src, coroutine* dst) { 116 dst->last = src; 117 100 118 // set state of current coroutine to inactive 101 119 src->state = Inactive; 102 dst->state = Active;103 120 104 // update the last resumer105 dst->last = src;121 // set new coroutine that task is executing 122 get_this_processor()->current_coroutine = dst; 106 123 107 // set new coroutine that the processor is executing 108 // and context switch to it 109 get_this_processor()->current_coroutine = dst; 124 // context switch to specified coroutine 110 125 CtxSwitch( src->stack.context, dst->stack.context ); 111 get_this_processor()->current_coroutine = src;126 // when CtxSwitch returns we are back in the src coroutine 112 127 113 128 // set state of new coroutine to active 114 dst->state = Inactive;115 129 src->state = Active; 116 130 } … … 120 134 extern "C" { 121 135 void __thread_signal_termination( thread * this ) { 122 this->c.state = Halt ed;123 LIB_DEBUG_PRINTF("Thread end : %p\n", this);124 signal( &this->terminated );136 this->c.state = Halt; 137 this->c.notHalted = false; 138 unlock( &this->lock ); 125 139 } 126 140 }
Note:
See TracChangeset
for help on using the changeset viewer.