source: src/libcfa/concurrency/kernel.c@ db6f06a

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since db6f06a was db6f06a, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

Implemented better condition lock to solve race condition on thread/processor termination

  • Property mode set to 100644
File size: 13.8 KB
RevLine 
[8118303]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// kernel.c --
9//
10// Author : Thierry Delisle
[75f3522]11// Created On : Tue Jan 17 12:27:26 2017
[8118303]12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count : 0
15//
16
[0c92c9f]17//Start and stop routine for the kernel, declared first to make sure they run first
18void kernel_startup(void) __attribute__((constructor(101)));
19void kernel_shutdown(void) __attribute__((destructor(101)));
20
[8118303]21//Header
[75f3522]22#include "kernel_private.h"
[8118303]23
24//C Includes
[c84e80a]25#include <stddef.h>
[eb2e723]26extern "C" {
[8fcbb4c]27#include <fenv.h>
[eb2e723]28#include <sys/resource.h>
29}
[8118303]30
31//CFA Includes
32#include "libhdr.h"
33
34//Private includes
35#define __CFA_INVOKE_PRIVATE__
36#include "invoke.h"
37
[8fcbb4c]38static volatile int lock;
39
40void spin_lock( volatile int *lock ) {
41 for ( unsigned int i = 1;; i += 1 ) {
42 if ( *lock == 0 && __sync_lock_test_and_set_4( lock, 1 ) == 0 ) break;
43 }
44}
45
46void spin_unlock( volatile int *lock ) {
47 __sync_lock_release_4( lock );
48}
49
[8def349]50//-----------------------------------------------------------------------------
51// Kernel storage
52#define KERNEL_STORAGE(T,X) static char X##_storage[sizeof(T)]
53
54KERNEL_STORAGE(processorCtx_t, systemProcessorCtx);
55KERNEL_STORAGE(cluster, systemCluster);
56KERNEL_STORAGE(processor, systemProcessor);
[e15df4c]57KERNEL_STORAGE(thread, mainThread);
[8def349]58KERNEL_STORAGE(machine_context_t, mainThread_context);
59
[bd98b58]60cluster * systemCluster;
[eb2e723]61processor * systemProcessor;
[e15df4c]62thread * mainThread;
[eb2e723]63
[bd98b58]64//-----------------------------------------------------------------------------
65// Global state
66
[8def349]67thread_local processor * this_processor;
68
69processor * get_this_processor() {
70 return this_processor;
71}
[c84e80a]72
[bd98b58]73coroutine * this_coroutine(void) {
74 return this_processor->current_coroutine;
75}
76
[e15df4c]77thread * this_thread(void) {
[bd98b58]78 return this_processor->current_thread;
[c84e80a]79}
80
81//-----------------------------------------------------------------------------
[8def349]82// Main thread construction
83struct current_stack_info_t {
84 machine_context_t ctx;
85 unsigned int size; // size of stack
86 void *base; // base of stack
87 void *storage; // pointer to stack
88 void *limit; // stack grows towards stack limit
89 void *context; // address of cfa_context_t
90 void *top; // address of top of storage
[c84e80a]91};
92
[8def349]93void ?{}( current_stack_info_t * this ) {
94 CtxGet( &this->ctx );
95 this->base = this->ctx.FP;
96 this->storage = this->ctx.SP;
97
98 rlimit r;
[132fad4]99 getrlimit( RLIMIT_STACK, &r);
[8def349]100 this->size = r.rlim_cur;
101
102 this->limit = (void *)(((intptr_t)this->base) - this->size);
103 this->context = &mainThread_context_storage;
104 this->top = this->base;
105}
106
107void ?{}( coStack_t * this, current_stack_info_t * info) {
108 this->size = info->size;
109 this->storage = info->storage;
110 this->limit = info->limit;
111 this->base = info->base;
112 this->context = info->context;
113 this->top = info->top;
114 this->userStack = true;
115}
116
117void ?{}( coroutine * this, current_stack_info_t * info) {
118 (&this->stack){ info };
119 this->name = "Main Thread";
120 this->errno_ = 0;
121 this->state = Inactive;
122 this->notHalted = true;
123}
124
[e15df4c]125void ?{}( thread * this, current_stack_info_t * info) {
[8def349]126 (&this->c){ info };
127}
[c84e80a]128
[8def349]129//-----------------------------------------------------------------------------
130// Processor coroutine
[eb2e723]131void ?{}(processorCtx_t * this, processor * proc) {
132 (&this->c){};
[c84e80a]133 this->proc = proc;
[8fcbb4c]134 proc->runner = this;
[8def349]135}
136
137void ?{}(processorCtx_t * this, processor * proc, current_stack_info_t * info) {
138 (&this->c){ info };
139 this->proc = proc;
[8fcbb4c]140 proc->runner = this;
[8def349]141}
142
143void ?{}(processor * this) {
144 this{ systemCluster };
145}
146
147void ?{}(processor * this, cluster * cltr) {
148 this->cltr = cltr;
149 this->current_coroutine = NULL;
150 this->current_thread = NULL;
[db6f06a]151 (&this->terminated){};
152 this->is_terminated = false;
[8def349]153
154 start( this );
[c84e80a]155}
156
[8fcbb4c]157void ?{}(processor * this, cluster * cltr, processorCtx_t * runner) {
[8def349]158 this->cltr = cltr;
159 this->current_coroutine = NULL;
160 this->current_thread = NULL;
[db6f06a]161 (&this->terminated){};
162 this->is_terminated = false;
[8def349]163
[8fcbb4c]164 this->runner = runner;
165 LIB_DEBUG_PRINTF("Kernel : constructing processor context %p\n", runner);
166 runner{ this };
[8def349]167}
168
169void ^?{}(processor * this) {
[db6f06a]170 if( ! this->is_terminated ) {
[8def349]171 LIB_DEBUG_PRINTF("Kernel : core %p signaling termination\n", this);
[db6f06a]172 this->is_terminated = true;
173 wait( &this->terminated );
[8def349]174 }
175}
176
177void ?{}(cluster * this) {
178 ( &this->ready_queue ){};
[8fcbb4c]179 lock = 0;
[8def349]180}
181
182void ^?{}(cluster * this) {
[8fcbb4c]183
[c84e80a]184}
185
[75f3522]186//=============================================================================================
187// Kernel Scheduling logic
188//=============================================================================================
[8fcbb4c]189//Main of the processor contexts
190void main(processorCtx_t * runner) {
191 processor * this = runner->proc;
[eb2e723]192 LIB_DEBUG_PRINTF("Kernel : core %p starting\n", this);
[8118303]193
[e15df4c]194 thread * readyThread = NULL;
[db6f06a]195 for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ )
[75f3522]196 {
[bd98b58]197 readyThread = nextThread( this->cltr );
[8118303]198
[75f3522]199 if(readyThread)
200 {
201 runThread(this, readyThread);
202
203 //Some actions need to be taken from the kernel
[db6f06a]204 finishRunning(this);
[75f3522]205
[c84e80a]206 spin_count = 0;
[75f3522]207 }
208 else
209 {
[c84e80a]210 spin(this, &spin_count);
211 }
212 }
[8118303]213
[8def349]214 LIB_DEBUG_PRINTF("Kernel : core %p unlocking thread\n", this);
[db6f06a]215 signal( &this->terminated );
[c84e80a]216 LIB_DEBUG_PRINTF("Kernel : core %p terminated\n", this);
217}
218
[75f3522]219// runThread runs a thread by context switching
[0c92c9f]220// from the processor coroutine to the target thread
[75f3522]221void runThread(processor * this, thread * dst) {
222 coroutine * proc_cor = get_coroutine(this->runner);
223 coroutine * thrd_cor = get_coroutine(dst);
224
225 //Reset the terminating actions here
[db6f06a]226 this->finish.action_code = No_Action;
[8fcbb4c]227
[75f3522]228 //Update global state
229 this->current_thread = dst;
230
231 // Context Switch to the thread
232 ThreadCtxSwitch(proc_cor, thrd_cor);
233 // when ThreadCtxSwitch returns we are back in the processor coroutine
234}
235
236// Once a thread has finished running, some of
237// its final actions must be executed from the kernel
[db6f06a]238void finishRunning(processor * this) {
239 if( this->finish.action_code == Release ) {
240 unlock( this->finish.lock );
241 }
242 else if( this->finish.action_code == Schedule ) {
243 ScheduleThread( this->finish.thrd );
244 }
245 else if( this->finish.action_code == Release_Schedule ) {
246 unlock( this->finish.lock );
247 ScheduleThread( this->finish.thrd );
248 }
249 else {
250 assert(this->finish.action_code == No_Action);
[8fcbb4c]251 }
[c84e80a]252}
253
[0c92c9f]254// Handles spinning logic
255// TODO : find some strategy to put cores to sleep after some time
[c84e80a]256void spin(processor * this, unsigned int * spin_count) {
257 (*spin_count)++;
258}
259
[0c92c9f]260// Context invoker for processors
261// This is the entry point for processors (kernel threads)
262// It effectively constructs a coroutine by stealing the pthread stack
[8def349]263void * CtxInvokeProcessor(void * arg) {
264 processor * proc = (processor *) arg;
265 this_processor = proc;
266 // SKULLDUGGERY: We want to create a context for the processor coroutine
267 // which is needed for the 2-step context switch. However, there is no reason
268 // to waste the perfectly valid stack create by pthread.
269 current_stack_info_t info;
270 machine_context_t ctx;
271 info.context = &ctx;
272 processorCtx_t proc_cor_storage = { proc, &info };
273
[8fcbb4c]274 LIB_DEBUG_PRINTF("Coroutine : created stack %p\n", proc_cor_storage.c.stack.base);
275
[0c92c9f]276 //Set global state
[8fcbb4c]277 proc->current_coroutine = &proc->runner->c;
[8def349]278 proc->current_thread = NULL;
279
280 //We now have a proper context from which to schedule threads
[8fcbb4c]281 LIB_DEBUG_PRINTF("Kernel : core %p created (%p, %p)\n", proc, proc->runner, &ctx);
[8def349]282
283 // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
284 // resume it to start it like it normally would, it will just context switch
285 // back to here. Instead directly call the main since we already are on the
286 // appropriate stack.
287 proc_cor_storage.c.state = Active;
288 main( &proc_cor_storage );
289 proc_cor_storage.c.state = Halt;
290 proc_cor_storage.c.notHalted = false;
291
[0c92c9f]292 // Main routine of the core returned, the core is now fully terminated
[8fcbb4c]293 LIB_DEBUG_PRINTF("Kernel : core %p main ended (%p)\n", proc, proc->runner);
[8def349]294
295 return NULL;
[c84e80a]296}
297
[8def349]298void start(processor * this) {
299 LIB_DEBUG_PRINTF("Kernel : Starting core %p\n", this);
300
[8fcbb4c]301 // pthread_attr_t attributes;
302 // pthread_attr_init( &attributes );
[eb2e723]303
[8fcbb4c]304 pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
[eb2e723]305
[8fcbb4c]306 // pthread_attr_destroy( &attributes );
[eb2e723]307
[8def349]308 LIB_DEBUG_PRINTF("Kernel : core %p started\n", this);
[eb2e723]309}
310
[8def349]311//-----------------------------------------------------------------------------
312// Scheduler routines
[75f3522]313void ScheduleThread( thread * thrd ) {
[8def349]314 assertf( thrd->next == NULL, "Expected null got %p", thrd->next );
315
[db6f06a]316 lock( &systemProcessor->cltr->lock );
[8def349]317 append( &systemProcessor->cltr->ready_queue, thrd );
[db6f06a]318 unlock( &systemProcessor->cltr->lock );
319}
320
321thread * nextThread(cluster * this) {
322 lock( &this->lock );
323 thread * head = pop_head( &this->ready_queue );
324 unlock( &this->lock );
325 return head;
[eb2e723]326}
327
[75f3522]328void ScheduleInternal() {
329 suspend();
330}
331
[db6f06a]332void ScheduleInternal( spinlock * lock ) {
333 get_this_processor()->finish.action_code = Release;
334 get_this_processor()->finish.lock = lock;
335 suspend();
336}
337
338void ScheduleInternal( thread * thrd ) {
339 get_this_processor()->finish.action_code = Schedule;
340 get_this_processor()->finish.thrd = thrd;
341 suspend();
342}
343
344void ScheduleInternal( spinlock * lock, thread * thrd ) {
345 get_this_processor()->finish.action_code = Release_Schedule;
346 get_this_processor()->finish.lock = lock;
347 get_this_processor()->finish.thrd = thrd;
348 suspend();
[eb2e723]349}
350
351//-----------------------------------------------------------------------------
352// Kernel boot procedures
353void kernel_startup(void) {
354 LIB_DEBUG_PRINTF("Kernel : Starting\n");
355
356 // Start by initializing the main thread
[8fcbb4c]357 // SKULLDUGGERY: the mainThread steals the process main thread
358 // which will then be scheduled by the systemProcessor normally
[e15df4c]359 mainThread = (thread *)&mainThread_storage;
[8fcbb4c]360 current_stack_info_t info;
[8def349]361 mainThread{ &info };
[eb2e723]362
[bd98b58]363 // Initialize the system cluster
364 systemCluster = (cluster *)&systemCluster_storage;
365 systemCluster{};
366
[8def349]367 // Initialize the system processor and the system processor ctx
[eb2e723]368 // (the coroutine that contains the processing control flow)
[8def349]369 systemProcessor = (processor *)&systemProcessor_storage;
370 systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtx_storage };
[eb2e723]371
[dcb42b8]372 // Add the main thread to the ready queue
373 // once resume is called on systemProcessor->ctx the mainThread needs to be scheduled like any normal thread
[75f3522]374 ScheduleThread(mainThread);
[eb2e723]375
[dcb42b8]376 //initialize the global state variables
[bd98b58]377 this_processor = systemProcessor;
378 this_processor->current_thread = mainThread;
379 this_processor->current_coroutine = &mainThread->c;
[eb2e723]380
[dcb42b8]381 // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX
382 // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
383 // mainThread is on the ready queue when this call is made.
[8fcbb4c]384 resume(systemProcessor->runner);
[eb2e723]385
[dcb42b8]386
387
388 // THE SYSTEM IS NOW COMPLETELY RUNNING
[eb2e723]389 LIB_DEBUG_PRINTF("Kernel : Started\n--------------------------------------------------\n\n");
390}
391
[dcb42b8]392void kernel_shutdown(void) {
393 LIB_DEBUG_PRINTF("\n--------------------------------------------------\nKernel : Shutting down\n");
[eb2e723]394
[dcb42b8]395 // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.
396 // When its coroutine terminates, it return control to the mainThread
397 // which is currently here
[db6f06a]398 systemProcessor->is_terminated = true;
[eb2e723]399 suspend();
400
[dcb42b8]401 // THE SYSTEM IS NOW COMPLETELY STOPPED
[eb2e723]402
[dcb42b8]403 // Destroy the system processor and its context in reverse order of construction
404 // These were manually constructed so we need manually destroy them
[8fcbb4c]405 ^(systemProcessor->runner){};
[eb2e723]406 ^(systemProcessor){};
407
[dcb42b8]408 // Final step, destroy the main thread since it is no longer needed
409 // Since we provided a stack to this taxk it will not destroy anything
[eb2e723]410 ^(mainThread){};
411
412 LIB_DEBUG_PRINTF("Kernel : Shutdown complete\n");
[8118303]413}
414
[bd98b58]415//-----------------------------------------------------------------------------
416// Locks
[db6f06a]417// void ?{}( simple_lock * this ) {
418// ( &this->blocked ){};
419// }
420
421// void ^?{}( simple_lock * this ) {
422
423// }
424
425// void lock( simple_lock * this ) {
426// {
427// spin_lock( &lock );
428// append( &this->blocked, this_thread() );
429// spin_unlock( &lock );
430// }
431// ScheduleInternal();
432// }
433
434// void lock( simple_lock * this, spinlock * to_release ) {
435// {
436// spin_lock( &lock );
437// append( &this->blocked, this_thread() );
438// spin_unlock( &lock );
439// }
440// ScheduleInternal( to_release );
441// lock( to_release );
442// }
443
444// void unlock( simple_lock * this ) {
445// thread * it;
446// while( it = pop_head( &this->blocked) ) {
447// ScheduleThread( it );
448// }
449// }
450
451void ?{}( spinlock * this ) {
452 this->lock = 0;
[bd98b58]453}
[db6f06a]454void ^?{}( spinlock * this ) {
[bd98b58]455
[db6f06a]456}
457
458void lock( spinlock * this ) {
459 for ( unsigned int i = 1;; i += 1 ) {
460 if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) break;
461 }
462}
[bd98b58]463
[db6f06a]464void unlock( spinlock * this ) {
465 __sync_lock_release_4( &this->lock );
[bd98b58]466}
467
[db6f06a]468void ?{}( signal_once * this ) {
469 this->condition = false;
470}
471void ^?{}( signal_once * this ) {
472
473}
474
475void wait( signal_once * this ) {
476 lock( &this->lock );
477 if( !this->condition ) {
[8def349]478 append( &this->blocked, this_thread() );
[db6f06a]479 ScheduleInternal( &this->lock );
480 lock( &this->lock );
[8def349]481 }
[db6f06a]482 unlock( &this->lock );
[bd98b58]483}
484
[db6f06a]485void signal( signal_once * this ) {
486 lock( &this->lock );
487 {
488 this->condition = true;
489
490 thread * it;
491 while( it = pop_head( &this->blocked) ) {
492 ScheduleThread( it );
493 }
[bd98b58]494 }
[db6f06a]495 unlock( &this->lock );
[bd98b58]496}
497
498//-----------------------------------------------------------------------------
499// Queues
500void ?{}( simple_thread_list * this ) {
501 this->head = NULL;
502 this->tail = &this->head;
503}
504
[e15df4c]505void append( simple_thread_list * this, thread * t ) {
[bd98b58]506 assert( t->next == NULL );
507 *this->tail = t;
508 this->tail = &t->next;
509}
510
[e15df4c]511thread * pop_head( simple_thread_list * this ) {
512 thread * head = this->head;
[bd98b58]513 if( head ) {
514 this->head = head->next;
515 if( !head->next ) {
516 this->tail = &this->head;
517 }
518 head->next = NULL;
519 }
520
521 return head;
522}
[8118303]523// Local Variables: //
524// mode: c //
525// tab-width: 4 //
526// End: //
Note: See TracBrowser for help on using the repository browser.