Skip to content

Commit

Permalink
winpthreads: make winpthreads compatible with libcxx __attribute__((_…
Browse files Browse the repository at this point in the history
…_require_constant_initialization__))

libcxx defined `_LIBCPP_SAFE_STATIC` for static variable, if use winpthreads need `PTHREAD_*_INITIALIZER` constant, but current version have `reinterpret_cast` to convert integer initial value to pointer, `__attribute__((__require_constant_initialization__))` not allowed this define and causing compilation error..

Signed-off-by: SquallATF <squallatf@gmail.com>
Signed-off-by: Liu Hao <lh_mouse@126.com>
  • Loading branch information
SquallATF authored and lhmouse committed Oct 26, 2018
1 parent c69c7a7 commit 8bba2a9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
16 changes: 8 additions & 8 deletions mingw-w64-libraries/winpthreads/include/pthread.h
Expand Up @@ -265,20 +265,20 @@ int WINPTHREAD_API pthread_attr_setschedpolicy (pthread_attr_t *attr, int pol);
int WINPTHREAD_API pthread_attr_getschedpolicy (const pthread_attr_t *attr, int *pol);

/* synchronization objects */
typedef void *pthread_spinlock_t;
typedef void *pthread_mutex_t;
typedef void *pthread_cond_t;
typedef void *pthread_rwlock_t;
typedef intptr_t pthread_spinlock_t;
typedef intptr_t pthread_mutex_t;
typedef intptr_t pthread_cond_t;
typedef intptr_t pthread_rwlock_t;
typedef void *pthread_barrier_t;

#define PTHREAD_MUTEX_NORMAL 0
#define PTHREAD_MUTEX_ERRORCHECK 1
#define PTHREAD_MUTEX_RECURSIVE 2

#define GENERIC_INITIALIZER ((void *) (size_t) -1)
#define GENERIC_ERRORCHECK_INITIALIZER ((void *) (size_t) -2)
#define GENERIC_RECURSIVE_INITIALIZER ((void *) (size_t) -3)
#define GENERIC_NORMAL_INITIALIZER ((void *) (size_t) -1)
#define GENERIC_INITIALIZER -1
#define GENERIC_ERRORCHECK_INITIALIZER -2
#define GENERIC_RECURSIVE_INITIALIZER -3
#define GENERIC_NORMAL_INITIALIZER -1
#define PTHREAD_MUTEX_INITIALIZER (pthread_mutex_t)GENERIC_INITIALIZER
#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER (pthread_mutex_t)GENERIC_RECURSIVE_INITIALIZER
#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER (pthread_mutex_t)GENERIC_ERRORCHECK_INITIALIZER
Expand Down
16 changes: 8 additions & 8 deletions mingw-w64-libraries/winpthreads/src/cond.c
Expand Up @@ -67,10 +67,10 @@ void cond_print(volatile pthread_cond_t *c, char *txt)
if (!print_state) return;
cond_t *c_ = (cond_t *)*c;
if (c_ == NULL) {
fprintf(fo,"C%p %d %s\n",*c,(int)GetCurrentThreadId(),txt);
fprintf(fo,"C%p %d %s\n",(void *)*c,(int)GetCurrentThreadId(),txt);
} else {
fprintf(fo,"C%p %d V=%0X w=%ld %s\n",
*c,
(void *)*c,
(int)GetCurrentThreadId(),
(int)c_->valid,
c_->waiters_count_,
Expand Down Expand Up @@ -202,7 +202,7 @@ pthread_cond_init (pthread_cond_t *c, const pthread_condattr_t *a)
if (a && *a == PTHREAD_PROCESS_SHARED)
return ENOSYS;

if ( !(_c = (pthread_cond_t)calloc(1,sizeof(*_c))) ) {
if ( !(_c = calloc(1,sizeof(*_c))) ) {
return ENOMEM;
}
_c->valid = DEAD_COND;
Expand Down Expand Up @@ -236,10 +236,10 @@ pthread_cond_init (pthread_cond_t *c, const pthread_condattr_t *a)
if (!r)
{
_c->valid = LIFE_COND;
*c = _c;
*c = (pthread_cond_t)_c;
}
else
*c = NULL;
*c = (pthread_cond_t)NULL;
return r;
}

Expand All @@ -255,7 +255,7 @@ pthread_cond_destroy (pthread_cond_t *c)
pthread_spin_lock (&cond_locked);
if (*c == PTHREAD_COND_INITIALIZER)
{
*c = NULL;
*c = (pthread_cond_t)NULL;
r = 0;
}
else
Expand All @@ -279,7 +279,7 @@ pthread_cond_destroy (pthread_cond_t *c)
LeaveCriticalSection(&_c->waiters_count_lock_);
return r;
}
*c = NULL;
*c = (pthread_cond_t)NULL;
do_sema_b_release (_c->sema_b, 1,&_c->waiters_b_lock_,&_c->value_b);

if (!CloseHandle (_c->sema_q) && !r)
Expand Down Expand Up @@ -419,7 +419,7 @@ pthread_cond_wait (pthread_cond_t *c, pthread_mutex_t *external_mutex)

/* pthread_testcancel(); */

if (!c || *c == NULL)
if (!c || *c == (pthread_cond_t)NULL)
return EINVAL;
_c = (cond_t *)*c;
if (*c == PTHREAD_COND_INITIALIZER)
Expand Down
18 changes: 9 additions & 9 deletions mingw-w64-libraries/winpthreads/src/mutex.c
Expand Up @@ -73,18 +73,18 @@ mutex_impl_init(pthread_mutex_t *m, mutex_impl_t *mi)
if (new_mi == NULL)
return NULL;
new_mi->state = Unlocked;
new_mi->type = (mi == PTHREAD_RECURSIVE_MUTEX_INITIALIZER ? Recursive
: mi == PTHREAD_ERRORCHECK_MUTEX_INITIALIZER ? Errorcheck
new_mi->type = (mi == (void *)PTHREAD_RECURSIVE_MUTEX_INITIALIZER ? Recursive
: mi == (void *)PTHREAD_ERRORCHECK_MUTEX_INITIALIZER ? Errorcheck
: Normal);
new_mi->event = NULL;
new_mi->rec_lock = 0;
new_mi->owner = (DWORD)-1;
if (__sync_bool_compare_and_swap(m, mi, new_mi)) {
if (__sync_bool_compare_and_swap(m, (pthread_mutex_t)mi, (pthread_mutex_t)new_mi)) {
return new_mi;
} else {
/* Someone created the struct before us. */
free(new_mi);
return *m;
return (mutex_impl_t *)*m;
}
}

Expand All @@ -96,8 +96,8 @@ mutex_impl_init(pthread_mutex_t *m, mutex_impl_t *mi)
static inline mutex_impl_t *
mutex_impl(pthread_mutex_t *m)
{
mutex_impl_t *mi = *m;
if (is_static_initializer(mi)) {
mutex_impl_t *mi = (mutex_impl_t *)*m;
if (is_static_initializer((pthread_mutex_t)mi)) {
return mutex_impl_init(m, mi);
} else {
/* mi cannot be null here; avoid a test in the fast path. */
Expand Down Expand Up @@ -279,13 +279,13 @@ pthread_mutex_init (pthread_mutex_t *m, const pthread_mutexattr_t *a)

int pthread_mutex_destroy (pthread_mutex_t *m)
{
mutex_impl_t *mi = *m;
if (!is_static_initializer(mi)) {
mutex_impl_t *mi = (mutex_impl_t *)*m;
if (!is_static_initializer((pthread_mutex_t)mi)) {
if (mi->event != NULL)
CloseHandle(mi->event);
free(mi);
/* Sabotage attempts to re-use the mutex before initialising it again. */
*m = NULL;
*m = (pthread_mutex_t)NULL;
}

return 0;
Expand Down
18 changes: 9 additions & 9 deletions mingw-w64-libraries/winpthreads/src/rwlock.c
Expand Up @@ -81,18 +81,18 @@ static WINPTHREADS_ATTRIBUTE((noinline)) int rwl_ref_destroy(pthread_rwlock_t *r
{
int r = 0;

*rDestroy = NULL;
*rDestroy = (pthread_rwlock_t)NULL;
pthread_spin_lock(&rwl_global);

if (!rwl || !*rwl) r = EINVAL;
else {
rwlock_t *r_ = (rwlock_t *)*rwl;
if (STATIC_RWL_INITIALIZER(*rwl)) *rwl = NULL;
if (STATIC_RWL_INITIALIZER(*rwl)) *rwl = (pthread_rwlock_t)NULL;
else if (r_->valid != LIFE_RWLOCK) r = EINVAL;
else if (r_->busy) r = EBUSY;
else {
*rDestroy = *rwl;
*rwl = NULL;
*rwl = (pthread_rwlock_t)NULL;
}
}

Expand Down Expand Up @@ -136,10 +136,10 @@ void rwl_print(volatile pthread_rwlock_t *rwl, char *txt)
if (!print_state) return;
rwlock_t *r = (rwlock_t *)*rwl;
if (r == NULL) {
printf("RWL%p %d %s\n",*rwl,(int)GetCurrentThreadId(),txt);
printf("RWL%p %d %s\n",(void *)*rwl,(int)GetCurrentThreadId(),txt);
} else {
printf("RWL%p %d V=%0X B=%d r=%ld w=%ld L=%p %s\n",
*rwl,
(void *)*rwl,
(int)GetCurrentThreadId(),
(int)r->valid,
(int)r->busy,
Expand Down Expand Up @@ -172,8 +172,8 @@ int pthread_rwlock_init (pthread_rwlock_t *rwlock_, const pthread_rwlockattr_t *

if(!rwlock_)
return EINVAL;
*rwlock_ = NULL;
if ((rwlock = (pthread_rwlock_t)calloc(1, sizeof(*rwlock))) == NULL)
*rwlock_ = (pthread_rwlock_t)NULL;
if ((rwlock = calloc(1, sizeof(*rwlock))) == NULL)
return ENOMEM;
rwlock->valid = DEAD_RWLOCK;

Expand All @@ -197,7 +197,7 @@ int pthread_rwlock_init (pthread_rwlock_t *rwlock_, const pthread_rwlockattr_t *
return r;
}
rwlock->valid = LIFE_RWLOCK;
*rwlock_ = rwlock;
*rwlock_ = (pthread_rwlock_t)rwlock;
return r;
}

Expand Down Expand Up @@ -239,7 +239,7 @@ int pthread_rwlock_destroy (pthread_rwlock_t *rwlock_)
r2 = pthread_mutex_destroy(&rwlock->mcomplete);
if (!r) r = r2;
rwlock->valid = DEAD_RWLOCK;
free(rDestroy);
free((void *)rDestroy);
return 0;
}

Expand Down

0 comments on commit 8bba2a9

Please sign in to comment.