static void * run2(void *arg) { char *message = arg; RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_write) == ISC_R_SUCCESS); printf("%s got WRITE lock\n", message); sleep(1); printf("%s giving up WRITE lock\n", message); RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) == ISC_R_SUCCESS); RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_write) == ISC_R_SUCCESS); printf("%s got WRITE lock\n", message); sleep(1); printf("%s giving up WRITE lock\n", message); RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) == ISC_R_SUCCESS); RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_read) == ISC_R_SUCCESS); printf("%s got READ lock\n", message); sleep(1); printf("%s giving up READ lock\n", message); RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_read) == ISC_R_SUCCESS); return (NULL); }
static isc_threadresult_t #ifdef WIN32 WINAPI #endif run2(void *arg) { char *message = arg; RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_write) == ISC_R_SUCCESS); printf("%s got WRITE lock\n", message); sleep(1); printf("%s giving up WRITE lock\n", message); RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) == ISC_R_SUCCESS); RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_write) == ISC_R_SUCCESS); printf("%s got WRITE lock\n", message); sleep(1); printf("%s giving up WRITE lock\n", message); RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_write) == ISC_R_SUCCESS); RUNTIME_CHECK(isc_rwlock_lock(&lock, isc_rwlocktype_read) == ISC_R_SUCCESS); printf("%s got READ lock\n", message); sleep(1); printf("%s giving up READ lock\n", message); RUNTIME_CHECK(isc_rwlock_unlock(&lock, isc_rwlocktype_read) == ISC_R_SUCCESS); return ((isc_threadresult_t)0); }
static void copy_counters(isc_stats_t *stats) { int i; #ifdef ISC_RWLOCK_USEATOMIC /* * We use a "write" lock before "reading" the statistics counters as * an exclusive lock. */ isc_rwlock_lock(&stats->counterlock, isc_rwlocktype_write); #endif #if ISC_STATS_USEMULTIFIELDS for (i = 0; i < stats->ncounters; i++) { stats->copiedcounters[i] = (isc_uint64_t)(stats->counters[i].hi) << 32 | stats->counters[i].lo; } #else UNUSED(i); memcpy(stats->copiedcounters, stats->counters, stats->ncounters * sizeof(isc_stat_t)); #endif #ifdef ISC_RWLOCK_USEATOMIC isc_rwlock_unlock(&stats->counterlock, isc_rwlocktype_write); #endif }
static inline void decrementcounter(isc_stats_t *stats, int counter) { isc_int32_t prev; #ifdef ISC_RWLOCK_USEATOMIC isc_rwlock_lock(&stats->counterlock, isc_rwlocktype_read); #endif #if ISC_STATS_USEMULTIFIELDS prev = isc_atomic_xadd((isc_int32_t *)&stats->counters[counter].lo, -1); if (prev == 0) isc_atomic_xadd((isc_int32_t *)&stats->counters[counter].hi, -1); #elif defined(ISC_PLATFORM_HAVEXADDQ) UNUSED(prev); isc_atomic_xaddq((isc_int64_t *)&stats->counters[counter], -1); #else UNUSED(prev); stats->counters[counter]--; #endif #ifdef ISC_RWLOCK_USEATOMIC isc_rwlock_unlock(&stats->counterlock, isc_rwlocktype_read); #endif }
void isc_stats_set(isc_stats_t *stats, isc_uint64_t val, isc_statscounter_t counter) { REQUIRE(ISC_STATS_VALID(stats)); REQUIRE(counter < stats->ncounters); #ifdef ISC_RWLOCK_USEATOMIC /* * We use a "write" lock before "reading" the statistics counters as * an exclusive lock. */ isc_rwlock_lock(&stats->counterlock, isc_rwlocktype_write); #endif #if ISC_STATS_USEMULTIFIELDS stats->counters[counter].hi = (isc_uint32_t)((val >> 32) & 0xffffffff); stats->counters[counter].lo = (isc_uint32_t)(val & 0xffffffff); #else stats->counters[counter] = val; #endif #ifdef ISC_RWLOCK_USEATOMIC isc_rwlock_unlock(&stats->counterlock, isc_rwlocktype_write); #endif }
static inline void incrementcounter(isc_stats_t *stats, int counter) { isc_int32_t prev; #ifdef ISC_RWLOCK_USEATOMIC /* * We use a "read" lock to prevent other threads from reading the * counter while we "writing" a counter field. The write access itself * is protected by the atomic operation. */ isc_rwlock_lock(&stats->counterlock, isc_rwlocktype_read); #endif #if ISC_STATS_USEMULTIFIELDS prev = isc_atomic_xadd((isc_int32_t *)&stats->counters[counter].lo, 1); /* * If the lower 32-bit field overflows, increment the higher field. * Note that it's *theoretically* possible that the lower field * overlaps again before the higher field is incremented. It doesn't * matter, however, because we don't read the value until * isc_stats_copy() is called where the whole process is protected * by the write (exclusive) lock. */ if (prev == (isc_int32_t)0xffffffff) isc_atomic_xadd((isc_int32_t *)&stats->counters[counter].hi, 1); #elif defined(ISC_PLATFORM_HAVEXADDQ) UNUSED(prev); isc_atomic_xaddq((isc_int64_t *)&stats->counters[counter], 1); #else UNUSED(prev); stats->counters[counter]++; #endif #ifdef ISC_RWLOCK_USEATOMIC isc_rwlock_unlock(&stats->counterlock, isc_rwlocktype_read); #endif }
isc_result_t isc_rwlock_trylock(isc_rwlock_t *rwl, isc_rwlocktype_t type) { return (isc_rwlock_lock(rwl, type)); }