source: src/libcfa/concurrency/preemption.c @ 381fdee

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 381fdee was 381fdee, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

harmonize conditional hardware-architecture code, fix conflicit with ftype variable on ARM

  • Property mode set to 100644
File size: 12.2 KB
RevLine 
[c81ebf9]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//
7// signal.c --
8//
9// Author           : Thierry Delisle
10// Created On       : Mon Jun 5 14:20:42 2017
[6b0b624]11// Last Modified By : Peter A. Buhr
[381fdee]12// Last Modified On : Fri Feb  9 14:42:34 2018
13// Update Count     : 25
[c81ebf9]14//
15
16#include "preemption.h"
17
18extern "C" {
[82ff5845]19#include <errno.h>
20#include <stdio.h>
21#include <string.h>
22#include <unistd.h>
[c81ebf9]23}
24
[dbe9b08]25#include "bits/signal.h"
[82ff5845]26
[969b3fe]27//TODO move to defaults
[82ff5845]28#define __CFA_DEFAULT_PREEMPTION__ 10000
[c81ebf9]29
[969b3fe]30//TODO move to defaults
[c81ebf9]31__attribute__((weak)) unsigned int default_preemption() {
32        return __CFA_DEFAULT_PREEMPTION__;
33}
34
[969b3fe]35// FwdDeclarations : timeout handlers
[c81ebf9]36static void preempt( processor   * this );
37static void timeout( thread_desc * this );
38
[969b3fe]39// FwdDeclarations : Signal handlers
[82ff5845]40void sigHandler_ctxSwitch( __CFA_SIGPARMS__ );
[1c273d0]41void sigHandler_segv     ( __CFA_SIGPARMS__ );
[2b8bc41]42void sigHandler_ill      ( __CFA_SIGPARMS__ );
43void sigHandler_fpe      ( __CFA_SIGPARMS__ );
[1c273d0]44void sigHandler_abort    ( __CFA_SIGPARMS__ );
[82ff5845]45
[969b3fe]46// FwdDeclarations : alarm thread main
47void * alarm_loop( __attribute__((unused)) void * args );
48
49// Machine specific register name
[381fdee]50#if   defined( __i386 )
[b2b44d8]51#define CFA_REG_IP gregs[REG_EIP]
[381fdee]52#elif defined( __x86_64 )
53#define CFA_REG_IP gregs[REG_RIP]
54#elif defined( __ARM_ARCH )
[b2b44d8]55#define CFA_REG_IP arm_pc
[381fdee]56#else
57#error unknown hardware architecture
[cd17862]58#endif
59
[969b3fe]60KERNEL_STORAGE(event_kernel_t, event_kernel);         // private storage for event kernel
61event_kernel_t * event_kernel;                        // kernel public handle to even kernel
62static pthread_t alarm_thread;                        // pthread handle to alarm thread
63
[65deb18]64void ?{}(event_kernel_t & this) with( this ) {
65        alarms{};
66        lock{};
[969b3fe]67}
[82ff5845]68
[c81ebf9]69//=============================================================================================
70// Kernel Preemption logic
71//=============================================================================================
72
[969b3fe]73// Get next expired node
74static inline alarm_node_t * get_expired( alarm_list_t * alarms, __cfa_time_t currtime ) {
75        if( !alarms->head ) return NULL;                          // If no alarms return null
76        if( alarms->head->alarm >= currtime ) return NULL;        // If alarms head not expired return null
77        return pop(alarms);                                       // Otherwise just pop head
78}
79
80// Tick one frame of the Discrete Event Simulation for alarms
[c81ebf9]81void tick_preemption() {
[969b3fe]82        alarm_node_t * node = NULL;                     // Used in the while loop but cannot be declared in the while condition
83        alarm_list_t * alarms = &event_kernel->alarms;  // Local copy for ease of reading
84        __cfa_time_t currtime = __kernel_get_time();    // Check current time once so we everything "happens at once"
[8cb529e]85
[969b3fe]86        //Loop throught every thing expired
87        while( node = get_expired( alarms, currtime ) ) {
[1c273d0]88
[969b3fe]89                // Check if this is a kernel
[c81ebf9]90                if( node->kernel_alarm ) {
91                        preempt( node->proc );
92                }
93                else {
94                        timeout( node->thrd );
95                }
96
[969b3fe]97                // Check if this is a periodic alarm
[8cb529e]98                __cfa_time_t period = node->period;
99                if( period > 0 ) {
[969b3fe]100                        node->alarm = currtime + period;    // Alarm is periodic, add currtime to it (used cached current time)
101                        insert( alarms, node );             // Reinsert the node for the next time it triggers
[c81ebf9]102                }
103                else {
[969b3fe]104                        node->set = false;                  // Node is one-shot, just mark it as not pending
[c81ebf9]105                }
106        }
107
[969b3fe]108        // If there are still alarms pending, reset the timer
109        if( alarms->head ) { __kernel_set_timer( alarms->head->alarm - currtime ); }
[c81ebf9]110}
111
[969b3fe]112// Update the preemption of a processor and notify interested parties
[c81ebf9]113void update_preemption( processor * this, __cfa_time_t duration ) {
114        alarm_node_t * alarm = this->preemption_alarm;
115
116        // Alarms need to be enabled
117        if ( duration > 0 && !alarm->set ) {
118                alarm->alarm = __kernel_get_time() + duration;
119                alarm->period = duration;
120                register_self( alarm );
121        }
122        // Zero duraction but alarm is set
123        else if ( duration == 0 && alarm->set ) {
124                unregister_self( alarm );
125                alarm->alarm = 0;
126                alarm->period = 0;
127        }
128        // If alarm is different from previous, change it
129        else if ( duration > 0 && alarm->period != duration ) {
130                unregister_self( alarm );
131                alarm->alarm = __kernel_get_time() + duration;
132                alarm->period = duration;
133                register_self( alarm );
134        }
135}
136
137//=============================================================================================
[cd17862]138// Kernel Signal Tools
[c81ebf9]139//=============================================================================================
140
[36982fc]141__cfaabi_dbg_debug_do( static thread_local void * last_interrupt = 0; )
[b227f68]142
[82ff5845]143extern "C" {
[969b3fe]144        // Disable interrupts by incrementing the counter
[82ff5845]145        void disable_interrupts() {
[d0a045c7]146                preemption_enabled = false;
147                __attribute__((unused)) unsigned short new_val = disable_preempt_count + 1;
148                disable_preempt_count = new_val;
[969b3fe]149                verify( new_val < 65_000u );              // If this triggers someone is disabling interrupts without enabling them
[82ff5845]150        }
151
[969b3fe]152        // Enable interrupts by decrementing the counter
153        // If counter reaches 0, execute any pending CtxSwitch
[36982fc]154        void enable_interrupts( __cfaabi_dbg_ctx_param ) {
[65deb18]155                processor   * proc = this_processor;      // Cache the processor now since interrupts can start happening after the atomic add
[969b3fe]156                thread_desc * thrd = this_thread;         // Cache the thread now since interrupts can start happening after the atomic add
157
[d0a045c7]158                unsigned short prev = disable_preempt_count;
159                disable_preempt_count -= 1;
[969b3fe]160                verify( prev != 0u );                     // If this triggers someone is enabled already enabled interruptsverify( prev != 0u );
161
162                // Check if we need to prempt the thread because an interrupt was missed
[d0a045c7]163                if( prev == 1 ) {
164                        preemption_enabled = true;
165                        if( proc->pending_preemption ) {
166                                proc->pending_preemption = false;
167                                BlockInternal( thrd );
168                        }
[82ff5845]169                }
[4e6fb8e]170
[969b3fe]171                // For debugging purposes : keep track of the last person to enable the interrupts
[36982fc]172                __cfaabi_dbg_debug_do( proc->last_enable = caller; )
[82ff5845]173        }
[969b3fe]174
175        // Disable interrupts by incrementint the counter
176        // Don't execute any pending CtxSwitch even if counter reaches 0
177        void enable_interrupts_noPoll() {
[d0a045c7]178                unsigned short prev = disable_preempt_count;
179                disable_preempt_count -= 1;
[2e9aed4]180                verifyf( prev != 0u, "Incremented from %u\n", prev );                     // If this triggers someone is enabled already enabled interrupts
[d0a045c7]181                if( prev == 1 ) {
182                        preemption_enabled = true;
183                }
[969b3fe]184        }
[82ff5845]185}
186
[969b3fe]187// sigprocmask wrapper : unblock a single signal
[1c273d0]188static inline void signal_unblock( int sig ) {
[82ff5845]189        sigset_t mask;
190        sigemptyset( &mask );
[1c273d0]191        sigaddset( &mask, sig );
[82ff5845]192
[47ecf2b]193        if ( pthread_sigmask( SIG_UNBLOCK, &mask, NULL ) == -1 ) {
194            abortf( "internal error, pthread_sigmask" );
[cd17862]195        }
[82ff5845]196}
197
[969b3fe]198// sigprocmask wrapper : block a single signal
[cd17862]199static inline void signal_block( int sig ) {
200        sigset_t mask;
201        sigemptyset( &mask );
202        sigaddset( &mask, sig );
[47ecf2b]203
[cd17862]204        if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) {
205            abortf( "internal error, pthread_sigmask" );
206        }
207}
[47ecf2b]208
[969b3fe]209// kill wrapper : signal a processor
[cd17862]210static void preempt( processor * this ) {
211        pthread_kill( this->kernel_thread, SIGUSR1 );
[1c273d0]212}
[82ff5845]213
[969b3fe]214// reserved for future use
[cd17862]215static void timeout( thread_desc * this ) {
216        //TODO : implement waking threads
217}
218
[969b3fe]219
220// Check if a CtxSwitch signal handler shoud defer
221// If true  : preemption is safe
222// If false : preemption is unsafe and marked as pending
223static inline bool preemption_ready() {
[d0a045c7]224        bool ready = preemption_enabled && !preemption_in_progress; // Check if preemption is safe
[969b3fe]225        this_processor->pending_preemption = !ready;                        // Adjust the pending flag accordingly
226        return ready;
227}
228
[cd17862]229//=============================================================================================
230// Kernel Signal Startup/Shutdown logic
231//=============================================================================================
232
[969b3fe]233// Startup routine to activate preemption
234// Called from kernel_startup
[cd17862]235void kernel_start_preemption() {
[36982fc]236        __cfaabi_dbg_print_safe("Kernel : Starting preemption\n");
[969b3fe]237
238        // Start with preemption disabled until ready
[d0a045c7]239        preemption_enabled = false;
[969b3fe]240        disable_preempt_count = 1;
241
242        // Initialize the event kernel
243        event_kernel = (event_kernel_t *)&storage_event_kernel;
[9236060]244        (*event_kernel){};
[969b3fe]245
246        // Setup proper signal handlers
[2b8bc41]247        __cfaabi_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO | SA_RESTART );         // CtxSwitch handler
[cd17862]248
249        signal_block( SIGALRM );
250
251        pthread_create( &alarm_thread, NULL, alarm_loop, NULL );
252}
253
[969b3fe]254// Shutdown routine to deactivate preemption
255// Called from kernel_shutdown
[cd17862]256void kernel_stop_preemption() {
[36982fc]257        __cfaabi_dbg_print_safe("Kernel : Preemption stopping\n");
[d6ff3ff]258
[969b3fe]259        // Block all signals since we are already shutting down
[cd17862]260        sigset_t mask;
261        sigfillset( &mask );
262        sigprocmask( SIG_BLOCK, &mask, NULL );
263
[969b3fe]264        // Notify the alarm thread of the shutdown
[a0b3e32]265        sigval val = { 1 };
266        pthread_sigqueue( alarm_thread, SIGALRM, val );
[969b3fe]267
268        // Wait for the preemption thread to finish
[cd17862]269        pthread_join( alarm_thread, NULL );
[969b3fe]270
271        // Preemption is now fully stopped
272
[36982fc]273        __cfaabi_dbg_print_safe("Kernel : Preemption stopped\n");
[cd17862]274}
275
[969b3fe]276// Raii ctor/dtor for the preemption_scope
277// Used by thread to control when they want to receive preemption signals
[242a902]278void ?{}( preemption_scope & this, processor * proc ) {
[9236060]279        (this.alarm){ proc, zero_time, zero_time };
[242a902]280        this.proc = proc;
281        this.proc->preemption_alarm = &this.alarm;
[969b3fe]282
[9236060]283        update_preemption( this.proc, from_us(this.proc->cltr->preemption) );
[cd17862]284}
285
[242a902]286void ^?{}( preemption_scope & this ) {
[cd17862]287        disable_interrupts();
288
[9236060]289        update_preemption( this.proc, zero_time );
[cd17862]290}
291
292//=============================================================================================
293// Kernel Signal Handlers
294//=============================================================================================
[47ecf2b]295
[969b3fe]296// Context switch signal handler
297// Receives SIGUSR1 signal and causes the current thread to yield
[1c273d0]298void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) {
[b2b44d8]299        __cfaabi_dbg_debug_do( last_interrupt = (void *)(cxt->uc_mcontext.CFA_REG_IP); )
[969b3fe]300
[b2b44d8]301        // Check if it is safe to preempt here
[969b3fe]302        if( !preemption_ready() ) { return; }
303
[2e9aed4]304        __cfaabi_dbg_print_buffer_decl(" KERNEL: preempting core %p (%p).\n", this_processor, this_thread);
[05615ba]305
[969b3fe]306        preemption_in_progress = true;                      // Sync flag : prevent recursive calls to the signal handler
307        signal_unblock( SIGUSR1 );                          // We are about to CtxSwitch out of the signal handler, let other handlers in
308        preemption_in_progress = false;                     // Clear the in progress flag
309
310        // Preemption can occur here
311
312        BlockInternal( (thread_desc*)this_thread );         // Do the actual CtxSwitch
[c81ebf9]313}
314
[969b3fe]315// Main of the alarm thread
316// Waits on SIGALRM and send SIGUSR1 to whom ever needs it
[cd17862]317void * alarm_loop( __attribute__((unused)) void * args ) {
[969b3fe]318        // Block sigalrms to control when they arrive
[cd17862]319        sigset_t mask;
320        sigemptyset( &mask );
321        sigaddset( &mask, SIGALRM );
322
323        if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) {
324            abortf( "internal error, pthread_sigmask" );
[82ff5845]325        }
[c81ebf9]326
[969b3fe]327        // Main loop
[cd17862]328        while( true ) {
[969b3fe]329                // Wait for a sigalrm
[a0b3e32]330                siginfo_t info;
331                int sig = sigwaitinfo( &mask, &info );
[969b3fe]332
[e2f7bc3]333                if( sig < 0 ) {
334                        //Error!
335                        int err = errno;
336                        switch( err ) {
337                                case EAGAIN :
338                                case EINTR :
339                                        continue;
340                        case EINVAL :
341                                        abortf("Timeout was invalid.");
342                                default:
343                                        abortf("Unhandled error %d", err);
344                        }
345                }
346
[969b3fe]347                // If another signal arrived something went wrong
[8cb529e]348                assertf(sig == SIGALRM, "Kernel Internal Error, sigwait: Unexpected signal %d (%d : %d)\n", sig, info.si_code, info.si_value.sival_int);
349
[36982fc]350                // __cfaabi_dbg_print_safe("Kernel : Caught alarm from %d with %d\n", info.si_code, info.si_value.sival_int );
[969b3fe]351                // Switch on the code (a.k.a. the sender) to
[8cb529e]352                switch( info.si_code )
[a0b3e32]353                {
[969b3fe]354                // Timers can apparently be marked as sent for the kernel
355                // In either case, tick preemption
[8cb529e]356                case SI_TIMER:
357                case SI_KERNEL:
[36982fc]358                        // __cfaabi_dbg_print_safe("Kernel : Preemption thread tick\n");
359                        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
[8cb529e]360                        tick_preemption();
[ea7d2b0]361                        unlock( event_kernel->lock );
[8cb529e]362                        break;
[969b3fe]363                // Signal was not sent by the kernel but by an other thread
[8cb529e]364                case SI_QUEUE:
[969b3fe]365                        // For now, other thread only signal the alarm thread to shut it down
366                        // If this needs to change use info.si_value and handle the case here
[8cb529e]367                        goto EXIT;
[cd17862]368                }
369        }
[a0b3e32]370
[8cb529e]371EXIT:
[36982fc]372        __cfaabi_dbg_print_safe("Kernel : Preemption thread stopping\n");
[a0b3e32]373        return NULL;
[82ff5845]374}
375
[6b0b624]376// Local Variables: //
377// mode: c //
378// tab-width: 4 //
379// End: //
Note: See TracBrowser for help on using the repository browser.