Subj : Re: Optimization to Jeffrey Richter's COptex (Windows)? To : comp.programming.threads From : SenderX Date : Thu Jan 27 2005 01:59 pm > You've omitted timedlock(). Why so? ;-) > Here is what it would look like: int optex_timedlock( optex_t *_this, DWORD timeout ) { register ac_uatomic_t cmp, xchg, old; ac_thread_t thisthread = ac_thread_self(); if ( ! ac_thread_equal( _this->owner, thisthread ) ) { ++_this->recurse; return; } old = _this->state; for ( ;; ) { if ( ! ( old & 0x80000000 ) ) { xchg = old | 0x80000000; } else { xchg = old + 1; } cmp = old; old = ac_atomic_cas_acq_dep( &_this->state, old, xchg ); if ( cmp == old ) { if ( ! ( old & 0x80000000 ) ) { break; } if ( WaitForSingleObject ( _this->waitset, timeout ) != WAIT_OBJECT_0 ) { ac_atomic_dec_rel( &_this->state ); return ETIMEDOUT; } old = _this->state; } }; _this->owner = thisthread; return 0; } void optex_unlock( optex_t *_this ) { register ac_uatomic_t cmp, xchg, old; if ( _this->recurse ) { --_this->recurse; return; } _this->owner = AC_THREAD_NULL; old = _this->state; do { xchg = old & 0x0FFFFFFF; if ( ((ac_atomic_t)xchg) > 0 ) { --xchg; } cmp = old; old = ac_atomic_cas_rel( &_this->state, old, xchg ); } while ( old != cmp ); if ( ((ac_atomic_t)(old & 0x0FFFFFFF)) > 0 ) { ReleaseSemaphore( _this->waitset, 1, 0 ); } } > Well, timedlock() aside for a moment, with your waiters counting you > increase the overhead of contention. More waiters in a batch results > in more counting/read-modify-write. Are you talking about counting in the CAS loop itself, xchg = old + 1, type stuff? > Get rid of waiters counting. With > waiters bit maintained by you instead of counting, you'll have > needless signaling from time to time (at the end of contention > interval) and it may cause "spurious" wake at the beginning of next > contention interval but that overhead is "constant" (it doesn't > increase with increased contention). I see... .