tor  0.4.0.0-alpha-dev
compat_threads.c
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2018, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
14 #include "orconfig.h"
15 #include <stdlib.h>
16 #include "lib/thread/threads.h"
17 #include "lib/thread/thread_sys.h"
18 
19 #include "lib/log/log.h"
20 #include "lib/log/util_bug.h"
21 #include "lib/subsys/subsys.h"
22 
23 #include <string.h>
24 
26 tor_cond_t *
28 {
29  tor_cond_t *cond = tor_malloc(sizeof(tor_cond_t));
30  if (BUG(tor_cond_init(cond)<0))
31  tor_free(cond); // LCOV_EXCL_LINE
32  return cond;
33 }
34 
36 void
38 {
39  if (!c)
40  return;
41  tor_cond_uninit(c);
42  tor_free(c);
43 }
44 
46 static unsigned long main_thread_id = -1;
47 
50 void
52 {
54 }
56 int
58 {
60 }
61 
62 #ifndef HAVE_WORKING_STDATOMIC
63 
64 void
66 {
67  memset(counter, 0, sizeof(*counter));
68  tor_mutex_init_nonrecursive(&counter->mutex);
69 }
71 void
73 {
74  tor_mutex_uninit(&counter->mutex);
75  memset(counter, 0, sizeof(*counter));
76 }
78 void
79 atomic_counter_add(atomic_counter_t *counter, size_t add)
80 {
81  tor_mutex_acquire(&counter->mutex);
82  counter->val += add;
83  tor_mutex_release(&counter->mutex);
84 }
86 void
87 atomic_counter_sub(atomic_counter_t *counter, size_t sub)
88 {
89  // this relies on unsigned overflow, but that's fine.
90  atomic_counter_add(counter, -sub);
91 }
93 size_t
95 {
96  size_t val;
97  tor_mutex_acquire(&counter->mutex);
98  val = counter->val;
99  tor_mutex_release(&counter->mutex);
100  return val;
101 }
103 size_t
105 {
106  size_t oldval;
107  tor_mutex_acquire(&counter->mutex);
108  oldval = counter->val;
109  counter->val = newval;
110  tor_mutex_release(&counter->mutex);
111  return oldval;
112 }
113 #endif /* !defined(HAVE_WORKING_STDATOMIC) */
114 
115 static int
116 subsys_threads_initialize(void)
117 {
119  return 0;
120 }
121 
122 const subsys_fns_t sys_threads = {
123  .name = "threads",
124  .supported = true,
125  .level = -95,
126  .initialize = subsys_threads_initialize,
127 };
size_t atomic_counter_get(atomic_counter_t *counter)
void tor_mutex_release(tor_mutex_t *m)
void tor_cond_uninit(tor_cond_t *cond)
void atomic_counter_init(atomic_counter_t *counter)
size_t atomic_counter_exchange(atomic_counter_t *counter, size_t newval)
tor_cond_t * tor_cond_new(void)
#define tor_free(p)
Definition: malloc.h:52
void tor_threads_init(void)
void tor_mutex_init_nonrecursive(tor_mutex_t *m)
int in_main_thread(void)
void tor_mutex_uninit(tor_mutex_t *m)
Header for threads.c.
void atomic_counter_sub(atomic_counter_t *counter, size_t sub)
void atomic_counter_add(atomic_counter_t *counter, size_t add)
void set_main_thread(void)
int tor_cond_init(tor_cond_t *cond)
static unsigned long main_thread_id
void tor_mutex_acquire(tor_mutex_t *m)
void tor_cond_free_(tor_cond_t *c)
const char * name
Definition: subsys.h:28
void atomic_counter_destroy(atomic_counter_t *counter)
Headers for log.c.
unsigned long tor_get_thread_id(void)
Macros to manage assertions, fatal and non-fatal.