thread_wrapper.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /* Simple wrapper for unit test threads */
  5. /* Threads */
  6. #ifndef WIN32
  7. #include <pthread.h>
  8. #define THREAD_HANDLE pthread_t
  9. #define THREAD_CREATE(handle, callback) pthread_create(&handle, NULL, callback, NULL)
  10. #define THREAD_JOIN(handle) pthread_join(handle, NULL)
  11. #define THREAD_CALLBACK(name) static void * name(void *_)
  12. #else
  13. #include <windows.h>
  14. #define THREAD_HANDLE HANDLE
  15. #define THREAD_CREATE(handle, callback) { handle = CreateThread( NULL, 0, callback, NULL, 0, NULL); }
  16. #define THREAD_JOIN(handle) WaitForSingleObject(handle, INFINITE)
  17. #define THREAD_CALLBACK(name) static DWORD WINAPI name( LPVOID lpParam )
  18. #endif
  19. /* Mutex */
  20. /* Windows returns non-zero on success and pthread returns zero,
  21. * so compare to zero to achieve consistent return values */
  22. #ifndef WIN32
  23. #define MUTEX_HANDLE pthread_mutex_t
  24. /* Will return UA_TRUE when zero */
  25. #define MUTEX_INIT(name) (pthread_mutex_init(&(name), NULL) == 0)
  26. #define MUTEX_LOCK(name) (pthread_mutex_lock(&(name)) == 0)
  27. #define MUTEX_UNLOCK(name) (pthread_mutex_unlock(&(name)) == 0)
  28. #define MUTEX_DESTROY(name) (pthread_mutex_destroy(&(name)) == 0)
  29. #else
  30. #define MUTEX_HANDLE HANDLE
  31. /* Will return UA_FALSE when zero */
  32. #define MUTEX_INIT(name) (CreateMutex(NULL, FALSE, NULL) != 0)
  33. #define MUTEX_LOCK(name) (WaitForSingleObject((name), INFINITE) != 0)
  34. #define MUTEX_UNLOCK(name) (ReleaseMutex((name)) != 0)
  35. #define MUTEX_DESTROY(name) (CloseHandle((name)) != 0)
  36. #endif