compat_mutex.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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.c
  7. *
  8. * \brief Portable wrapper for platform mutex implementations.
  9. **/
  10. #include "lib/lock/compat_mutex.h"
  11. #include "lib/malloc/malloc.h"
  12. /** Return a newly allocated, ready-for-use mutex. */
  13. tor_mutex_t *
  14. tor_mutex_new(void)
  15. {
  16. tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t));
  17. tor_mutex_init(m);
  18. return m;
  19. }
  20. /** Return a newly allocated, ready-for-use mutex. This one might be
  21. * non-recursive, if that's faster. */
  22. tor_mutex_t *
  23. tor_mutex_new_nonrecursive(void)
  24. {
  25. tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t));
  26. tor_mutex_init_nonrecursive(m);
  27. return m;
  28. }
  29. /** Release all storage and system resources held by <b>m</b>.
  30. *
  31. * Destroying a locked mutex is undefined behaviour. Global mutexes may be
  32. * locked when they are passed to this function, because multiple threads can
  33. * still access them. So we can either:
  34. * - destroy on shutdown, and re-initialise when tor re-initialises, or
  35. * - skip destroying and re-initialisation, using a sentinel variable.
  36. * See #31735 for details.
  37. */
  38. void
  39. tor_mutex_free_(tor_mutex_t *m)
  40. {
  41. if (!m)
  42. return;
  43. tor_mutex_uninit(m);
  44. tor_free(m);
  45. }