compat_threads.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #include "compat.h"
  6. #include "util.h"
  7. /** Return a newly allocated, ready-for-use mutex. */
  8. tor_mutex_t *
  9. tor_mutex_new(void)
  10. {
  11. tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t));
  12. tor_mutex_init(m);
  13. return m;
  14. }
  15. /** Release all storage and system resources held by <b>m</b>. */
  16. void
  17. tor_mutex_free(tor_mutex_t *m)
  18. {
  19. if (!m)
  20. return;
  21. tor_mutex_uninit(m);
  22. tor_free(m);
  23. }
  24. tor_cond_t *
  25. tor_cond_new(void)
  26. {
  27. tor_cond_t *cond = tor_malloc(sizeof(tor_cond_t));
  28. if (tor_cond_init(cond)<0)
  29. tor_free(cond);
  30. return cond;
  31. }
  32. void
  33. tor_cond_free(tor_cond_t *c)
  34. {
  35. if (!c)
  36. return;
  37. tor_cond_uninit(c);
  38. tor_free(c);
  39. }
  40. /** Identity of the "main" thread */
  41. static unsigned long main_thread_id = -1;
  42. /** Start considering the current thread to be the 'main thread'. This has
  43. * no effect on anything besides in_main_thread(). */
  44. void
  45. set_main_thread(void)
  46. {
  47. main_thread_id = tor_get_thread_id();
  48. }
  49. /** Return true iff called from the main thread. */
  50. int
  51. in_main_thread(void)
  52. {
  53. return main_thread_id == tor_get_thread_id();
  54. }