• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2022 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 v2.0
6  * and Eclipse Distribution License v1.0 which accompany this distribution.
7  *
8  * The Eclipse Public License is available at
9  *    https://www.eclipse.org/legal/epl-2.0/
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 
20 #if !defined(THREAD_H)
21 #define THREAD_H
22 
23 #if !defined(_WIN32) && !defined(_WIN64)
24 #if defined(__GNUC__) && defined(__linux__)
25 #if !defined(_GNU_SOURCE)
26 // for pthread_setname
27 	#define _GNU_SOURCE
28 #endif
29 #endif
30 #endif
31 
32 #include "MQTTExportDeclarations.h"
33 
34 #include "MQTTClient.h"
35 
36 #include "mutex_type.h" /* Needed for mutex_type */
37 
38 #if defined(_WIN32) || defined(_WIN64)
39 	#include <windows.h>
40 	#define thread_type HANDLE
41 	#define thread_id_type DWORD
42 	#define thread_return_type DWORD
43 	#define thread_fn LPTHREAD_START_ROUTINE
44 	#define cond_type HANDLE
45 	#define sem_type HANDLE
46 	#undef ETIMEDOUT
47 	#define ETIMEDOUT WSAETIMEDOUT
48 #elif defined(COMPAT_CMSIS)
49 	#include <cmsis_os2.h>
50 
51 	typedef osThreadId_t thread_type;
52 	typedef osThreadId_t thread_id_type;
53 	typedef osSemaphoreId_t sem_type;
54 	#define thread_return_type void*
55 	typedef thread_return_type (*thread_fn)(void*);
56 #else
57 	#include <pthread.h>
58 
59 	#define thread_type pthread_t
60 	#define thread_id_type pthread_t
61 	#define thread_return_type void*
62 	typedef thread_return_type (*thread_fn)(void*);
63 	typedef struct { pthread_cond_t cond; pthread_mutex_t mutex; } cond_type_struct;
64 	typedef cond_type_struct *cond_type;
65 	#if defined(OSX)
66 	  #include <dispatch/dispatch.h>
67 	  typedef dispatch_semaphore_t sem_type;
68 	#else
69 	  #include <semaphore.h>
70 	  typedef sem_t *sem_type;
71 	#endif
72 
73 	cond_type Thread_create_cond(int*);
74 	int Thread_signal_cond(cond_type);
75 	int Thread_wait_cond(cond_type condvar, int timeout);
76 	int Thread_destroy_cond(cond_type);
77 #endif
78 
79 LIBMQTT_API void Thread_start(thread_fn, void*);
80 int Thread_set_name(const char* thread_name);
81 
82 LIBMQTT_API mutex_type Thread_create_mutex(int*);
83 LIBMQTT_API int Thread_lock_mutex(mutex_type);
84 LIBMQTT_API int Thread_unlock_mutex(mutex_type);
85 int Thread_destroy_mutex(mutex_type);
86 
87 #if defined(IOT_CONNECT) || defined(IOT_LITEOS_ADAPT)
88 LIBMQTT_API thread_id_type Thread_getid(void);
89 #else
90 LIBMQTT_API thread_id_type Thread_getid();
91 #endif
92 
93 sem_type Thread_create_sem(int*);
94 int Thread_wait_sem(sem_type sem, int timeout);
95 int Thread_check_sem(sem_type sem);
96 int Thread_post_sem(sem_type sem);
97 int Thread_destroy_sem(sem_type sem);
98 
99 
100 #endif
101