Thread.h 2.3 KB

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