threads: use thread ID for mutex owner

This commit is contained in:
Rémi Denis-Courmont 2021-10-07 18:09:32 +03:00 committed by Jean-Baptiste Kempf
parent 377a1c0103
commit eae968d889
2 changed files with 12 additions and 14 deletions

View File

@ -226,13 +226,13 @@ typedef struct
struct {
atomic_uint value;
atomic_uint recursion;
_Atomic (const void *) owner;
atomic_ulong owner;
};
#endif
struct {
unsigned int value;
unsigned int recursion;
const void *owner;
unsigned long owner;
} dummy;
};
} vlc_mutex_t;
@ -246,7 +246,7 @@ typedef struct
#define VLC_STATIC_MUTEX { \
.value = ATOMIC_VAR_INIT(0), \
.recursion = ATOMIC_VAR_INIT(0), \
.owner = ATOMIC_VAR_INIT(NULL), \
.owner = ATOMIC_VAR_INIT(0), \
}
/**
@ -734,8 +734,8 @@ VLC_API void vlc_control_cancel(vlc_cleanup_t *);
/**
* Thread identifier.
*
* This function returns an unique identifier of the calling thread. The
* identifier cannot change for the entire lifetime of the thread, and two
* This function returns a non-zero unique identifier of the calling thread.
* The identifier cannot change for the entire lifetime of the thread, and two
* concurrent threads cannot have the same identifier.
*
* The thread identifier has no defined semantics other than uniqueness,

View File

@ -67,7 +67,7 @@ static void vlc_mutex_init_common(vlc_mutex_t *mtx, bool recursive)
{
atomic_init(&mtx->value, 0);
atomic_init(&mtx->recursion, recursive);
atomic_init(&mtx->owner, NULL);
atomic_init(&mtx->owner, 0);
}
void vlc_mutex_init(vlc_mutex_t *mtx)
@ -80,9 +80,6 @@ void vlc_mutex_init_recursive(vlc_mutex_t *mtx)
vlc_mutex_init_common(mtx, true);
}
static _Thread_local char thread_self[1];
#define THREAD_SELF ((const void *)thread_self)
bool vlc_mutex_held(const vlc_mutex_t *mtx)
{
#if defined(__clang__) && !defined(__apple_build_version__) && (__clang_major__ < 8)
@ -101,8 +98,8 @@ bool vlc_mutex_held(const vlc_mutex_t *mtx)
* Even though other threads may modify the owner field at any time,
* they will never make it compare equal to the calling thread.
*/
return THREAD_SELF == atomic_load_explicit(&tmp_mtx->owner,
memory_order_relaxed);
return vlc_thread_id() == atomic_load_explicit(&tmp_mtx->owner,
memory_order_relaxed);
}
void vlc_mutex_lock(vlc_mutex_t *mtx)
@ -122,7 +119,7 @@ void vlc_mutex_lock(vlc_mutex_t *mtx)
vlc_atomic_wait(&mtx->value, 2);
vlc_restorecancel(canc);
atomic_store_explicit(&mtx->owner, THREAD_SELF, memory_order_relaxed);
atomic_store_explicit(&mtx->owner, vlc_thread_id(), memory_order_relaxed);
}
int vlc_mutex_trylock(vlc_mutex_t *mtx)
@ -149,7 +146,8 @@ int vlc_mutex_trylock(vlc_mutex_t *mtx)
if (atomic_compare_exchange_strong_explicit(&mtx->value, &value, 1,
memory_order_acquire,
memory_order_relaxed)) {
atomic_store_explicit(&mtx->owner, THREAD_SELF, memory_order_relaxed);
atomic_store_explicit(&mtx->owner, vlc_thread_id(),
memory_order_relaxed);
return 0;
}
@ -169,7 +167,7 @@ void vlc_mutex_unlock(vlc_mutex_t *mtx)
return;
}
atomic_store_explicit(&mtx->owner, NULL, memory_order_relaxed);
atomic_store_explicit(&mtx->owner, 0, memory_order_relaxed);
switch (atomic_exchange_explicit(&mtx->value, 0, memory_order_release)) {
case 2: