mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-04-18 09:32:34 +00:00
add mutex to windows emulated recursive Read locks
This commit is contained in:
parent
fc578a67f1
commit
2c5e40369f
@ -430,6 +430,7 @@ struct ks_rwl {
|
|||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
SRWLOCK rwlock;
|
SRWLOCK rwlock;
|
||||||
ks_hash_t *read_lock_list;
|
ks_hash_t *read_lock_list;
|
||||||
|
ks_mutex_t *read_lock_mutex;
|
||||||
#else
|
#else
|
||||||
pthread_rwlock_t rwlock;
|
pthread_rwlock_t rwlock;
|
||||||
#endif
|
#endif
|
||||||
@ -479,6 +480,10 @@ KS_DECLARE(ks_status_t) ks_rwl_create(ks_rwl_t **rwlock, ks_pool_t *pool)
|
|||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ks_mutex_create(&check->read_lock_mutex, KS_MUTEX_FLAG_DEFAULT, pool) != KS_STATUS_SUCCESS) {
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
InitializeSRWLock(&check->rwlock);
|
InitializeSRWLock(&check->rwlock);
|
||||||
#else
|
#else
|
||||||
if ((pthread_rwlock_init(&check->rwlock, NULL))) {
|
if ((pthread_rwlock_init(&check->rwlock, NULL))) {
|
||||||
@ -496,13 +501,18 @@ KS_DECLARE(ks_status_t) ks_rwl_create(ks_rwl_t **rwlock, ks_pool_t *pool)
|
|||||||
KS_DECLARE(ks_status_t) ks_rwl_read_lock(ks_rwl_t *rwlock)
|
KS_DECLARE(ks_status_t) ks_rwl_read_lock(ks_rwl_t *rwlock)
|
||||||
{
|
{
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
|
||||||
|
ks_mutex_lock(rwlock->read_lock_mutex);
|
||||||
|
|
||||||
int count = (int)(intptr_t)ks_hash_remove(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self());
|
int count = (int)(intptr_t)ks_hash_remove(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self());
|
||||||
|
|
||||||
if (count) {
|
if (count) {
|
||||||
ks_hash_insert(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self(), (void *)(intptr_t)++count);
|
ks_hash_insert(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self(), (void *)(intptr_t)++count);
|
||||||
|
ks_mutex_unlock(rwlock->read_lock_mutex);
|
||||||
return KS_STATUS_SUCCESS;
|
return KS_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ks_mutex_unlock(rwlock->read_lock_mutex);
|
||||||
|
|
||||||
AcquireSRWLockShared(&rwlock->rwlock);
|
AcquireSRWLockShared(&rwlock->rwlock);
|
||||||
#else
|
#else
|
||||||
@ -511,6 +521,7 @@ KS_DECLARE(ks_status_t) ks_rwl_read_lock(ks_rwl_t *rwlock)
|
|||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
ks_hash_insert(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self(), (void *)(intptr_t)(int)1);
|
ks_hash_insert(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self(), (void *)(intptr_t)(int)1);
|
||||||
|
ks_mutex_unlock(rwlock->read_lock_mutex);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return KS_STATUS_SUCCESS;
|
return KS_STATUS_SUCCESS;
|
||||||
@ -539,14 +550,18 @@ KS_DECLARE(ks_status_t) ks_rwl_write_lock(ks_rwl_t *rwlock)
|
|||||||
KS_DECLARE(ks_status_t) ks_rwl_try_read_lock(ks_rwl_t *rwlock)
|
KS_DECLARE(ks_status_t) ks_rwl_try_read_lock(ks_rwl_t *rwlock)
|
||||||
{
|
{
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
ks_mutex_lock(rwlock->read_lock_mutex);
|
||||||
|
|
||||||
int count = (int)(intptr_t)ks_hash_remove(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self());
|
int count = (int)(intptr_t)ks_hash_remove(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self());
|
||||||
|
|
||||||
if (count) {
|
if (count) {
|
||||||
ks_hash_insert(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self(), (void *)(intptr_t)++count);
|
ks_hash_insert(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self(), (void *)(intptr_t)++count);
|
||||||
|
ks_mutex_unlock(rwlock->read_lock_mutex);
|
||||||
return KS_STATUS_SUCCESS;
|
return KS_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!TryAcquireSRWLockShared(&rwlock->rwlock)) {
|
if (!TryAcquireSRWLockShared(&rwlock->rwlock)) {
|
||||||
|
ks_mutex_unlock(rwlock->read_lock_mutex);
|
||||||
return KS_STATUS_FAIL;
|
return KS_STATUS_FAIL;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -557,6 +572,7 @@ KS_DECLARE(ks_status_t) ks_rwl_try_read_lock(ks_rwl_t *rwlock)
|
|||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
ks_hash_insert(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self(), (void *)(intptr_t)(int)1);
|
ks_hash_insert(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self(), (void *)(intptr_t)(int)1);
|
||||||
|
ks_mutex_unlock(rwlock->read_lock_mutex);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return KS_STATUS_SUCCESS;
|
return KS_STATUS_SUCCESS;
|
||||||
@ -589,14 +605,18 @@ KS_DECLARE(ks_status_t) ks_rwl_try_write_lock(ks_rwl_t *rwlock)
|
|||||||
KS_DECLARE(ks_status_t) ks_rwl_read_unlock(ks_rwl_t *rwlock)
|
KS_DECLARE(ks_status_t) ks_rwl_read_unlock(ks_rwl_t *rwlock)
|
||||||
{
|
{
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
ks_mutex_lock(rwlock->read_lock_mutex);
|
||||||
|
|
||||||
int count = (int)(intptr_t)ks_hash_remove(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self());
|
int count = (int)(intptr_t)ks_hash_remove(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self());
|
||||||
|
|
||||||
if (count > 1) {
|
if (count > 1) {
|
||||||
ks_hash_insert(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self(), (void *)(intptr_t)--count);
|
ks_hash_insert(rwlock->read_lock_list, (void *)(intptr_t)ks_thread_self(), (void *)(intptr_t)--count);
|
||||||
|
ks_mutex_unlock(rwlock->read_lock_mutex);
|
||||||
return KS_STATUS_SUCCESS;
|
return KS_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReleaseSRWLockShared(&rwlock->rwlock);
|
ReleaseSRWLockShared(&rwlock->rwlock);
|
||||||
|
ks_mutex_unlock(rwlock->read_lock_mutex);
|
||||||
#else
|
#else
|
||||||
pthread_rwlock_unlock(&rwlock->rwlock);
|
pthread_rwlock_unlock(&rwlock->rwlock);
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user