Changeset e60e0dc


Ignore:
Timestamp:
Jul 17, 2017, 3:54:02 PM (4 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
5bd0aad
Parents:
b706db1
Message:

Some cleanu[ in the kernel, notably phasing out the system processor

Location:
src/libcfa/concurrency
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/concurrency/alarm.c

    rb706db1 re60e0dc  
    153153
    154154void register_self( alarm_node_t * this ) {
     155        alarm_list_t * alarms = &event_kernel->alarms;
     156
    155157        disable_interrupts();
    156         verify( !systemProcessor->pending_alarm );
    157         lock( &systemProcessor->alarm_lock DEBUG_CTX2 );
     158        lock( &event_kernel->lock DEBUG_CTX2 );
    158159        {
    159                 verify( validate( &systemProcessor->alarms ) );
    160                 bool first = !systemProcessor->alarms.head;
     160                verify( validate( alarms ) );
     161                bool first = !alarms->head;
    161162
    162                 insert( &systemProcessor->alarms, this );
    163                 if( systemProcessor->pending_alarm ) {
    164                         tick_preemption();
    165                 }
     163                insert( alarms, this );
    166164                if( first ) {
    167                         __kernel_set_timer( systemProcessor->alarms.head->alarm - __kernel_get_time() );
     165                        __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
    168166                }
    169167        }
    170         unlock( &systemProcessor->alarm_lock );
     168        unlock( &event_kernel->lock );
    171169        this->set = true;
    172170        enable_interrupts( DEBUG_CTX );
     
    174172
    175173void unregister_self( alarm_node_t * this ) {
    176         // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : unregister %p start\n", this );
    177174        disable_interrupts();
    178         lock( &systemProcessor->alarm_lock DEBUG_CTX2 );
     175        lock( &event_kernel->lock DEBUG_CTX2 );
    179176        {
    180                 verify( validate( &systemProcessor->alarms ) );
    181                 remove( &systemProcessor->alarms, this );
     177                verify( validate( &event_kernel->alarms ) );
     178                remove( &event_kernel->alarms, this );
    182179        }
    183         unlock( &systemProcessor->alarm_lock );
     180        unlock( &event_kernel->lock );
    184181        enable_interrupts( DEBUG_CTX );
    185182        this->set = false;
    186         // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Kernel : unregister %p end\n", this );
    187183}
  • src/libcfa/concurrency/kernel

    rb706db1 re60e0dc  
    2828//-----------------------------------------------------------------------------
    2929// Locks
    30 bool try_lock  ( spinlock * DEBUG_CTX_PARAM2 );
    31 void lock      ( spinlock * DEBUG_CTX_PARAM2 );
    32 void lock_yield( spinlock * DEBUG_CTX_PARAM2 );
    33 void unlock    ( spinlock * );
     30void lock      ( spinlock * DEBUG_CTX_PARAM2 );       // Lock the spinlock, spin if already acquired
     31void lock_yield( spinlock * DEBUG_CTX_PARAM2 );       // Lock the spinlock, yield repeatedly if already acquired
     32bool try_lock  ( spinlock * DEBUG_CTX_PARAM2 );       // Lock the spinlock, return false if already acquired
     33void unlock    ( spinlock * );                        // Unlock the spinlock
    3434
    3535struct semaphore {
     
    4848// Cluster
    4949struct cluster {
    50         __thread_queue_t ready_queue;
    51         spinlock lock;
     50        spinlock ready_queue_lock;                      // Ready queue locks
     51        __thread_queue_t ready_queue;                   // Ready queue for threads
     52        unsigned long long int preemption;              // Preemption rate on this cluster
    5253};
    5354
     
    7677static inline void ^?{}(FinishAction * this) {}
    7778
     79// Processor
     80// Wrapper around kernel threads
    7881struct processor {
    79         struct processorCtx_t * runner;
    80         cluster * cltr;
    81         pthread_t kernel_thread;
     82        // Main state
     83        struct processorCtx_t * runner;                 // Coroutine ctx who does keeps the state of the processor
     84        cluster * cltr;                                 // Cluster from which to get threads
     85        pthread_t kernel_thread;                        // Handle to pthreads
    8286
    83         semaphore terminated;
    84         volatile bool is_terminated;
     87        // Termination
     88        volatile bool do_terminate;                     // Set to true to notify the processor should terminate
     89        semaphore terminated;                           // Termination synchronisation
    8590
    86         struct FinishAction finish;
     91        // RunThread data
     92        struct FinishAction finish;                     // Action to do after a thread is ran
    8793
    88         struct alarm_node_t * preemption_alarm;
    89         unsigned int preemption;
     94        // Preemption data
     95        struct alarm_node_t * preemption_alarm;         // Node which is added in the discrete event simulaiton
     96        bool pending_preemption;                        // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
    9097
    91         bool pending_preemption;
    92 
    93         char * last_enable;
     98#ifdef __CFA_DEBUG__
     99        char * last_enable;                             // Last function to enable preemption on this processor
     100#endif
    94101};
    95102
  • src/libcfa/concurrency/kernel.c

    rb706db1 re60e0dc  
    4747KERNEL_STORAGE(cluster, systemCluster);
    4848KERNEL_STORAGE(system_proc_t, systemProcessor);
     49KERNEL_STORAGE(event_kernel_t, event_kernel);
    4950KERNEL_STORAGE(thread_desc, mainThread);
    5051KERNEL_STORAGE(machine_context_t, mainThreadCtx);
     
    5253cluster * systemCluster;
    5354system_proc_t * systemProcessor;
     55event_kernel_t * event_kernel;
    5456thread_desc * mainThread;
    5557
     
    131133        this->cltr = cltr;
    132134        (&this->terminated){ 0 };
    133         this->is_terminated = false;
     135        this->do_terminate = false;
    134136        this->preemption_alarm = NULL;
    135         this->preemption = default_preemption();
    136137        this->pending_preemption = false;
    137138
     
    142143        this->cltr = cltr;
    143144        (&this->terminated){ 0 };
    144         this->is_terminated = false;
     145        this->do_terminate = false;
    145146        this->preemption_alarm = NULL;
    146         this->preemption = default_preemption();
    147147        this->pending_preemption = false;
    148148        this->kernel_thread = pthread_self();
     
    156156
    157157void ?{}(system_proc_t * this, cluster * cltr, processorCtx_t * runner) {
     158        (&this->proc){ cltr, runner };
     159}
     160
     161void ?{}(event_kernel_t * this) {
    158162        (&this->alarms){};
    159         (&this->alarm_lock){};
    160         this->pending_alarm = false;
    161 
    162         (&this->proc){ cltr, runner };
     163        (&this->lock){};
    163164
    164165        verify( validate( &this->alarms ) );
     
    166167
    167168void ^?{}(processor * this) {
    168         if( ! this->is_terminated ) {
     169        if( ! this->do_terminate ) {
    169170                LIB_DEBUG_PRINT_SAFE("Kernel : core %p signaling termination\n", this);
    170                 this->is_terminated = true;
     171                this->do_terminate = true;
    171172                P( &this->terminated );
    172173                pthread_join( this->kernel_thread, NULL );
     
    176177void ?{}(cluster * this) {
    177178        ( &this->ready_queue ){};
    178         ( &this->lock ){};
     179        ( &this->ready_queue_lock ){};
     180
     181        this->preemption = default_preemption();
    179182}
    180183
     
    199202
    200203                thread_desc * readyThread = NULL;
    201                 for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ )
     204                for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ )
    202205                {
    203206                        readyThread = nextThread( this->cltr );
     
    343346        verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
    344347
    345         lock( &systemProcessor->proc.cltr->lock DEBUG_CTX2 );
     348        lock( &systemProcessor->proc.cltr->ready_queue_lock DEBUG_CTX2 );
    346349        append( &systemProcessor->proc.cltr->ready_queue, thrd );
    347         unlock( &systemProcessor->proc.cltr->lock );
     350        unlock( &systemProcessor->proc.cltr->ready_queue_lock );
    348351
    349352        verify( disable_preempt_count > 0 );
     
    352355thread_desc * nextThread(cluster * this) {
    353356        verify( disable_preempt_count > 0 );
    354         lock( &this->lock DEBUG_CTX2 );
     357        lock( &this->ready_queue_lock DEBUG_CTX2 );
    355358        thread_desc * head = pop_head( &this->ready_queue );
    356         unlock( &this->lock );
     359        unlock( &this->ready_queue_lock );
    357360        verify( disable_preempt_count > 0 );
    358361        return head;
     
    470473        systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtxStorage };
    471474
     475        // Initialize the event kernel
     476        event_kernel = (event_kernel_t *)&event_kernelStorage;
     477        event_kernel{};
     478
    472479        // Add the main thread to the ready queue
    473480        // once resume is called on systemProcessor->runner the mainThread needs to be scheduled like any normal thread
     
    504511        // When its coroutine terminates, it return control to the mainThread
    505512        // which is currently here
    506         systemProcessor->proc.is_terminated = true;
     513        systemProcessor->proc.do_terminate = true;
    507514        suspend();
    508515
  • src/libcfa/concurrency/kernel_private.h

    rb706db1 re60e0dc  
    4545thread_desc * nextThread(cluster * this);
    4646
     47//Block current thread and release/wake-up the following resources
    4748void BlockInternal(void);
    4849void BlockInternal(spinlock * lock);
     
    6768struct system_proc_t {
    6869        processor proc;
     70};
    6971
     72struct event_kernel_t {
    7073        alarm_list_t alarms;
    71         spinlock alarm_lock;
    72 
    73         bool pending_alarm;
     74        spinlock lock;
    7475};
    7576
    7677extern cluster * systemCluster;
    7778extern system_proc_t * systemProcessor;
     79extern event_kernel_t * event_kernel;
     80
    7881extern volatile thread_local processor * this_processor;
    7982extern volatile thread_local coroutine_desc * this_coroutine;
  • src/libcfa/concurrency/preemption.c

    rb706db1 re60e0dc  
    6666
    6767void tick_preemption() {
    68         alarm_list_t * alarms = &systemProcessor->alarms;
     68        alarm_list_t * alarms = &event_kernel->alarms;
    6969        __cfa_time_t currtime = __kernel_get_time();
    7070
     
    189189}
    190190
    191 static inline void defer_alarm() {
    192         systemProcessor->pending_alarm = true;
    193 }
    194 
    195191static void preempt( processor * this ) {
    196192        pthread_kill( this->kernel_thread, SIGUSR1 );
     
    236232        this->proc = proc;
    237233        this->proc->preemption_alarm = &this->alarm;
    238         update_preemption( this->proc, this->proc->preemption );
     234        update_preemption( this->proc, this->proc->cltr->preemption );
    239235}
    240236
     
    283279                case SI_KERNEL:
    284280                        LIB_DEBUG_PRINT_SAFE("Kernel : Preemption thread tick\n");
    285                         lock( &systemProcessor->alarm_lock DEBUG_CTX2 );
     281                        lock( &event_kernel->lock DEBUG_CTX2 );
    286282                        tick_preemption();
    287                         unlock( &systemProcessor->alarm_lock );
     283                        unlock( &event_kernel->lock );
    288284                        break;
    289285                case SI_QUEUE:
Note: See TracChangeset for help on using the changeset viewer.