Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MacOS: fix usage of libdispatch and Availability macros in Thread #1523

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ int Thread_set_name(const char* thread_name)
#endif*/
#elif defined(OSX)
/* pthread_setname_np __API_AVAILABLE(macos(10.6), ios(3.2)) */
#if defined(__APPLE__) && __MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
#if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
rc = pthread_setname_np(thread_name);
#endif
#else
Expand Down Expand Up @@ -234,7 +234,7 @@ sem_type Thread_create_sem(int *rc)
NULL /* object name */
);
*rc = (sem == NULL) ? GetLastError() : 0;
#elif defined(OSX)
#elif defined(OSX) && defined(HAS_DISPATCH)
sem = dispatch_semaphore_create(0L);
*rc = (sem == NULL) ? -1 : 0;
#else
Expand All @@ -259,7 +259,7 @@ int Thread_wait_sem(sem_type sem, int timeout)
* so I've used trywait in a loop instead. Ian Craggs 23/7/2010
*/
int rc = -1;
#if !defined(_WIN32) && !defined(_WIN64) && !defined(OSX)
#if !defined(_WIN32) && !defined(_WIN64) && !(defined(OSX) && defined(HAS_DISPATCH))
#define USE_TRYWAIT
#if defined(USE_TRYWAIT)
int i = 0;
Expand All @@ -276,7 +276,7 @@ int Thread_wait_sem(sem_type sem, int timeout)
rc = WaitForSingleObject(sem, timeout < 0 ? 0 : timeout);
if (rc == WAIT_TIMEOUT)
rc = ETIMEDOUT;
#elif defined(OSX)
#elif defined(OSX) && defined(HAS_DISPATCH)
/* returns 0 on success, non-zero if timeout occurred */
rc = (int)dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, (int64_t)timeout*1000000L));
if (rc != 0)
Expand Down Expand Up @@ -320,7 +320,7 @@ int Thread_check_sem(sem_type sem)
#if defined(_WIN32) || defined(_WIN64)
/* if the return value is not 0, the semaphore will not have been decremented */
return WaitForSingleObject(sem, 0) == WAIT_OBJECT_0;
#elif defined(OSX)
#elif defined(OSX) && defined(HAS_DISPATCH)
/* if the return value is not 0, the semaphore will not have been decremented */
return dispatch_semaphore_wait(sem, DISPATCH_TIME_NOW) == 0;
#else
Expand All @@ -344,7 +344,7 @@ int Thread_post_sem(sem_type sem)
#if defined(_WIN32) || defined(_WIN64)
if (SetEvent(sem) == 0)
rc = GetLastError();
#elif defined(OSX)
#elif defined(OSX) && defined(HAS_DISPATCH)
rc = (int)dispatch_semaphore_signal(sem);
#else
int val;
Expand All @@ -371,7 +371,7 @@ int Thread_destroy_sem(sem_type sem)
FUNC_ENTRY;
#if defined(_WIN32) || defined(_WIN64)
rc = CloseHandle(sem);
#elif defined(OSX)
#elif defined(OSX) && defined(HAS_DISPATCH)
dispatch_release(sem);
#else
rc = sem_destroy(sem);
Expand Down Expand Up @@ -449,7 +449,7 @@ int Thread_wait_cond(cond_type condvar, int timeout_ms)
interval.tv_sec = timeout_ms / 1000;
interval.tv_nsec = (timeout_ms % 1000) * 1000000L;

#if defined(__APPLE__) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101200 /* for older versions of MacOS */
#if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED < 101200 /* for older versions of MacOS */
struct timeval cur_time;
gettimeofday(&cur_time, NULL);
cond_timeout.tv_sec = cur_time.tv_sec;
Expand Down
9 changes: 8 additions & 1 deletion src/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
#endif
#endif

#ifdef __APPLE__
#include <AvailabilityMacros.h>
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 && !defined(__ppc__)
#define HAS_DISPATCH
#endif
#endif

#include "MQTTExportDeclarations.h"

#include "MQTTClient.h"
Expand All @@ -54,7 +61,7 @@
typedef thread_return_type (*thread_fn)(void*);
typedef struct { pthread_cond_t cond; pthread_mutex_t mutex; } cond_type_struct;
typedef cond_type_struct *cond_type;
#if defined(OSX)
#if defined(OSX) && defined(HAS_DISPATCH)
#include <dispatch/dispatch.h>
typedef dispatch_semaphore_t sem_type;
#else
Expand Down
Loading