WIN32 thread primitives must return int

Also, zero return means "success", non-zero means "failure" and the
non-zero value is a system error.  That's how it is for the other
platforms' thread primitives.

(The no-threads defaults are still wrong though, as then are macros that
expand into do..while, which can't be used as expressions and don't
"return" values.)
This commit is contained in:
Nicolas Williams
2016-04-13 00:47:36 -05:00
parent 4b37c27515
commit c2fafff992

View File

@@ -114,15 +114,16 @@ typedef struct heim_mutex {
HANDLE h; HANDLE h;
} heim_mutex_t; } heim_mutex_t;
static inline void static inline int
heim_mutex_init(heim_mutex_t *m) heim_mutex_init(heim_mutex_t *m)
{ {
m->h = CreateSemaphore(NULL, 1, 1, NULL); m->h = CreateSemaphore(NULL, 1, 1, NULL);
if (m->h == INVALID_HANDLE_VALUE) if (m->h == INVALID_HANDLE_VALUE)
abort(); return EAGAIN;
return 0;
} }
static inline void static inline int
heim_mutex_lock(heim_mutex_t *m) heim_mutex_lock(heim_mutex_t *m)
{ {
HANDLE h, new_h; HANDLE h, new_h;
@@ -133,7 +134,7 @@ heim_mutex_lock(heim_mutex_t *m)
created = 1; created = 1;
new_h = CreateSemaphore(NULL, 0, 1, NULL); new_h = CreateSemaphore(NULL, 0, 1, NULL);
if (new_h == INVALID_HANDLE_VALUE) if (new_h == INVALID_HANDLE_VALUE)
abort(); return EAGAIN;
if (InterlockedCompareExchangePointer(&m->h, new_h, h) != h) { if (InterlockedCompareExchangePointer(&m->h, new_h, h) != h) {
created = 0; created = 0;
CloseHandle(new_h); CloseHandle(new_h);
@@ -141,16 +142,18 @@ heim_mutex_lock(heim_mutex_t *m)
} }
if (!created) if (!created)
WaitForSingleObject(m->h, INFINITE); WaitForSingleObject(m->h, INFINITE);
return 0;
} }
static inline void static inline int
heim_mutex_unlock(heim_mutex_t *m) heim_mutex_unlock(heim_mutex_t *m)
{ {
if (ReleaseSemaphore(m->h, 1, NULL) == FALSE) if (ReleaseSemaphore(m->h, 1, NULL) == FALSE)
abort(); return EPERM;
return 0;
} }
static inline void static inline int
heim_mutex_destroy(heim_mutex_t *m) heim_mutex_destroy(heim_mutex_t *m)
{ {
HANDLE h; HANDLE h;
@@ -158,6 +161,7 @@ heim_mutex_destroy(heim_mutex_t *m)
h = InterlockedCompareExchangePointer(&m->h, INVALID_HANDLE_VALUE, m->h); h = InterlockedCompareExchangePointer(&m->h, INVALID_HANDLE_VALUE, m->h);
if (h != INVALID_HANDLE_VALUE) if (h != INVALID_HANDLE_VALUE)
CloseHandle(h); CloseHandle(h);
return 0;
} }
#define HEIMDAL_MUTEX heim_mutex_t #define HEIMDAL_MUTEX heim_mutex_t
@@ -172,49 +176,46 @@ typedef struct heim_rwlock {
int exclusive; int exclusive;
} heim_rwlock_t; } heim_rwlock_t;
static inline void static inline int
heim_rwlock_init(heim_rwlock_t *l) heim_rwlock_init(heim_rwlock_t *l)
{ {
InitializeSRWLock(&l->lock); InitializeSRWLock(&l->lock);
l->exclusive = 0; l->exclusive = 0;
return 0;
} }
static inline void static inline int
heim_rwlock_rdlock(heim_rwlock_t *l) heim_rwlock_rdlock(heim_rwlock_t *l)
{ {
AcquireSRWLockShared(&l->lock); AcquireSRWLockShared(&l->lock);
return 0;
} }
static inline void static inline int
heim_rwlock_wrlock(heim_rwlock_t *l) heim_rwlock_wrlock(heim_rwlock_t *l)
{ {
AcquireSRWLockExclusive(&l->lock); AcquireSRWLockExclusive(&l->lock);
l->exclusive = 1; l->exclusive = 1;
return 0;
} }
static inline BOOLEAN static inline int
heim_rwlock_tryrdlock(heim_rwlock_t *l) heim_rwlock_tryrdlock(heim_rwlock_t *l)
{ {
BOOLEAN bLocked; if (TryAcquireSRWLockShared(&l->lock))
return 0;
bLocked = TryAcquireSRWLockShared(&l->lock); return EBUSY;
return bLocked;
} }
static inline BOOLEAN static inline int
heim_rwlock_trywrlock(heim_rwlock_t *l) heim_rwlock_trywrlock(heim_rwlock_t *l)
{ {
BOOLEAN bLocked; if (TryAcquireSRWLockExclusive(&l->lock))
return 0;
bLocked = TryAcquireSRWLockExclusive(&l->lock); return EBUSY;
if (bLocked)
l->exclusive = 1;
return bLocked;
} }
static inline void static inline vint
heim_rwlock_unlock(heim_rwlock_t *l) heim_rwlock_unlock(heim_rwlock_t *l)
{ {
if (l->exclusive) { if (l->exclusive) {
@@ -223,14 +224,16 @@ heim_rwlock_unlock(heim_rwlock_t *l)
} else { } else {
ReleaseSRWLockShared(&(l)->lock); ReleaseSRWLockShared(&(l)->lock);
} }
return 0;
} }
static inline void static inline int
heim_rwlock_destroy(heim_rwlock_t *l) heim_rwlock_destroy(heim_rwlock_t *l)
{ {
/* SRW locks cannot be destroyed so re-initialize */ /* SRW locks cannot be destroyed so re-initialize */
InitializeSRWLock(&l->lock); InitializeSRWLock(&l->lock);
l->exclusive = 0; l->exclusive = 0;
return 0;
} }
#define HEIMDAL_RWLOCK heim_rwlock_t #define HEIMDAL_RWLOCK heim_rwlock_t