compat_threads.c 1003 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. /** Identity of the "main" thread */
  25. static unsigned long main_thread_id = -1;
  26. /** Start considering the current thread to be the 'main thread'. This has
  27. * no effect on anything besides in_main_thread(). */
  28. void
  29. set_main_thread(void)
  30. {
  31. main_thread_id = tor_get_thread_id();
  32. }
  33. /** Return true iff called from the main thread. */
  34. int
  35. in_main_thread(void)
  36. {
  37. return main_thread_id == tor_get_thread_id();
  38. }