Thread.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #if !defined(THREAD_H)
  20. #define THREAD_H
  21. #if !defined(_WIN32) && !defined(_WIN64)
  22. #if defined(__GNUC__) && defined(__linux__)
  23. #if !defined(_GNU_SOURCE)
  24. // for pthread_setname
  25. #define _GNU_SOURCE
  26. #endif
  27. #endif
  28. #endif
  29. #include "MQTTExportDeclarations.h"
  30. #include "MQTTClient.h"
  31. #include "mutex_type.h" /* Needed for mutex_type */
  32. #if defined(_WIN32) || defined(_WIN64)
  33. #include <windows.h>
  34. #define thread_type HANDLE
  35. #define thread_id_type DWORD
  36. #define thread_return_type DWORD
  37. #define thread_fn LPTHREAD_START_ROUTINE
  38. #define cond_type HANDLE
  39. #define sem_type HANDLE
  40. #undef ETIMEDOUT
  41. #define ETIMEDOUT WSAETIMEDOUT
  42. #else
  43. #include <pthread.h>
  44. #define thread_type pthread_t
  45. #define thread_id_type pthread_t
  46. #define thread_return_type void*
  47. typedef thread_return_type (*thread_fn)(void*);
  48. typedef struct { pthread_cond_t cond; pthread_mutex_t mutex; } cond_type_struct;
  49. typedef cond_type_struct *cond_type;
  50. #if defined(OSX)
  51. #include <dispatch/dispatch.h>
  52. typedef dispatch_semaphore_t sem_type;
  53. #else
  54. #include <semaphore.h>
  55. typedef sem_t *sem_type;
  56. #endif
  57. cond_type Thread_create_cond(int*);
  58. int Thread_signal_cond(cond_type);
  59. int Thread_wait_cond(cond_type condvar, int timeout);
  60. int Thread_destroy_cond(cond_type);
  61. #endif
  62. LIBMQTT_API void Thread_start(thread_fn, void*);
  63. int Thread_set_name(const char* thread_name);
  64. LIBMQTT_API mutex_type Thread_create_mutex(int*);
  65. LIBMQTT_API int Thread_lock_mutex(mutex_type);
  66. LIBMQTT_API int Thread_unlock_mutex(mutex_type);
  67. int Thread_destroy_mutex(mutex_type);
  68. LIBMQTT_API thread_id_type Thread_getid();
  69. sem_type Thread_create_sem(int*);
  70. int Thread_wait_sem(sem_type sem, int timeout);
  71. int Thread_check_sem(sem_type sem);
  72. int Thread_post_sem(sem_type sem);
  73. int Thread_destroy_sem(sem_type sem);
  74. #endif