compat_mutex.h 1.9 KB

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