Changeset 4e83bb7


Ignore:
Timestamp:
Jun 23, 2022, 1:41:29 PM (3 months ago)
Author:
caparsons <caparson@…>
Branches:
master, pthread-emulation, qualifiedEnum
Children:
d28524a
Parents:
1dbbef6
Message:

fixed timeout cond var bug and updated pthread_locks test with timeout lock test

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/locks.cfa

    r1dbbef6 r4e83bb7  
    237237                // This pthread_cond_var member is called from the kernel, and therefore, cannot block, but it can spin.
    238238                lock( cond->lock __cfaabi_dbg_ctx2 );
    239 
    240239                // this check is necessary to avoid a race condition since this timeout handler
    241240                //      may still be called after a thread has been removed from the queue but
     
    347346                size_t recursion_count = queue_and_get_recursion(this, &info);
    348347                alarm_node_wrap(L) node_wrap = { t, 0`s, callback, &this, &info };
     348                unlock( lock );
     349
     350                // registers alarm outside cond lock to avoid deadlock
    349351                register_self( &node_wrap.alarm_node );
    350                 unlock( lock );
    351352
    352353                // blocks here
     
    437438                if ( ret ) {
    438439                        info_thread(L) & popped = try_pop_front( blocked_threads );
     440                        popped.signalled = true;
    439441                        on_notify(*popped.lock, popped.t);
    440442                }
     
    448450                while( ! blocked_threads`isEmpty ) {
    449451                        info_thread(L) & popped = try_pop_front( blocked_threads );
     452                        popped.signalled = true;
    450453                        on_notify(*popped.lock, popped.t);
    451454                }
     
    469472                size_t recursion_count = queue_and_get_recursion(this, &info);
    470473                pthread_alarm_node_wrap(L) node_wrap = { t, 0`s, callback, &this, &info };
     474                unlock( lock );
     475
     476                // registers alarm outside cond lock to avoid deadlock
    471477                register_self( &node_wrap.alarm_node );
    472                 unlock( lock );
    473478
    474479                // blocks here
     
    500505                return i.signalled;
    501506
     507        Duration getDuration(timespec t) {
     508                timespec currTime;
     509                clock_gettime(CLOCK_REALTIME, &currTime);
     510                Duration waitUntil = { t };
     511                Duration currDur = { currTime };
     512                if ( currDur >= waitUntil ) return currDur - waitUntil;
     513                Duration zero = { 0 };
     514                return zero;
     515        }
     516
    502517        bool wait( pthread_cond_var(L) & this, L & l, timespec t ) {
    503                 Duration d = { t };
    504                 WAIT_TIME( 0, &l , d )
     518                PTHREAD_WAIT_TIME( 0, &l , getDuration( t ) )
    505519        }
    506520       
    507521        bool wait( pthread_cond_var(L) & this, L & l, uintptr_t info, timespec t  ) {
    508                 Duration d = { t };
    509                 WAIT_TIME( info, &l , d )
     522                PTHREAD_WAIT_TIME( info, &l , getDuration( t ) )
    510523        }
    511524}
  • tests/unified_locking/.expect/pthread_locks.txt

    r1dbbef6 r4e83bb7  
    55Start Test 3: lock and condition variable multiple acquire and wait/notify
    66Done Test 3
     7Start Test 4: lock and condition variable single timed wait/notify
     8Done Test 4
  • tests/unified_locking/pthread_locks.cfa

    r1dbbef6 r4e83bb7  
    33#include <stdlib.hfa>
    44#include <thread.hfa>
     5#include <time.h>
     6#include <stdlib.hfa>
    57
    6 const unsigned int num_times = 50000;
     8const unsigned int num_times = 50;
    79
    810simple_owner_lock l;
    911pthread_cond_var( simple_owner_lock ) c;
     12
     13owner_lock l2;
     14condition_variable( owner_lock ) c2;
    1015
    1116volatile int counter = 0;
     
    5964}
    6065
     66thread Wait_Time_Signal_1 {};
     67
     68void main( Wait_Time_Signal_1 & this ) {
     69        for (unsigned int i = 0; i < num_times; i++) {
     70                lock(l);
     71                if(empty(c) || random(10) >= 9 ) {
     72                        timespec t;
     73                        clock_gettime(CLOCK_REALTIME, &t);
     74                        timespec waitTime{0,1};
     75                        bool woken = wait(c,l, t + waitTime);
     76                }else{
     77                        notify_one(c);
     78                }
     79                unlock(l);
     80        }
     81}
     82
    6183int main() {
    62         processor p[3];
     84        processor p[1];
    6385        printf("Start Test 1: lock and condition variable single wait/notify\n");
    6486        {
     
    78100        }
    79101        printf("Done Test 3\n");
     102
     103        printf("Start Test 4: lock and condition variable single timed wait/notify\n");
     104        {
     105                Wait_Time_Signal_1 t1[2];
     106        }
     107        printf("Done Test 4\n");
    80108}
Note: See TracChangeset for help on using the changeset viewer.