Changeset 4cedd9f for src/libcfa


Ignore:
Timestamp:
Nov 2, 2017, 2:15:19 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:
8fc45b7
Parents:
e1e8408
Message:

Updated public concurrency API to use references

Location:
src/libcfa/concurrency
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/concurrency/kernel

    re1e8408 r4cedd9f  
    3939void  ?{}(semaphore & this, int count = 1);
    4040void ^?{}(semaphore & this);
    41 void P(semaphore * this);
    42 void V(semaphore * this);
     41void   P (semaphore & this);
     42void   V (semaphore & this);
    4343
    4444
     
    5151};
    5252
    53 void ?{}(cluster & this);
     53void ?{} (cluster & this);
    5454void ^?{}(cluster & this);
    5555
  • src/libcfa/concurrency/kernel.c

    re1e8408 r4cedd9f  
    158158                LIB_DEBUG_PRINT_SAFE("Kernel : core %p signaling termination\n", &this);
    159159                this.do_terminate = true;
    160                 P( &this.terminated );
     160                P( this.terminated );
    161161                pthread_join( this.kernel_thread, NULL );
    162162        }
     
    216216        }
    217217
    218         V( &this->terminated );
     218        V( this->terminated );
    219219
    220220        LIB_DEBUG_PRINT_SAFE("Kernel : core %p terminated\n", this);
     
    618618void ^?{}(semaphore & this) {}
    619619
    620 void P(semaphore * this) {
    621         lock( &this->lock DEBUG_CTX2 );
    622         this->count -= 1;
    623         if ( this->count < 0 ) {
     620void P(semaphore & this) {
     621        lock( &this.lock DEBUG_CTX2 );
     622        this.count -= 1;
     623        if ( this.count < 0 ) {
    624624                // queue current task
    625                 append( &this->waiting, (thread_desc *)this_thread );
     625                append( &this.waiting, (thread_desc *)this_thread );
    626626
    627627                // atomically release spin lock and block
    628                 BlockInternal( &this->lock );
     628                BlockInternal( &this.lock );
    629629        }
    630630        else {
    631             unlock( &this->lock );
    632         }
    633 }
    634 
    635 void V(semaphore * this) {
     631            unlock( &this.lock );
     632        }
     633}
     634
     635void V(semaphore & this) {
    636636        thread_desc * thrd = NULL;
    637         lock( &this->lock DEBUG_CTX2 );
    638         this->count += 1;
    639         if ( this->count <= 0 ) {
     637        lock( &this.lock DEBUG_CTX2 );
     638        this.count += 1;
     639        if ( this.count <= 0 ) {
    640640                // remove task at head of waiting list
    641                 thrd = pop_head( &this->waiting );
    642         }
    643 
    644         unlock( &this->lock );
     641                thrd = pop_head( &this.waiting );
     642        }
     643
     644        unlock( &this.lock );
    645645
    646646        // make new owner
  • src/libcfa/concurrency/monitor

    re1e8408 r4cedd9f  
    116116}
    117117
    118 void wait( condition * this, uintptr_t user_info = 0 );
    119 bool signal( condition * this );
    120 bool signal_block( condition * this );
    121 static inline bool is_empty( condition * this ) { return !this->blocked.head; }
    122 uintptr_t front( condition * this );
     118void wait( condition & this, uintptr_t user_info = 0 );
     119bool signal( condition & this );
     120bool signal_block( condition & this );
     121static inline bool is_empty( condition & this ) { return !this.blocked.head; }
     122uintptr_t front( condition & this );
    123123
    124124//-----------------------------------------------------------------------------
  • src/libcfa/concurrency/monitor.c

    re1e8408 r4cedd9f  
    4545
    4646static inline thread_desc *        check_condition   ( __condition_criterion_t * );
    47 static inline void                 brand_condition   ( condition * );
     47static inline void                 brand_condition   ( condition & );
    4848static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t &, monitor_desc ** monitors, int count );
    4949
     
    6969        unsigned short count = cnt;                               /* Save the count to a local variable                                                  */ \
    7070        unsigned int recursions[ count ];                         /* Save the current recursion levels to restore them later                             */ \
    71         __waitfor_mask_t masks[ count ];                          /* Save the current waitfor masks to restore them later                                */ \
     71        __waitfor_mask_t masks [ count ];                         /* Save the current waitfor masks to restore them later                                */ \
    7272        spinlock *   locks     [ count ];                         /* We need to pass-in an array of locks to BlockInternal                               */ \
    7373
     
    387387//-----------------------------------------------------------------------------
    388388// Internal scheduling
    389 void wait( condition * this, uintptr_t user_info = 0 ) {
     389void wait( condition & this, uintptr_t user_info = 0 ) {
    390390        brand_condition( this );
    391391
    392392        // Check that everything is as expected
    393         assertf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
    394         verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
    395         verifyf( this->monitor_count < 32u, "Excessive monitor count (%i)", this->monitor_count );
     393        assertf( this.monitors != NULL, "Waiting with no monitors (%p)", this.monitors );
     394        verifyf( this.monitor_count != 0, "Waiting with 0 monitors (%i)", this.monitor_count );
     395        verifyf( this.monitor_count < 32u, "Excessive monitor count (%i)", this.monitor_count );
    396396
    397397        // Create storage for monitor context
    398         monitor_ctx( this->monitors, this->monitor_count );
     398        monitor_ctx( this.monitors, this.monitor_count );
    399399
    400400        // Create the node specific to this wait operation
     
    403403        // Append the current wait operation to the ones already queued on the condition
    404404        // We don't need locks for that since conditions must always be waited on inside monitor mutual exclusion
    405         append( &this->blocked, &waiter );
     405        append( &this.blocked, &waiter );
    406406
    407407        // Lock all monitors (aggregates the locks as well)
     
    429429}
    430430
    431 bool signal( condition * this ) {
     431bool signal( condition & this ) {
    432432        if( is_empty( this ) ) { return false; }
    433433
    434434        //Check that everything is as expected
    435         verify( this->monitors );
    436         verify( this->monitor_count != 0 );
     435        verify( this.monitors );
     436        verify( this.monitor_count != 0 );
    437437
    438438        //Some more checking in debug
    439439        LIB_DEBUG_DO(
    440440                thread_desc * this_thrd = this_thread;
    441                 if ( this->monitor_count != this_thrd->monitors.size ) {
    442                         abortf( "Signal on condition %p made with different number of monitor(s), expected %i got %i", this, this->monitor_count, this_thrd->monitors.size );
    443                 }
    444 
    445                 for(int i = 0; i < this->monitor_count; i++) {
    446                         if ( this->monitors[i] != this_thrd->monitors.list[i] ) {
    447                                 abortf( "Signal on condition %p made with different monitor, expected %p got %i", this, this->monitors[i], this_thrd->monitors.list[i] );
     441                if ( this.monitor_count != this_thrd->monitors.size ) {
     442                        abortf( "Signal on condition %p made with different number of monitor(s), expected %i got %i", &this, this.monitor_count, this_thrd->monitors.size );
     443                }
     444
     445                for(int i = 0; i < this.monitor_count; i++) {
     446                        if ( this.monitors[i] != this_thrd->monitors.list[i] ) {
     447                                abortf( "Signal on condition %p made with different monitor, expected %p got %i", &this, this.monitors[i], this_thrd->monitors.list[i] );
    448448                        }
    449449                }
    450450        );
    451451
    452         unsigned short count = this->monitor_count;
     452        unsigned short count = this.monitor_count;
    453453
    454454        // Lock all monitors
    455         lock_all( this->monitors, NULL, count );
     455        lock_all( this.monitors, NULL, count );
    456456
    457457        //Pop the head of the waiting queue
    458         __condition_node_t * node = pop_head( &this->blocked );
     458        __condition_node_t * node = pop_head( &this.blocked );
    459459
    460460        //Add the thread to the proper AS stack
     
    466466
    467467        //Release
    468         unlock_all( this->monitors, count );
     468        unlock_all( this.monitors, count );
    469469
    470470        return true;
    471471}
    472472
    473 bool signal_block( condition * this ) {
    474         if( !this->blocked.head ) { return false; }
     473bool signal_block( condition & this ) {
     474        if( !this.blocked.head ) { return false; }
    475475
    476476        //Check that everything is as expected
    477         verifyf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
    478         verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
     477        verifyf( this.monitors != NULL, "Waiting with no monitors (%p)", this.monitors );
     478        verifyf( this.monitor_count != 0, "Waiting with 0 monitors (%i)", this.monitor_count );
    479479
    480480        // Create storage for monitor context
    481         monitor_ctx( this->monitors, this->monitor_count );
     481        monitor_ctx( this.monitors, this.monitor_count );
    482482
    483483        // Lock all monitors (aggregates the locks them as well)
     
    491491
    492492        //Find the thread to run
    493         thread_desc * signallee = pop_head( &this->blocked )->waiting_thread;
     493        thread_desc * signallee = pop_head( &this.blocked )->waiting_thread;
    494494        set_owner( monitors, count, signallee );
    495495
    496         LIB_DEBUG_PRINT_BUFFER_DECL( "Kernel : signal_block condition %p (s: %p)\n", this, signallee );
     496        LIB_DEBUG_PRINT_BUFFER_DECL( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee );
    497497
    498498        //Everything is ready to go to sleep
     
    512512
    513513// Access the user_info of the thread waiting at the front of the queue
    514 uintptr_t front( condition * this ) {
     514uintptr_t front( condition & this ) {
    515515        verifyf( !is_empty(this),
    516516                "Attempt to access user data on an empty condition.\n"
    517517                "Possible cause is not checking if the condition is empty before reading stored data."
    518518        );
    519         return this->blocked.head->user_info;
     519        return this.blocked.head->user_info;
    520520}
    521521
     
    811811}
    812812
    813 static inline void brand_condition( condition * this ) {
     813static inline void brand_condition( condition & this ) {
    814814        thread_desc * thrd = this_thread;
    815         if( !this->monitors ) {
     815        if( !this.monitors ) {
    816816                // LIB_DEBUG_PRINT_SAFE("Branding\n");
    817817                assertf( thrd->monitors.list != NULL, "No current monitor to brand condition %p", thrd->monitors.list );
    818                 this->monitor_count = thrd->monitors.size;
    819 
    820                 this->monitors = malloc( this->monitor_count * sizeof( *this->monitors ) );
    821                 for( int i = 0; i < this->monitor_count; i++ ) {
    822                         this->monitors[i] = thrd->monitors.list[i];
     818                this.monitor_count = thrd->monitors.size;
     819
     820                this.monitors = malloc( this.monitor_count * sizeof( *this.monitors ) );
     821                for( int i = 0; i < this.monitor_count; i++ ) {
     822                        this.monitors[i] = thrd->monitors.list[i];
    823823                }
    824824        }
Note: See TracChangeset for help on using the changeset viewer.