1 /******************************************************************************* 2 * Copyright (c) 2009, 2018 IBM Corp. 3 * 4 * All rights reserved. This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License v1.0 6 * and Eclipse Distribution License v1.0 which accompany this distribution. 7 * 8 * The Eclipse Public License is available at 9 * http://www.eclipse.org/legal/epl-v10.html 10 * and the Eclipse Distribution License is available at 11 * http://www.eclipse.org/org/documents/edl-v10.php. 12 * 13 * Contributors: 14 * Ian Craggs - initial implementation 15 * Ian Craggs, Allan Stockdill-Mander - async client updates 16 * Ian Craggs - fix for bug #420851 17 * Ian Craggs - change MacOS semaphore implementation 18 *******************************************************************************/ 19 #include "MQTTClient.h" 20 #define NOSTACKTRACE 21 #if !defined(THREAD_H) 22 #define THREAD_H 23 24 #include "mutex_type.h" /* Needed for mutex_type */ 25 26 #if defined(WIN32) || defined(WIN64) 27 #include <windows.h> 28 #define thread_type HANDLE 29 #define thread_id_type DWORD 30 #define thread_return_type DWORD 31 #define thread_fn LPTHREAD_START_ROUTINE 32 #define cond_type HANDLE 33 #define sem_type HANDLE 34 #undef ETIMEDOUT 35 #define ETIMEDOUT WSAETIMEDOUT 36 #else 37 #include <pthread.h> 38 39 #define thread_type pthread_t 40 #define thread_id_type pthread_t 41 #define thread_return_type void* 42 typedef thread_return_type (*thread_fn)(void*); 43 typedef struct { pthread_cond_t cond; pthread_mutex_t mutex; } cond_type_struct; 44 typedef cond_type_struct *cond_type; 45 #if defined(OSX) 46 #include <dispatch/dispatch.h> 47 typedef dispatch_semaphore_t sem_type; 48 #else 49 #include <semaphore.h> 50 typedef sem_t *sem_type; 51 #endif 52 53 cond_type Thread_create_cond(void); 54 int Thread_signal_cond(cond_type); 55 int Thread_wait_cond(cond_type condvar, int timeout); 56 int Thread_destroy_cond(cond_type); 57 #endif 58 59 DLLExport thread_type Thread_start(thread_fn, void*); 60 61 mutex_type Thread_create_mutex(void); 62 DLLExport int Thread_lock_mutex(mutex_type); 63 DLLExport int Thread_unlock_mutex(mutex_type); 64 void Thread_destroy_mutex(mutex_type); 65 66 thread_id_type Thread_getid(void); 67 68 sem_type Thread_create_sem(void); 69 int Thread_wait_sem(sem_type sem, int timeout); 70 int Thread_check_sem(sem_type sem); 71 int Thread_post_sem(sem_type sem); 72 int Thread_destroy_sem(sem_type sem); 73 74 75 #endif 76