thread_wrapper.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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_CREATE_PARAM(handle, callback, param) pthread_create(&(handle), NULL, callback, (void*) &(param))
  11. #define THREAD_JOIN(handle) pthread_join(handle, NULL)
  12. #define THREAD_CALLBACK(name) static void * name(void *_)
  13. #define THREAD_CALLBACK_PARAM(name, param) static void * name(void *param)
  14. #else
  15. #include <windows.h>
  16. #define THREAD_HANDLE HANDLE
  17. #define THREAD_CREATE(handle, callback) { handle = CreateThread( NULL, 0, callback, NULL, 0, NULL); }
  18. #define THREAD_CREATE_PARAM(handle, callback, param) { handle = CreateThread( NULL, 0, callback, (LPVOID) &param, 0, NULL); }
  19. #define THREAD_JOIN(handle) WaitForSingleObject(handle, INFINITE)
  20. #define THREAD_CALLBACK(name) static DWORD WINAPI name( LPVOID lpParam )
  21. #define THREAD_CALLBACK_PARAM(name, param) static DWORD WINAPI name( LPVOID param )
  22. #endif
  23. /* Mutex */
  24. /* Windows returns non-zero on success and pthread returns zero,
  25. * so compare to zero to achieve consistent return values */
  26. #ifndef WIN32
  27. #define MUTEX_HANDLE pthread_mutex_t
  28. /* Will return UA_TRUE when zero */
  29. #define MUTEX_INIT(name) (pthread_mutex_init(&(name), NULL) == 0)
  30. #define MUTEX_LOCK(name) (pthread_mutex_lock(&(name)) == 0)
  31. #define MUTEX_UNLOCK(name) (pthread_mutex_unlock(&(name)) == 0)
  32. #define MUTEX_DESTROY(name) (pthread_mutex_destroy(&(name)) == 0)
  33. #else
  34. #define MUTEX_HANDLE HANDLE
  35. /* Will return UA_FALSE when zero */
  36. #define MUTEX_INIT(name) (CreateMutex(NULL, FALSE, NULL) != 0)
  37. #define MUTEX_LOCK(name) (WaitForSingleObject((name), INFINITE) != 0)
  38. #define MUTEX_UNLOCK(name) (ReleaseMutex((name)) != 0)
  39. #define MUTEX_DESTROY(name) (CloseHandle((name)) != 0)
  40. #endif