From 60056a8010c3dbaabd713f7a3e4f2963bb921a1b Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Fri, 7 Sep 2018 16:20:39 -0400 Subject: [PATCH] for c11 mtx and cnd functions, use externally consistent type names MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit despite looking like undefined behavior, the affected code is correct both before and after this patch. the pairs mtx_t and pthread_mutex_t, and cnd_t and pthread_cond_t, are not mutually compatible within a single translation unit (because they are distinct untagged aggregate instances), but they are compatible with an object of either type from another translation unit (6.2.7 ¶1), and therefore a given translation unit can choose which one it wants to use. in the interest of being able to move declarations out of source files to headers that facilitate checking, use the pthread type names in declaring the namespace-safe versions of the pthread functions and cast the argument pointer types when calling them. --- src/thread/cnd_broadcast.c | 5 +++-- src/thread/cnd_signal.c | 5 +++-- src/thread/cnd_timedwait.c | 5 +++-- src/thread/mtx_timedlock.c | 5 +++-- src/thread/mtx_trylock.c | 4 ++-- src/thread/mtx_unlock.c | 5 +++-- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/thread/cnd_broadcast.c b/src/thread/cnd_broadcast.c index 85d4d3ea..0ad061a3 100644 --- a/src/thread/cnd_broadcast.c +++ b/src/thread/cnd_broadcast.c @@ -1,10 +1,11 @@ #include +#include -int __private_cond_signal(cnd_t *, int); +int __private_cond_signal(pthread_cond_t *, int); int cnd_broadcast(cnd_t *c) { /* This internal function never fails, and always returns zero, * which matches the value thrd_success is defined with. */ - return __private_cond_signal(c, -1); + return __private_cond_signal((pthread_cond_t *)c, -1); } diff --git a/src/thread/cnd_signal.c b/src/thread/cnd_signal.c index 1211260b..8165dae1 100644 --- a/src/thread/cnd_signal.c +++ b/src/thread/cnd_signal.c @@ -1,10 +1,11 @@ #include +#include -int __private_cond_signal(cnd_t *, int); +int __private_cond_signal(pthread_cond_t *, int); int cnd_signal(cnd_t *c) { /* This internal function never fails, and always returns zero, * which matches the value thrd_success is defined with. */ - return __private_cond_signal(c, 1); + return __private_cond_signal((pthread_cond_t *)c, 1); } diff --git a/src/thread/cnd_timedwait.c b/src/thread/cnd_timedwait.c index 59976793..7bfe1045 100644 --- a/src/thread/cnd_timedwait.c +++ b/src/thread/cnd_timedwait.c @@ -1,11 +1,12 @@ #include +#include #include -int __pthread_cond_timedwait(cnd_t *restrict, mtx_t *restrict, const struct timespec *restrict); +int __pthread_cond_timedwait(pthread_cond_t *restrict, pthread_mutex_t *restrict, const struct timespec *restrict); int cnd_timedwait(cnd_t *restrict c, mtx_t *restrict m, const struct timespec *restrict ts) { - int ret = __pthread_cond_timedwait(c, m, ts); + int ret = __pthread_cond_timedwait((pthread_cond_t *)c, (pthread_mutex_t *)m, ts); switch (ret) { /* May also return EINVAL or EPERM. */ default: return thrd_error; diff --git a/src/thread/mtx_timedlock.c b/src/thread/mtx_timedlock.c index bcc152c5..d098053b 100644 --- a/src/thread/mtx_timedlock.c +++ b/src/thread/mtx_timedlock.c @@ -1,11 +1,12 @@ #include +#include #include -int __pthread_mutex_timedlock(mtx_t *restrict, const struct timespec *restrict); +int __pthread_mutex_timedlock(pthread_mutex_t *restrict, const struct timespec *restrict); int mtx_timedlock(mtx_t *restrict m, const struct timespec *restrict ts) { - int ret = __pthread_mutex_timedlock(m, ts); + int ret = __pthread_mutex_timedlock((pthread_mutex_t *)m, ts); switch (ret) { default: return thrd_error; case 0: return thrd_success; diff --git a/src/thread/mtx_trylock.c b/src/thread/mtx_trylock.c index 61e7694e..8d1fb07c 100644 --- a/src/thread/mtx_trylock.c +++ b/src/thread/mtx_trylock.c @@ -1,14 +1,14 @@ #include "pthread_impl.h" #include -int __pthread_mutex_trylock(mtx_t *); +int __pthread_mutex_trylock(pthread_mutex_t *); int mtx_trylock(mtx_t *m) { if (m->_m_type == PTHREAD_MUTEX_NORMAL) return (a_cas(&m->_m_lock, 0, EBUSY) & EBUSY) ? thrd_busy : thrd_success; - int ret = __pthread_mutex_trylock(m); + int ret = __pthread_mutex_trylock((pthread_mutex_t *)m); switch (ret) { default: return thrd_error; case 0: return thrd_success; diff --git a/src/thread/mtx_unlock.c b/src/thread/mtx_unlock.c index 5033ace7..ac91f993 100644 --- a/src/thread/mtx_unlock.c +++ b/src/thread/mtx_unlock.c @@ -1,11 +1,12 @@ #include +#include -int __pthread_mutex_unlock(mtx_t *); +int __pthread_mutex_unlock(pthread_mutex_t *); int mtx_unlock(mtx_t *mtx) { /* The only cases where pthread_mutex_unlock can return an * error are undefined behavior for C11 mtx_unlock, so we can * assume it does not return an error and simply tail call. */ - return __pthread_mutex_unlock(mtx); + return __pthread_mutex_unlock((pthread_mutex_t *)mtx); } -- 2.25.1