fix a deadlock bug (#87): use wrlock_owner to save the current thread holding the wrlock, skip rdlock if the wrlock owner is the current thread

This commit is contained in:
Vincent Wei
2022-09-14 12:52:29 +08:00
parent 492bd649ed
commit b5c5c3f8ae
2 changed files with 11 additions and 0 deletions

View File

@@ -147,6 +147,7 @@ typedef struct _ZORDERINFO {
# else
pthread_mutex_t rwlock;
# endif
pthread_t wrlock_owner;
#elif defined(_MGRM_PROCESSES)
int zi_semid;
int zi_semnum;

View File

@@ -602,20 +602,30 @@ static void reset_window (PMAINWIN pWin, RECT* rcWin)
static inline void lock_zi_for_change (const ZORDERINFO* zi)
{
pthread_rwlock_wrlock(&((ZORDERINFO*)zi)->rwlock);
((ZORDERINFO*)zi)->wrlock_owner = pthread_self();
}
static inline void unlock_zi_for_change (const ZORDERINFO* zi)
{
pthread_rwlock_unlock(&((ZORDERINFO*)zi)->rwlock);
((ZORDERINFO*)zi)->wrlock_owner = 0;
}
static inline void lock_zi_for_read (const ZORDERINFO* zi)
{
if (zi->wrlock_owner == pthread_self()) {
return;
}
pthread_rwlock_rdlock(&((ZORDERINFO*)zi)->rwlock);
}
static inline void unlock_zi_for_read (const ZORDERINFO* zi)
{
if (zi->wrlock_owner == pthread_self()) {
return;
}
pthread_rwlock_unlock(&((ZORDERINFO*)zi)->rwlock);
}
#else /* __NOUNIX__ */