source: src/libcfa/concurrency/kernel.c @ 1c273d0

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

preemption works for threads

  • Property mode set to 100644
File size: 19.7 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// kernel.c --
9//
10// Author           : Thierry Delisle
11// Created On       : Tue Jan 17 12:27:26 2017
12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count     : 0
15//
16
17#include "startup.h"
18
19//Start and stop routine for the kernel, declared first to make sure they run first
20void kernel_startup(void)  __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
21void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
22
23//Header
24#include "kernel_private.h"
25
26//C Includes
27#include <stddef.h>
28extern "C" {
29#include <stdio.h>
30#include <fenv.h>
31#include <sys/resource.h>
32#include <signal.h>
33#include <unistd.h>
34}
35
36//CFA Includes
37#include "libhdr.h"
38#include "preemption.h"
39
40//Private includes
41#define __CFA_INVOKE_PRIVATE__
42#include "invoke.h"
43
44//-----------------------------------------------------------------------------
45// Kernel storage
46#define KERNEL_STORAGE(T,X) static char X##_storage[sizeof(T)]
47
48KERNEL_STORAGE(processorCtx_t, systemProcessorCtx);
49KERNEL_STORAGE(cluster, systemCluster);
50KERNEL_STORAGE(system_proc_t, systemProcessor);
51KERNEL_STORAGE(thread_desc, mainThread);
52KERNEL_STORAGE(machine_context_t, mainThread_context);
53
54cluster * systemCluster;
55system_proc_t * systemProcessor;
56thread_desc * mainThread;
57
58//-----------------------------------------------------------------------------
59// Global state
60
61volatile thread_local processor * this_processor;
62volatile thread_local coroutine_desc * this_coroutine;
63volatile thread_local thread_desc * this_thread;
64volatile thread_local unsigned short disable_preempt_count = 1;
65
66//-----------------------------------------------------------------------------
67// Main thread construction
68struct current_stack_info_t {
69        machine_context_t ctx;
70        unsigned int size;              // size of stack
71        void *base;                             // base of stack
72        void *storage;                  // pointer to stack
73        void *limit;                    // stack grows towards stack limit
74        void *context;                  // address of cfa_context_t
75        void *top;                              // address of top of storage
76};
77
78void ?{}( current_stack_info_t * this ) {
79        CtxGet( &this->ctx );
80        this->base = this->ctx.FP;
81        this->storage = this->ctx.SP;
82
83        rlimit r;
84        getrlimit( RLIMIT_STACK, &r);
85        this->size = r.rlim_cur;
86
87        this->limit = (void *)(((intptr_t)this->base) - this->size);
88        this->context = &mainThread_context_storage;
89        this->top = this->base;
90}
91
92void ?{}( coStack_t * this, current_stack_info_t * info) {
93        this->size = info->size;
94        this->storage = info->storage;
95        this->limit = info->limit;
96        this->base = info->base;
97        this->context = info->context;
98        this->top = info->top;
99        this->userStack = true;
100}
101
102void ?{}( coroutine_desc * this, current_stack_info_t * info) {
103        (&this->stack){ info };
104        this->name = "Main Thread";
105        this->errno_ = 0;
106        this->state = Start;
107}
108
109void ?{}( thread_desc * this, current_stack_info_t * info) {
110        (&this->cor){ info };
111}
112
113//-----------------------------------------------------------------------------
114// Processor coroutine
115void ?{}(processorCtx_t * this, processor * proc) {
116        (&this->__cor){ "Processor" };
117        this->proc = proc;
118        proc->runner = this;
119}
120
121void ?{}(processorCtx_t * this, processor * proc, current_stack_info_t * info) {
122        (&this->__cor){ info };
123        this->proc = proc;
124        proc->runner = this;
125}
126
127void ?{}(processor * this) {
128        this{ systemCluster };
129}
130
131void ?{}(processor * this, cluster * cltr) {
132        this->cltr = cltr;
133        (&this->terminated){};
134        this->is_terminated = false;
135        this->preemption_alarm = NULL;
136        this->preemption = default_preemption();
137        this->pending_preemption = false;
138
139        start( this );
140}
141
142void ?{}(processor * this, cluster * cltr, processorCtx_t * runner) {
143        this->cltr = cltr;
144        (&this->terminated){};
145        this->is_terminated = false;
146        this->preemption_alarm = NULL;
147        this->preemption = default_preemption();
148        this->pending_preemption = false;
149        this->kernel_thread = pthread_self();
150
151        this->runner = runner;
152        LIB_DEBUG_PRINT_SAFE("Kernel : constructing system processor context %p\n", runner);
153        runner{ this };
154}
155
156LIB_DEBUG_DO( bool validate( alarm_list_t * this ); )
157
158void ?{}(system_proc_t * this, cluster * cltr, processorCtx_t * runner) {
159        (&this->alarms){};
160        (&this->alarm_lock){};
161        this->pending_alarm = false;
162
163        (&this->proc){ cltr, runner };
164
165        verify( validate( &this->alarms ) );
166}
167
168void ^?{}(processor * this) {
169        if( ! this->is_terminated ) {
170                LIB_DEBUG_PRINT_SAFE("Kernel : core %p signaling termination\n", this);
171                this->is_terminated = true;
172                wait( &this->terminated );
173        }
174}
175
176void ?{}(cluster * this) {
177        ( &this->ready_queue ){};
178        ( &this->lock ){};
179}
180
181void ^?{}(cluster * this) {
182
183}
184
185//=============================================================================================
186// Kernel Scheduling logic
187//=============================================================================================
188//Main of the processor contexts
189void main(processorCtx_t * runner) {
190        processor * this = runner->proc;
191
192        LIB_DEBUG_PRINT_SAFE("Kernel : core %p starting\n", this);
193
194        {
195                // Setup preemption data
196                preemption_scope scope = { this };
197
198                LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);
199
200                thread_desc * readyThread = NULL;
201                for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ )
202                {
203                        readyThread = nextThread( this->cltr );
204
205                        if(readyThread)
206                        {
207                                verify( disable_preempt_count > 0 );
208
209                                runThread(this, readyThread);
210
211                                verify( disable_preempt_count > 0 );
212
213                                //Some actions need to be taken from the kernel
214                                finishRunning(this);
215
216                                spin_count = 0;
217                        }
218                        else
219                        {
220                                spin(this, &spin_count);
221                        }
222                }
223
224                LIB_DEBUG_PRINT_SAFE("Kernel : core %p stopping\n", this);
225        }
226
227        signal( &this->terminated );
228        LIB_DEBUG_PRINT_SAFE("Kernel : core %p terminated\n", this);
229}
230
231// runThread runs a thread by context switching
232// from the processor coroutine to the target thread
233void runThread(processor * this, thread_desc * dst) {
234        coroutine_desc * proc_cor = get_coroutine(this->runner);
235        coroutine_desc * thrd_cor = get_coroutine(dst);
236
237        //Reset the terminating actions here
238        this->finish.action_code = No_Action;
239
240        //Update global state
241        this_thread = dst;
242
243        // Context Switch to the thread
244        ThreadCtxSwitch(proc_cor, thrd_cor);
245        // when ThreadCtxSwitch returns we are back in the processor coroutine
246}
247
248// Once a thread has finished running, some of
249// its final actions must be executed from the kernel
250void finishRunning(processor * this) {
251        if( this->finish.action_code == Release ) {
252                unlock( this->finish.lock );
253        }
254        else if( this->finish.action_code == Schedule ) {
255                ScheduleThread( this->finish.thrd );
256        }
257        else if( this->finish.action_code == Release_Schedule ) {
258                unlock( this->finish.lock );
259                ScheduleThread( this->finish.thrd );
260        }
261        else if( this->finish.action_code == Release_Multi ) {
262                for(int i = 0; i < this->finish.lock_count; i++) {
263                        unlock( this->finish.locks[i] );
264                }
265        }
266        else if( this->finish.action_code == Release_Multi_Schedule ) {
267                for(int i = 0; i < this->finish.lock_count; i++) {
268                        unlock( this->finish.locks[i] );
269                }
270                for(int i = 0; i < this->finish.thrd_count; i++) {
271                        ScheduleThread( this->finish.thrds[i] );
272                }
273        }
274        else {
275                assert(this->finish.action_code == No_Action);
276        }
277}
278
279// Handles spinning logic
280// TODO : find some strategy to put cores to sleep after some time
281void spin(processor * this, unsigned int * spin_count) {
282        (*spin_count)++;
283}
284
285// Context invoker for processors
286// This is the entry point for processors (kernel threads)
287// It effectively constructs a coroutine by stealing the pthread stack
288void * CtxInvokeProcessor(void * arg) {
289        processor * proc = (processor *) arg;
290        this_processor = proc;
291        this_coroutine = NULL;
292        this_thread = NULL;
293        disable_preempt_count = 1;
294        // SKULLDUGGERY: We want to create a context for the processor coroutine
295        // which is needed for the 2-step context switch. However, there is no reason
296        // to waste the perfectly valid stack create by pthread.
297        current_stack_info_t info;
298        machine_context_t ctx;
299        info.context = &ctx;
300        processorCtx_t proc_cor_storage = { proc, &info };
301
302        LIB_DEBUG_PRINT_SAFE("Coroutine : created stack %p\n", proc_cor_storage.__cor.stack.base);
303
304        //Set global state
305        this_coroutine = &proc->runner->__cor;
306        this_thread = NULL;
307
308        //We now have a proper context from which to schedule threads
309        LIB_DEBUG_PRINT_SAFE("Kernel : core %p created (%p, %p)\n", proc, proc->runner, &ctx);
310
311        // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
312        // resume it to start it like it normally would, it will just context switch
313        // back to here. Instead directly call the main since we already are on the
314        // appropriate stack.
315        proc_cor_storage.__cor.state = Active;
316        main( &proc_cor_storage );
317        proc_cor_storage.__cor.state = Halted;
318
319        // Main routine of the core returned, the core is now fully terminated
320        LIB_DEBUG_PRINT_SAFE("Kernel : core %p main ended (%p)\n", proc, proc->runner);
321
322        return NULL;
323}
324
325void start(processor * this) {
326        LIB_DEBUG_PRINT_SAFE("Kernel : Starting core %p\n", this);
327
328        // SIGALRM must only be caught by the system processor
329        sigset_t old_mask;
330        bool is_system_proc = this_processor == &systemProcessor->proc;
331        if ( is_system_proc ) {
332                // Child kernel-thread inherits the signal mask from the parent kernel-thread. So one special case for the
333                // system processor creating the user processor => toggle the blocking SIGALRM on system processor, create user
334                // processor, and toggle back (below) previous signal mask of the system processor.
335
336                sigset_t new_mask;
337                sigemptyset( &new_mask );
338                sigemptyset( &old_mask );
339                sigaddset( &new_mask, SIGALRM );
340
341                if ( sigprocmask( SIG_BLOCK, &new_mask, &old_mask ) == -1 ) {
342                        abortf( "internal error, sigprocmask" );
343                }
344
345                assert( ! sigismember( &old_mask, SIGALRM ) );
346        }
347
348        pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
349
350        // Toggle back previous signal mask of system processor.
351        if ( is_system_proc ) {
352                if ( sigprocmask( SIG_SETMASK, &old_mask, NULL ) == -1 ) {
353                        abortf( "internal error, sigprocmask" );
354                } // if
355        } // if
356
357        LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);
358}
359
360//-----------------------------------------------------------------------------
361// Scheduler routines
362void ScheduleThread( thread_desc * thrd ) {
363        // if( !thrd ) return;
364        assert( thrd );
365        assert( thrd->cor.state != Halted );
366
367        verify( disable_preempt_count > 0 );
368
369        verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
370
371        lock( &systemProcessor->proc.cltr->lock, __PRETTY_FUNCTION__ );
372        append( &systemProcessor->proc.cltr->ready_queue, thrd );
373        unlock( &systemProcessor->proc.cltr->lock );
374
375        verify( disable_preempt_count > 0 );
376}
377
378thread_desc * nextThread(cluster * this) {
379        verify( disable_preempt_count > 0 );
380        lock( &this->lock, __PRETTY_FUNCTION__ );
381        thread_desc * head = pop_head( &this->ready_queue );
382        unlock( &this->lock );
383        verify( disable_preempt_count > 0 );
384        return head;
385}
386
387void BlockInternal() {
388        disable_interrupts();
389        verify( disable_preempt_count > 0 );
390        suspend();
391        verify( disable_preempt_count > 0 );
392        enable_interrupts( __PRETTY_FUNCTION__ );
393}
394
395void BlockInternal( spinlock * lock ) {
396        disable_interrupts();
397        this_processor->finish.action_code = Release;
398        this_processor->finish.lock = lock;
399
400        verify( disable_preempt_count > 0 );
401        suspend();
402        verify( disable_preempt_count > 0 );
403
404        enable_interrupts( __PRETTY_FUNCTION__ );
405}
406
407void BlockInternal( thread_desc * thrd ) {
408        disable_interrupts();
409        assert( thrd->cor.state != Halted );
410        this_processor->finish.action_code = Schedule;
411        this_processor->finish.thrd = thrd;
412
413        verify( disable_preempt_count > 0 );
414        suspend();
415        verify( disable_preempt_count > 0 );
416
417        enable_interrupts( __PRETTY_FUNCTION__ );
418}
419
420void BlockInternal( spinlock * lock, thread_desc * thrd ) {
421        disable_interrupts();
422        this_processor->finish.action_code = Release_Schedule;
423        this_processor->finish.lock = lock;
424        this_processor->finish.thrd = thrd;
425
426        verify( disable_preempt_count > 0 );
427        suspend();
428        verify( disable_preempt_count > 0 );
429
430        enable_interrupts( __PRETTY_FUNCTION__ );
431}
432
433void BlockInternal(spinlock ** locks, unsigned short count) {
434        disable_interrupts();
435        this_processor->finish.action_code = Release_Multi;
436        this_processor->finish.locks = locks;
437        this_processor->finish.lock_count = count;
438
439        verify( disable_preempt_count > 0 );
440        suspend();
441        verify( disable_preempt_count > 0 );
442
443        enable_interrupts( __PRETTY_FUNCTION__ );
444}
445
446void BlockInternal(spinlock ** locks, unsigned short lock_count, thread_desc ** thrds, unsigned short thrd_count) {
447        disable_interrupts();
448        this_processor->finish.action_code = Release_Multi_Schedule;
449        this_processor->finish.locks = locks;
450        this_processor->finish.lock_count = lock_count;
451        this_processor->finish.thrds = thrds;
452        this_processor->finish.thrd_count = thrd_count;
453
454        verify( disable_preempt_count > 0 );
455        suspend();
456        verify( disable_preempt_count > 0 );
457
458        enable_interrupts( __PRETTY_FUNCTION__ );
459}
460
461//=============================================================================================
462// Kernel Setup logic
463//=============================================================================================
464//-----------------------------------------------------------------------------
465// Kernel boot procedures
466void kernel_startup(void) {
467        LIB_DEBUG_PRINT_SAFE("Kernel : Starting\n");
468
469        // Start by initializing the main thread
470        // SKULLDUGGERY: the mainThread steals the process main thread
471        // which will then be scheduled by the systemProcessor normally
472        mainThread = (thread_desc *)&mainThread_storage;
473        current_stack_info_t info;
474        mainThread{ &info };
475
476        LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n");
477
478        // Initialize the system cluster
479        systemCluster = (cluster *)&systemCluster_storage;
480        systemCluster{};
481
482        LIB_DEBUG_PRINT_SAFE("Kernel : System cluster ready\n");
483
484        // Initialize the system processor and the system processor ctx
485        // (the coroutine that contains the processing control flow)
486        systemProcessor = (system_proc_t *)&systemProcessor_storage;
487        systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtx_storage };
488
489        // Add the main thread to the ready queue
490        // once resume is called on systemProcessor->runner the mainThread needs to be scheduled like any normal thread
491        ScheduleThread(mainThread);
492
493        //initialize the global state variables
494        this_processor = &systemProcessor->proc;
495        this_thread = mainThread;
496        this_coroutine = &mainThread->cor;
497        disable_preempt_count = 1;
498
499        // Enable preemption
500        kernel_start_preemption();
501
502        // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX
503        // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
504        // mainThread is on the ready queue when this call is made.
505        resume( systemProcessor->proc.runner );
506
507
508
509        // THE SYSTEM IS NOW COMPLETELY RUNNING
510        LIB_DEBUG_PRINT_SAFE("Kernel : Started\n--------------------------------------------------\n\n");
511
512        enable_interrupts( __PRETTY_FUNCTION__ );
513}
514
515void kernel_shutdown(void) {
516        LIB_DEBUG_PRINT_SAFE("\n--------------------------------------------------\nKernel : Shutting down\n");
517
518        disable_interrupts();
519
520        // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.
521        // When its coroutine terminates, it return control to the mainThread
522        // which is currently here
523        systemProcessor->proc.is_terminated = true;
524        suspend();
525
526        // THE SYSTEM IS NOW COMPLETELY STOPPED
527
528        // Disable preemption
529        kernel_stop_preemption();
530
531        // Destroy the system processor and its context in reverse order of construction
532        // These were manually constructed so we need manually destroy them
533        ^(systemProcessor->proc.runner){};
534        ^(systemProcessor){};
535
536        // Final step, destroy the main thread since it is no longer needed
537        // Since we provided a stack to this taxk it will not destroy anything
538        ^(mainThread){};
539
540        LIB_DEBUG_PRINT_SAFE("Kernel : Shutdown complete\n");
541}
542
543static spinlock kernel_abort_lock;
544static spinlock kernel_debug_lock;
545static bool kernel_abort_called = false;
546
547void * kernel_abort    (void) __attribute__ ((__nothrow__)) {
548        // abort cannot be recursively entered by the same or different processors because all signal handlers return when
549        // the globalAbort flag is true.
550        lock( &kernel_abort_lock, __PRETTY_FUNCTION__ );
551
552        // first task to abort ?
553        if ( !kernel_abort_called ) {                   // not first task to abort ?
554                kernel_abort_called = true;
555                unlock( &kernel_abort_lock );
556        }
557        else {
558                unlock( &kernel_abort_lock );
559
560                sigset_t mask;
561                sigemptyset( &mask );
562                sigaddset( &mask, SIGALRM );                    // block SIGALRM signals
563                sigaddset( &mask, SIGUSR1 );                    // block SIGUSR1 signals
564                sigsuspend( &mask );                            // block the processor to prevent further damage during abort
565                _exit( EXIT_FAILURE );                          // if processor unblocks before it is killed, terminate it
566        }
567
568        return this_thread;
569}
570
571void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
572        thread_desc * thrd = kernel_data;
573
574        int len = snprintf( abort_text, abort_text_size, "Error occurred while executing task %.256s (%p)", thrd->cor.name, thrd );
575        __lib_debug_write( STDERR_FILENO, abort_text, len );
576
577        if ( thrd != this_coroutine ) {
578                len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine->name, this_coroutine );
579                __lib_debug_write( STDERR_FILENO, abort_text, len );
580        }
581        else {
582                __lib_debug_write( STDERR_FILENO, ".\n", 2 );
583        }
584}
585
586extern "C" {
587        void __lib_debug_acquire() {
588                lock(&kernel_debug_lock, __PRETTY_FUNCTION__);
589        }
590
591        void __lib_debug_release() {
592                unlock(&kernel_debug_lock);
593        }
594}
595
596//=============================================================================================
597// Kernel Utilities
598//=============================================================================================
599//-----------------------------------------------------------------------------
600// Locks
601void ?{}( spinlock * this ) {
602        this->lock = 0;
603}
604void ^?{}( spinlock * this ) {
605
606}
607
608bool try_lock( spinlock * this, const char * caller ) {
609        bool ret = this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0;
610        this->prev = caller;
611        return ret;
612}
613
614void lock( spinlock * this, const char * caller ) {
615        for ( unsigned int i = 1;; i += 1 ) {
616                if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) break;
617        }
618        this->prev = caller;
619}
620
621void unlock( spinlock * this ) {
622        __sync_lock_release_4( &this->lock );
623}
624
625void ?{}( signal_once * this ) {
626        this->cond = false;
627}
628void ^?{}( signal_once * this ) {
629
630}
631
632void wait( signal_once * this ) {
633        lock( &this->lock, __PRETTY_FUNCTION__ );
634        if( !this->cond ) {
635                append( &this->blocked, (thread_desc*)this_thread );
636                BlockInternal( &this->lock );
637        }
638        else {
639                unlock( &this->lock );
640        }
641}
642
643void signal( signal_once * this ) {
644        lock( &this->lock, __PRETTY_FUNCTION__ );
645        {
646                this->cond = true;
647
648                disable_interrupts();
649                thread_desc * it;
650                while( it = pop_head( &this->blocked) ) {
651                        ScheduleThread( it );
652                }
653                enable_interrupts( __PRETTY_FUNCTION__ );
654        }
655        unlock( &this->lock );
656}
657
658//-----------------------------------------------------------------------------
659// Queues
660void ?{}( __thread_queue_t * this ) {
661        this->head = NULL;
662        this->tail = &this->head;
663}
664
665void append( __thread_queue_t * this, thread_desc * t ) {
666        verify(this->tail != NULL);
667        *this->tail = t;
668        this->tail = &t->next;
669}
670
671thread_desc * pop_head( __thread_queue_t * this ) {
672        thread_desc * head = this->head;
673        if( head ) {
674                this->head = head->next;
675                if( !head->next ) {
676                        this->tail = &this->head;
677                }
678                head->next = NULL;
679        }
680        return head;
681}
682
683void ?{}( __condition_stack_t * this ) {
684        this->top = NULL;
685}
686
687void push( __condition_stack_t * this, __condition_criterion_t * t ) {
688        verify( !t->next );
689        t->next = this->top;
690        this->top = t;
691}
692
693__condition_criterion_t * pop( __condition_stack_t * this ) {
694        __condition_criterion_t * top = this->top;
695        if( top ) {
696                this->top = top->next;
697                top->next = NULL;
698        }
699        return top;
700}
701// Local Variables: //
702// mode: c //
703// tab-width: 4 //
704// End: //
Note: See TracBrowser for help on using the repository browser.