Ignore:
Timestamp:
Feb 28, 2017, 12:40:47 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
2781e65, fd061ed3
Parents:
4868be4
Message:
  • renamed monitor to monitor_t since the type should not be exposed to users
  • added support for recursion in monitors
Location:
src/libcfa/concurrency
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/concurrency/invoke.h

    r4868be4 rcc7f4b1  
    6060
    6161      struct coStack_t {
    62             unsigned int size;          // size of stack
    63             void *storage;                      // pointer to stack
    64             void *limit;                        // stack grows towards stack limit
    65             void *base;                         // base of stack
    66             void *context;                      // address of cfa_context_t
    67             void *top;                          // address of top of storage
     62            unsigned int size;                // size of stack
     63            void *storage;                            // pointer to stack
     64            void *limit;                              // stack grows towards stack limit
     65            void *base;                               // base of stack
     66            void *context;                            // address of cfa_context_t
     67            void *top;                                // address of top of storage
    6868            bool userStack;     
    6969      };
     
    7373      struct coroutine {
    7474            struct coStack_t stack;
    75             const char *name;                   // textual name for coroutine/task, initialized by uC++ generated code
    76             int errno_;                         // copy of global UNIX variable errno
    77             enum coroutine_state state; // current execution status for coroutine
    78             struct coroutine *starter;  // first coroutine to resume this one
    79             struct coroutine *last;             // last coroutine to resume this one
     75            const char *name;                         // textual name for coroutine/task, initialized by uC++ generated code
     76            int errno_;                               // copy of global UNIX variable errno
     77            enum coroutine_state state;       // current execution status for coroutine
     78            struct coroutine *starter;        // first coroutine to resume this one
     79            struct coroutine *last;                   // last coroutine to resume this one
    8080      };
    8181
    8282      struct thread {
    83             struct coroutine c;           // coroutine body used to store context
    84             struct signal_once terminated;// indicate if execuation state is not halted
    85             struct thread * next;         // instrusive link field for threads
     83            struct coroutine c;                 // coroutine body used to store context
     84            struct signal_once terminated;      // indicate if execuation state is not halted
     85            struct thread * next;               // instrusive link field for threads
    8686      };
    8787
  • src/libcfa/concurrency/monitor

    r4868be4 rcc7f4b1  
    2121#include "invoke.h"
    2222
    23 struct monitor {
     23struct __monitor_t {
    2424        spinlock lock;
    25         thread * holder;
     25        thread * owner;
    2626        simple_thread_list entry_queue;
     27        unsigned int recursion;
    2728};
    2829
    29 void enter(monitor *);
    30 void leave(monitor *);
     30static inline void ?{}(__monitor_t * this) {
     31        this->owner = 0;
     32        this->recursion = 0;
     33}
    3134
    32 struct monitor_guard {
    33         monitor * m;
     35void enter(__monitor_t *);
     36void leave(__monitor_t *);
     37
     38struct monitor_guard_t {
     39        __monitor_t * m;
    3440};
    3541
    36 static inline void ?{}( monitor_guard * this, monitor * m ) {
     42static inline void ?{}( monitor_guard_t * this, __monitor_t * m ) {
    3743        this->m = m;
    3844        enter( this->m );
    3945}
    4046
    41 static inline void ^?{}( monitor_guard * this ) {
     47static inline void ^?{}( monitor_guard_t * this ) {
    4248        leave( this->m );
    4349}
  • src/libcfa/concurrency/monitor.c

    r4868be4 rcc7f4b1  
    66// file "LICENCE" distributed with Cforall.
    77//
    8 // monitor.c --
     8// __monitor_t.c --
    99//
    1010// Author           : Thierry Delisle
     
    1919#include "kernel_private.h"
    2020
    21 void enter(monitor * this) {
     21void enter(__monitor_t * this) {
    2222        lock( &this->lock );
    2323        thread * thrd = this_thread();
    2424
    25         if( this->holder ) {
     25        if( !this->owner ) {
     26                //No one has the monitor, just take it
     27                this->owner = thrd;
     28                this->recursion = 1;
     29        }
     30        else if( this->owner == thrd) {
     31                //We already have the monitor, just not how many times we took it
     32                assert( this->recursion > 0 );
     33                this->recursion += 1;
     34        }
     35        else {
     36                //Some one else has the monitor, wait in line for it
    2637                append( &this->entry_queue, thrd );
    2738                ScheduleInternal( &this->lock );
    28                 return;
    29         }
    30         else {
    31                 this->holder = thrd;
     39
     40                //ScheduleInternal will unlock spinlock, no need to unlock ourselves
     41                return;
    3242        }
    3343
     
    3545}
    3646
    37 void leave(monitor * this) {
     47void leave(__monitor_t * this) {
    3848        lock( &this->lock );
    3949
    4050        thread * thrd = this_thread();
    41         assert( thrd == this->holder );
     51        assert( thrd == this->owner );
    4252
    43         this->holder = pop_head( &this->entry_queue );
     53        //Leaving a recursion level, decrement the counter
     54        this->recursion -= 1;
     55
     56        //If we left the last level of recursion it means we are changing who owns the monitor
     57        thread * new_owner = 0;
     58        if( this->recursion == 0) {
     59                //Get the next thread in the list
     60                new_owner = this->owner = pop_head( &this->entry_queue );
     61
     62                //We are passing the monitor to someone else, which means recursion level is not 0
     63                this->recursion = new_owner ? 1 : 0;
     64        }       
    4465
    4566        unlock( &this->lock );
    4667
    47         if( this->holder ) ScheduleThread( this->holder );
     68        //If we have a new owner, we need to wake-up the thread
     69        if( new_owner ) {
     70                ScheduleThread( new_owner );
     71        }
    4872}
Note: See TracChangeset for help on using the changeset viewer.