compat_threads.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef TOR_COMPAT_THREADS_H
  6. #define TOR_COMPAT_THREADS_H
  7. #include "orconfig.h"
  8. #include "torint.h"
  9. #include "testsupport.h"
  10. #if defined(HAVE_PTHREAD_H) && !defined(_WIN32)
  11. #include <pthread.h>
  12. #endif
  13. #if defined(_WIN32)
  14. #define USE_WIN32_THREADS
  15. #elif defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_CREATE)
  16. #define USE_PTHREADS
  17. #else
  18. #error "No threading system was found"
  19. #endif
  20. int spawn_func(void (*func)(void *), void *data);
  21. void spawn_exit(void) ATTR_NORETURN;
  22. /* Because we use threads instead of processes on most platforms (Windows,
  23. * Linux, etc), we need locking for them. On platforms with poor thread
  24. * support or broken gethostbyname_r, these functions are no-ops. */
  25. /** A generic lock structure for multithreaded builds. */
  26. typedef struct tor_mutex_t {
  27. #if defined(USE_WIN32_THREADS)
  28. /** Windows-only: on windows, we implement locks with CRITICAL_SECTIONS. */
  29. CRITICAL_SECTION mutex;
  30. #elif defined(USE_PTHREADS)
  31. /** Pthreads-only: with pthreads, we implement locks with
  32. * pthread_mutex_t. */
  33. pthread_mutex_t mutex;
  34. #else
  35. /** No-threads only: Dummy variable so that tor_mutex_t takes up space. */
  36. int _unused;
  37. #endif
  38. } tor_mutex_t;
  39. tor_mutex_t *tor_mutex_new(void);
  40. void tor_mutex_init(tor_mutex_t *m);
  41. void tor_mutex_init_for_cond(tor_mutex_t *m);
  42. void tor_mutex_acquire(tor_mutex_t *m);
  43. void tor_mutex_release(tor_mutex_t *m);
  44. void tor_mutex_free(tor_mutex_t *m);
  45. void tor_mutex_uninit(tor_mutex_t *m);
  46. unsigned long tor_get_thread_id(void);
  47. void tor_threads_init(void);
  48. void set_main_thread(void);
  49. int in_main_thread(void);
  50. typedef struct tor_cond_t {
  51. #ifdef USE_PTHREADS
  52. pthread_cond_t cond;
  53. #elif defined(USE_WIN32_THREADS)
  54. HANDLE event;
  55. CRITICAL_SECTION lock;
  56. int n_waiting;
  57. int n_to_wake;
  58. int generation;
  59. #else
  60. #error no known condition implementation.
  61. #endif
  62. } tor_cond_t;
  63. tor_cond_t *tor_cond_new(void);
  64. void tor_cond_free(tor_cond_t *cond);
  65. int tor_cond_init(tor_cond_t *cond);
  66. void tor_cond_uninit(tor_cond_t *cond);
  67. int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
  68. const struct timeval *tv);
  69. void tor_cond_signal_one(tor_cond_t *cond);
  70. void tor_cond_signal_all(tor_cond_t *cond);
  71. /** Helper type used to manage waking up the main thread while it's in
  72. * the libevent main loop. Used by the work queue code. */
  73. typedef struct alert_sockets_s {
  74. /* XXXX This structure needs a better name. */
  75. /** Socket that the main thread should listen for EV_READ events on.
  76. * Note that this socket may be a regular fd on a non-Windows platform.
  77. */
  78. tor_socket_t read_fd;
  79. /** Socket to use when alerting the main thread. */
  80. tor_socket_t write_fd;
  81. /** Function to alert the main thread */
  82. int (*alert_fn)(tor_socket_t write_fd);
  83. /** Function to make the main thread no longer alerted. */
  84. int (*drain_fn)(tor_socket_t read_fd);
  85. } alert_sockets_t;
  86. int alert_sockets_create(alert_sockets_t *socks_out);
  87. void alert_sockets_close(alert_sockets_t *socks);
  88. #endif