Line data Source code
1 : /* Copyright (c) 2003-2004, Roger Dingledine 2 : * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. 3 : * Copyright (c) 2007-2021, The Tor Project, Inc. */ 4 : /* See LICENSE for licensing information */ 5 : 6 : /** 7 : * \file compat_mutex.c 8 : * 9 : * \brief Portable wrapper for platform mutex implementations. 10 : **/ 11 : 12 : #include "lib/lock/compat_mutex.h" 13 : #include "lib/malloc/malloc.h" 14 : 15 : /** Return a newly allocated, ready-for-use mutex. */ 16 : tor_mutex_t * 17 91 : tor_mutex_new(void) 18 : { 19 91 : tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t)); 20 91 : tor_mutex_init(m); 21 91 : return m; 22 : } 23 : /** Return a newly allocated, ready-for-use mutex. This one might be 24 : * non-recursive, if that's faster. */ 25 : tor_mutex_t * 26 2 : tor_mutex_new_nonrecursive(void) 27 : { 28 2 : tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t)); 29 2 : tor_mutex_init_nonrecursive(m); 30 2 : return m; 31 : } 32 : /** Release all storage and system resources held by <b>m</b>. 33 : * 34 : * Destroying a locked mutex is undefined behaviour. Global mutexes may be 35 : * locked when they are passed to this function, because multiple threads can 36 : * still access them. So we can either: 37 : * - destroy on shutdown, and re-initialise when tor re-initialises, or 38 : * - skip destroying and re-initialisation, using a sentinel variable. 39 : * See #31735 for details. 40 : */ 41 : void 42 272 : tor_mutex_free_(tor_mutex_t *m) 43 : { 44 272 : if (!m) 45 : return; 46 60 : tor_mutex_uninit(m); 47 60 : tor_free(m); 48 : }