Thread.h 2.3 KB

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