compat_mutex.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef TOR_COMPAT_MUTEX_H
  6. #define TOR_COMPAT_MUTEX_H
  7. #include "orconfig.h"
  8. #include "lib/cc/torint.h"
  9. #include "lib/malloc/util_malloc.h"
  10. #if defined(HAVE_PTHREAD_H) && !defined(_WIN32)
  11. #include <pthread.h>
  12. #endif
  13. #if defined(_WIN32)
  14. #include <windows.h>
  15. #endif
  16. #if defined(_WIN32)
  17. #define USE_WIN32_THREADS
  18. #elif defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_CREATE)
  19. #define USE_PTHREADS
  20. #else
  21. #error "No threading system was found"
  22. #endif /* defined(_WIN32) || ... */
  23. /* Because we use threads instead of processes on most platforms (Windows,
  24. * Linux, etc), we need locking for them. On platforms with poor thread
  25. * support or broken gethostbyname_r, these functions are no-ops. */
  26. /** A generic lock structure for multithreaded builds. */
  27. typedef struct tor_mutex_t {
  28. #if defined(USE_WIN32_THREADS)
  29. /** Windows-only: on windows, we implement locks with CRITICAL_SECTIONS. */
  30. CRITICAL_SECTION mutex;
  31. #elif defined(USE_PTHREADS)
  32. /** Pthreads-only: with pthreads, we implement locks with
  33. * pthread_mutex_t. */
  34. pthread_mutex_t mutex;
  35. #else
  36. /** No-threads only: Dummy variable so that tor_mutex_t takes up space. */
  37. int _unused;
  38. #endif /* defined(USE_WIN32_MUTEX) || ... */
  39. } tor_mutex_t;
  40. tor_mutex_t *tor_mutex_new(void);
  41. tor_mutex_t *tor_mutex_new_nonrecursive(void);
  42. void tor_mutex_init(tor_mutex_t *m);
  43. void tor_mutex_init_nonrecursive(tor_mutex_t *m);
  44. void tor_mutex_acquire(tor_mutex_t *m);
  45. void tor_mutex_release(tor_mutex_t *m);
  46. void tor_mutex_free_(tor_mutex_t *m);
  47. #define tor_mutex_free(m) FREE_AND_NULL(tor_mutex_t, tor_mutex_free_, (m))
  48. void tor_mutex_uninit(tor_mutex_t *m);
  49. void tor_locking_init(void);
  50. #endif /* !defined(TOR_COMPAT_MUTEX_H) */