[Mesa-dev] [PATCH v2 1/2] mesa: Split full featured mutex into its own type
Kristian Høgsberg
krh at bitplanet.net
Thu Jan 29 20:54:28 PST 2015
Most of the mtx_t uses in mesa only use lock/unlock, but a couple of
places we use a condition variable and a recursive mutex. This patch
introduces a new full_mtx_t for the cases where we need a more featureful
mutex and changes mtx_t to only support lock/unlock.
Signed-off-by: Kristian Høgsberg <krh at bitplanet.net>
---
include/c11/threads_posix.h | 51 ++++++++++++++++++++++++++++--------
include/c11/threads_win32.h | 48 ++++++++++++++++++++++++++-------
src/gallium/auxiliary/os/os_thread.h | 10 +++----
src/mesa/main/mtypes.h | 2 +-
src/mesa/main/shared.c | 4 +--
src/mesa/main/texobj.c | 4 +--
src/mesa/main/texobj.h | 4 +--
7 files changed, 91 insertions(+), 32 deletions(-)
diff --git a/include/c11/threads_posix.h b/include/c11/threads_posix.h
index f9c165d..5119773 100644
--- a/include/c11/threads_posix.h
+++ b/include/c11/threads_posix.h
@@ -59,13 +59,14 @@ Configuration macro:
#endif
// FIXME: temporary non-standard hack to ease transition
+#define _FULL_MTX_INITIALIZER_NP PTHREAD_MUTEX_INITIALIZER
#define _MTX_INITIALIZER_NP PTHREAD_MUTEX_INITIALIZER
/*---------------------------- types ----------------------------*/
typedef pthread_cond_t cnd_t;
typedef pthread_t thrd_t;
typedef pthread_key_t tss_t;
-typedef pthread_mutex_t mtx_t;
+typedef pthread_mutex_t full_mtx_t;
typedef pthread_once_t once_flag;
@@ -135,7 +136,7 @@ cnd_signal(cnd_t *cond)
// 7.25.3.5
static inline int
-cnd_timedwait(cnd_t *cond, mtx_t *mtx, const xtime *xt)
+cnd_timedwait(cnd_t *cond, full_mtx_t *mtx, const xtime *xt)
{
struct timespec abs_time;
int rt;
@@ -148,7 +149,7 @@ cnd_timedwait(cnd_t *cond, mtx_t *mtx, const xtime *xt)
// 7.25.3.6
static inline int
-cnd_wait(cnd_t *cond, mtx_t *mtx)
+cnd_wait(cnd_t *cond, full_mtx_t *mtx)
{
if (!cond || !mtx) return thrd_error;
pthread_cond_wait(cond, mtx);
@@ -159,7 +160,7 @@ cnd_wait(cnd_t *cond, mtx_t *mtx)
/*-------------------- 7.25.4 Mutex functions --------------------*/
// 7.25.4.1
static inline void
-mtx_destroy(mtx_t *mtx)
+full_mtx_destroy(full_mtx_t *mtx)
{
assert(mtx);
pthread_mutex_destroy(mtx);
@@ -167,7 +168,7 @@ mtx_destroy(mtx_t *mtx)
// 7.25.4.2
static inline int
-mtx_init(mtx_t *mtx, int type)
+full_mtx_init(full_mtx_t *mtx, int type)
{
pthread_mutexattr_t attr;
if (!mtx) return thrd_error;
@@ -191,7 +192,7 @@ mtx_init(mtx_t *mtx, int type)
// 7.25.4.3
static inline int
-mtx_lock(mtx_t *mtx)
+full_mtx_lock(full_mtx_t *mtx)
{
if (!mtx) return thrd_error;
pthread_mutex_lock(mtx);
@@ -199,14 +200,14 @@ mtx_lock(mtx_t *mtx)
}
static inline int
-mtx_trylock(mtx_t *mtx);
+full_mtx_trylock(full_mtx_t *mtx);
static inline void
thrd_yield(void);
// 7.25.4.4
static inline int
-mtx_timedlock(mtx_t *mtx, const xtime *xt)
+full_mtx_timedlock(full_mtx_t *mtx, const xtime *xt)
{
if (!mtx || !xt) return thrd_error;
{
@@ -222,7 +223,7 @@ mtx_timedlock(mtx_t *mtx, const xtime *xt)
#else
time_t expire = time(NULL);
expire += xt->sec;
- while (mtx_trylock(mtx) != thrd_success) {
+ while (full_mtx_trylock(mtx) != thrd_success) {
time_t now = time(NULL);
if (expire < now)
return thrd_busy;
@@ -236,7 +237,7 @@ mtx_timedlock(mtx_t *mtx, const xtime *xt)
// 7.25.4.5
static inline int
-mtx_trylock(mtx_t *mtx)
+full_mtx_trylock(full_mtx_t *mtx)
{
if (!mtx) return thrd_error;
return (pthread_mutex_trylock(mtx) == 0) ? thrd_success : thrd_busy;
@@ -244,13 +245,41 @@ mtx_trylock(mtx_t *mtx)
// 7.25.4.6
static inline int
-mtx_unlock(mtx_t *mtx)
+full_mtx_unlock(full_mtx_t *mtx)
{
if (!mtx) return thrd_error;
pthread_mutex_unlock(mtx);
return thrd_success;
}
+typedef full_mtx_t mtx_t;
+
+static inline void
+mtx_init(mtx_t *mtx, int type)
+{
+ assert(type == mtx_plain);
+
+ full_mtx_init(mtx, mtx_plain);
+}
+
+static inline void
+mtx_destroy(mtx_t *mtx)
+{
+ full_mtx_destroy(mtx);
+}
+
+static inline void
+mtx_lock(mtx_t *mtx)
+{
+ full_mtx_lock(mtx);
+}
+
+static inline void
+mtx_unlock(mtx_t *mtx)
+{
+ full_mtx_unlock(mtx);
+}
+
/*------------------- 7.25.5 Thread functions -------------------*/
// 7.25.5.1
diff --git a/include/c11/threads_win32.h b/include/c11/threads_win32.h
index d017c31..6968ce4 100644
--- a/include/c11/threads_win32.h
+++ b/include/c11/threads_win32.h
@@ -85,6 +85,7 @@ Configuration macro:
#define TSS_DTOR_ITERATIONS 1
// FIXME: temporary non-standard hack to ease transition
+#define _FULL_MTX_INITIALIZER_NP {(PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0}
#define _MTX_INITIALIZER_NP {(PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0}
/*---------------------------- types ----------------------------*/
@@ -206,7 +207,7 @@ static void impl_cond_do_signal(cnd_t *cond, int broadcast)
ReleaseSemaphore(cond->sem_queue, nsignal, NULL);
}
-static int impl_cond_do_wait(cnd_t *cond, mtx_t *mtx, const xtime *xt)
+static int impl_cond_do_wait(cnd_t *cond, full_mtx_t *mtx, const xtime *xt)
{
int nleft = 0;
int ngone = 0;
@@ -378,7 +379,7 @@ cnd_signal(cnd_t *cond)
// 7.25.3.5
static inline int
-cnd_timedwait(cnd_t *cond, mtx_t *mtx, const xtime *xt)
+cnd_timedwait(cnd_t *cond, full_mtx_t *mtx, const xtime *xt)
{
if (!cond || !mtx || !xt) return thrd_error;
#ifdef EMULATED_THREADS_USE_NATIVE_CV
@@ -392,7 +393,7 @@ cnd_timedwait(cnd_t *cond, mtx_t *mtx, const xtime *xt)
// 7.25.3.6
static inline int
-cnd_wait(cnd_t *cond, mtx_t *mtx)
+cnd_wait(cnd_t *cond, full_mtx_t *mtx)
{
if (!cond || !mtx) return thrd_error;
#ifdef EMULATED_THREADS_USE_NATIVE_CV
@@ -407,7 +408,7 @@ cnd_wait(cnd_t *cond, mtx_t *mtx)
/*-------------------- 7.25.4 Mutex functions --------------------*/
// 7.25.4.1
static inline void
-mtx_destroy(mtx_t *mtx)
+full_mtx_destroy(full_mtx_t *mtx)
{
assert(mtx);
DeleteCriticalSection(mtx);
@@ -415,7 +416,7 @@ mtx_destroy(mtx_t *mtx)
// 7.25.4.2
static inline int
-mtx_init(mtx_t *mtx, int type)
+full_mtx_init(full_mtx_t *mtx, int type)
{
if (!mtx) return thrd_error;
if (type != mtx_plain && type != mtx_timed && type != mtx_try
@@ -429,7 +430,7 @@ mtx_init(mtx_t *mtx, int type)
// 7.25.4.3
static inline int
-mtx_lock(mtx_t *mtx)
+full_mtx_lock(full_mtx_t *mtx)
{
if (!mtx) return thrd_error;
EnterCriticalSection(mtx);
@@ -438,7 +439,7 @@ mtx_lock(mtx_t *mtx)
// 7.25.4.4
static inline int
-mtx_timedlock(mtx_t *mtx, const xtime *xt)
+full_mtx_timedlock(full_mtx_t *mtx, const xtime *xt)
{
time_t expire, now;
if (!mtx || !xt) return thrd_error;
@@ -456,7 +457,7 @@ mtx_timedlock(mtx_t *mtx, const xtime *xt)
// 7.25.4.5
static inline int
-mtx_trylock(mtx_t *mtx)
+full_mtx_trylock(full_mtx_t *mtx)
{
if (!mtx) return thrd_error;
return TryEnterCriticalSection(mtx) ? thrd_success : thrd_busy;
@@ -464,7 +465,7 @@ mtx_trylock(mtx_t *mtx)
// 7.25.4.6
static inline int
-mtx_unlock(mtx_t *mtx)
+full_mtx_unlock(full_mtx_t *mtx)
{
if (!mtx) return thrd_error;
LeaveCriticalSection(mtx);
@@ -472,6 +473,35 @@ mtx_unlock(mtx_t *mtx)
}
+typedef full_mtx_t mtx_t;
+
+static inline void
+mtx_init(mtx_t *mtx, int type)
+{
+ assert(type == mtx_plain);
+
+ full_mtx_init(mtx, mtx_plain);
+}
+
+static inline void
+mtx_destroy(mtx_t *mtx)
+{
+ full_mtx_destroy(mtx);
+}
+
+static inline void
+mtx_lock(mtx_t *mtx)
+{
+ full_mtx_lock(mtx);
+}
+
+static inline void
+mtx_unlock(mtx_t *mtx)
+{
+ full_mtx_unlock(mtx);
+}
+
+
/*------------------- 7.25.5 Thread functions -------------------*/
// 7.25.5.1
static inline int
diff --git a/src/gallium/auxiliary/os/os_thread.h b/src/gallium/auxiliary/os/os_thread.h
index ff46a89..4acda4b 100644
--- a/src/gallium/auxiliary/os/os_thread.h
+++ b/src/gallium/auxiliary/os/os_thread.h
@@ -88,22 +88,22 @@ static INLINE int pipe_thread_destroy( pipe_thread thread )
/* pipe_mutex
*/
-typedef mtx_t pipe_mutex;
+typedef full_mtx_t pipe_mutex;
#define pipe_static_mutex(mutex) \
static pipe_mutex mutex = _MTX_INITIALIZER_NP
#define pipe_mutex_init(mutex) \
- (void) mtx_init(&(mutex), mtx_plain)
+ (void) full_mtx_init(&(mutex), mtx_plain)
#define pipe_mutex_destroy(mutex) \
- mtx_destroy(&(mutex))
+ full_mtx_destroy(&(mutex))
#define pipe_mutex_lock(mutex) \
- (void) mtx_lock(&(mutex))
+ (void) full_mtx_lock(&(mutex))
#define pipe_mutex_unlock(mutex) \
- (void) mtx_unlock(&(mutex))
+ (void) full_mtx_unlock(&(mutex))
/* pipe_condvar
diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h
index 4c83379..a030f92 100644
--- a/src/mesa/main/mtypes.h
+++ b/src/mesa/main/mtypes.h
@@ -3112,7 +3112,7 @@ struct gl_shared_state
* \todo Improve the granularity of locking.
*/
/*@{*/
- mtx_t TexMutex; /**< texobj thread safety */
+ full_mtx_t TexMutex; /**< texobj thread safety */
GLuint TextureStateStamp; /**< state notification for shared tex */
/*@}*/
diff --git a/src/mesa/main/shared.c b/src/mesa/main/shared.c
index ccf5355..bac8401 100644
--- a/src/mesa/main/shared.c
+++ b/src/mesa/main/shared.c
@@ -113,7 +113,7 @@ _mesa_alloc_shared_state(struct gl_context *ctx)
assert(shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount == 1);
/* Mutex and timestamp for texobj state validation */
- mtx_init(&shared->TexMutex, mtx_recursive);
+ full_mtx_init(&shared->TexMutex, mtx_recursive);
shared->TextureStateStamp = 0;
shared->FrameBuffers = _mesa_NewHashTable();
@@ -357,7 +357,7 @@ free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared)
_mesa_DeleteHashTable(shared->TexObjects);
mtx_destroy(&shared->Mutex);
- mtx_destroy(&shared->TexMutex);
+ full_mtx_destroy(&shared->TexMutex);
free(shared);
}
diff --git a/src/mesa/main/texobj.c b/src/mesa/main/texobj.c
index a99dd7a..10da9bc 100644
--- a/src/mesa/main/texobj.c
+++ b/src/mesa/main/texobj.c
@@ -2026,7 +2026,7 @@ _mesa_IsTexture( GLuint texture )
void
_mesa_lock_context_textures( struct gl_context *ctx )
{
- mtx_lock(&ctx->Shared->TexMutex);
+ full_mtx_lock(&ctx->Shared->TexMutex);
if (ctx->Shared->TextureStateStamp != ctx->TextureStateTimestamp) {
ctx->NewState |= _NEW_TEXTURE;
@@ -2039,7 +2039,7 @@ void
_mesa_unlock_context_textures( struct gl_context *ctx )
{
assert(ctx->Shared->TextureStateStamp == ctx->TextureStateTimestamp);
- mtx_unlock(&ctx->Shared->TexMutex);
+ full_mtx_unlock(&ctx->Shared->TexMutex);
}
diff --git a/src/mesa/main/texobj.h b/src/mesa/main/texobj.h
index ec5ccb2..1319565 100644
--- a/src/mesa/main/texobj.h
+++ b/src/mesa/main/texobj.h
@@ -107,7 +107,7 @@ _mesa_reference_texobj(struct gl_texture_object **ptr,
static inline void
_mesa_lock_texture(struct gl_context *ctx, struct gl_texture_object *texObj)
{
- mtx_lock(&ctx->Shared->TexMutex);
+ full_mtx_lock(&ctx->Shared->TexMutex);
ctx->Shared->TextureStateStamp++;
(void) texObj;
}
@@ -116,7 +116,7 @@ static inline void
_mesa_unlock_texture(struct gl_context *ctx, struct gl_texture_object *texObj)
{
(void) texObj;
- mtx_unlock(&ctx->Shared->TexMutex);
+ full_mtx_unlock(&ctx->Shared->TexMutex);
}
--
2.2.2
More information about the mesa-dev
mailing list