Browse Source

threads: use thread ID for mutex owner

Rémi Denis-Courmont 5 years ago
committed by Alexandre Janniaux
parent
commit
a0669d6457
  1. 10
      include/vlc_threads.h
  2. 16
      src/misc/threads.c

10
include/vlc_threads.h

@ -228,13 +228,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;
@ -248,7 +248,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), \
}
/**
@ -803,8 +803,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,

16
src/misc/threads.c

@ -117,7 +117,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)
@ -130,9 +130,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)
@ -151,8 +148,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)
@ -172,7 +169,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)
@ -199,7 +196,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;
}
@ -219,7 +217,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:

Loading…
Cancel
Save