cmThread:Added cmThPtrCAS()

This commit is contained in:
kpl 2012-11-23 21:16:02 -08:00
parent 186cabcf32
commit 6b445b0f9d
2 changed files with 21 additions and 6 deletions

View File

@ -12,7 +12,6 @@
#include <unistd.h> // usleep #include <unistd.h> // usleep
//#include <atomic_ops.h> //#include <atomic_ops.h>
cmThreadH_t cmThreadNullHandle = {NULL}; cmThreadH_t cmThreadNullHandle = {NULL};
enum enum
@ -102,10 +101,12 @@ cmThThread_t* _cmThThreadFromHandle( cmThreadH_t h )
cmThRC_t _cmThWaitForState( cmThThread_t* t, unsigned stateId ) cmThRC_t _cmThWaitForState( cmThThread_t* t, unsigned stateId )
{ {
unsigned waitTimeMicroSecs = 0; unsigned waitTimeMicroSecs = 0;
while( t->state != stateId && waitTimeMicroSecs < t->waitMicroSecs ) while( t->state != stateId && waitTimeMicroSecs < t->waitMicroSecs )
{ {
usleep( t->waitMicroSecs ); //usleep( t->waitMicroSecs );
waitTimeMicroSecs += t->waitMicroSecs; usleep( 15000 );
waitTimeMicroSecs += 15000; //t->waitMicroSecs;
} }
return t->state==stateId ? kOkThRC : kTimeOutThRC; 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 ) bool cmThFloatCAS( float* addr, float old, float new )
{ return __sync_bool_compare_and_swap((unsigned*)addr, *(unsigned*)(&old),*(unsigned*)(&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 ) void cmThIntIncr( int* addr, int incr )
{ {
@ -1432,7 +1443,8 @@ void* cmTsMp1cCbArg( cmTsMp1cH_t h )
return p->cbArg; 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 ) 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; old_hp = p->ilp;
new_hp = hp; 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 // link the prev recd to this recd
if( old_hp != NULL ) if( old_hp != NULL )
@ -1527,7 +1539,7 @@ cmThRC_t cmTsMp1cEnqueueSegMsg( cmTsMp1cH_t h, const void* msgPtrArray[], unsi
if( old_hp != NULL ) if( old_hp != NULL )
break; break;
}while(!CAS(&p->olp,old_hp,new_hp)); }while(!cmThPtrCAS(&p->olp,old_hp,new_hp));
return rc; return rc;
} }

View File

@ -226,6 +226,9 @@ extern "C" {
bool cmThUIntCAS( unsigned* addr, unsigned old, unsigned neww ); bool cmThUIntCAS( unsigned* addr, unsigned old, unsigned neww );
bool cmThFloatCAS( float* addr, float old, float 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 // Thread safe increment and decrement implemented in terms of
// cmThXXXCAS(). // cmThXXXCAS().
void cmThIntIncr( int* addr, int incr ); void cmThIntIncr( int* addr, int incr );