From 6b445b0f9d8007a33c55adbcf55cec97cd644677 Mon Sep 17 00:00:00 2001 From: kpl Date: Fri, 23 Nov 2012 21:16:02 -0800 Subject: [PATCH] cmThread:Added cmThPtrCAS() --- cmThread.c | 24 ++++++++++++++++++------ cmThread.h | 3 +++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/cmThread.c b/cmThread.c index 3e7c1a7..978b47e 100644 --- a/cmThread.c +++ b/cmThread.c @@ -12,7 +12,6 @@ #include // usleep //#include - cmThreadH_t cmThreadNullHandle = {NULL}; enum @@ -102,10 +101,12 @@ cmThThread_t* _cmThThreadFromHandle( cmThreadH_t h ) cmThRC_t _cmThWaitForState( cmThThread_t* t, unsigned stateId ) { unsigned waitTimeMicroSecs = 0; + while( t->state != stateId && waitTimeMicroSecs < t->waitMicroSecs ) { - usleep( t->waitMicroSecs ); - waitTimeMicroSecs += t->waitMicroSecs; + //usleep( t->waitMicroSecs ); + usleep( 15000 ); + waitTimeMicroSecs += 15000; //t->waitMicroSecs; } return t->state==stateId ? kOkThRC : kTimeOutThRC; @@ -1261,6 +1262,16 @@ bool cmThUIntCAS( unsigned* addr, unsigned old, unsigned new ) bool cmThFloatCAS( float* addr, float old, float new ) { return __sync_bool_compare_and_swap((unsigned*)addr, *(unsigned*)(&old),*(unsigned*)(&new)); } +bool cmThPtrCAS( void* addr, void* old, void* neww ) +{ +#ifdef OS_64 + return __sync_bool_compare_and_swap((long long*)addr, (long long)old, (long long)neww); +#else + return __sync_bool_compare_and_swap((int*)addr,(int)old,(int)neww); +#endif +} + + void cmThIntIncr( int* addr, int incr ) { @@ -1432,7 +1443,8 @@ void* cmTsMp1cCbArg( cmTsMp1cH_t h ) return p->cbArg; } -#define CAS(addr,old,new) __sync_bool_compare_and_swap(addr,old,new) +//#define CAS(addr,old,new) __sync_bool_compare_and_swap(addr,old,new) +//#define CAS(addr,old,neww) cmThPtrCAS(addr,old,neww) cmThRC_t cmTsMp1cEnqueueSegMsg( cmTsMp1cH_t h, const void* msgPtrArray[], unsigned msgByteCntArray[], unsigned arrayCnt ) { @@ -1512,7 +1524,7 @@ cmThRC_t cmTsMp1cEnqueueSegMsg( cmTsMp1cH_t h, const void* msgPtrArray[], unsi { old_hp = p->ilp; new_hp = hp; - }while(!CAS(&p->ilp,old_hp,new_hp)); + }while(!cmThPtrCAS(&p->ilp,old_hp,new_hp)); // link the prev recd to this recd if( old_hp != NULL ) @@ -1527,7 +1539,7 @@ cmThRC_t cmTsMp1cEnqueueSegMsg( cmTsMp1cH_t h, const void* msgPtrArray[], unsi if( old_hp != NULL ) break; - }while(!CAS(&p->olp,old_hp,new_hp)); + }while(!cmThPtrCAS(&p->olp,old_hp,new_hp)); return rc; } diff --git a/cmThread.h b/cmThread.h index 49fcaba..b43ba35 100644 --- a/cmThread.h +++ b/cmThread.h @@ -226,6 +226,9 @@ extern "C" { bool cmThUIntCAS( unsigned* addr, unsigned old, unsigned neww ); bool cmThFloatCAS( float* addr, float old, float neww ); + // Note: voidPtrPtr is must really be a pointer to a pointer. + bool cmThPtrCAS( void* voidPtrPtr, void* old, void* neww ); + // Thread safe increment and decrement implemented in terms of // cmThXXXCAS(). void cmThIntIncr( int* addr, int incr );