aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2014-02-07 13:13:15 -0500
committerNick Mathewson <nickm@torproject.org>2014-02-07 13:13:15 -0500
commit1ebdaf5788cb7969ee3f853ede4e6e3364343fc0 (patch)
treed38de02c12628bcfb4f10dd44d5a39d390f30414 /src/common
parent85b46d57bcc40b8053dafe5d0ebb4b0bb611b484 (diff)
downloadtor-1ebdaf5788cb7969ee3f853ede4e6e3364343fc0.tar
tor-1ebdaf5788cb7969ee3f853ede4e6e3364343fc0.tar.gz
More hacking around spawn_func issues
This time, we use a pthread_attr to make sure that if pthread_create succeeds, the thread is successfully detached. This probably isn't the big thing going on with 4345, since it'd be a bit weird for pthread_detach to be failing. But it's worth getting it right.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/compat.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index d88c5f92d..8a3655e19 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -2248,6 +2248,12 @@ tor_pthread_helper_fn(void *_data)
func(arg);
return NULL;
}
+/**
+ * A pthread attribute to make threads start detached.
+ */
+static pthread_attr_t attr_detached;
+/** True iff we've called tor_threads_init() */
+static int threads_initialized = 0;
#endif
/** Minimalist interface to run a void function in the background. On
@@ -2271,12 +2277,12 @@ spawn_func(void (*func)(void *), void *data)
#elif defined(USE_PTHREADS)
pthread_t thread;
tor_pthread_data_t *d;
+ if (PREDICT_UNLIKELY(!threads_initialized))
+ tor_threads_init();
d = tor_malloc(sizeof(tor_pthread_data_t));
d->data = data;
d->func = func;
- if (pthread_create(&thread,NULL,tor_pthread_helper_fn,d))
- return -1;
- if (pthread_detach(thread))
+ if (pthread_create(&thread,&attr_detached,tor_pthread_helper_fn,d))
return -1;
return 0;
#else
@@ -2633,8 +2639,6 @@ tor_get_thread_id(void)
* "reentrant" mutexes (i.e., once we can re-lock if we're already holding
* them.) */
static pthread_mutexattr_t attr_reentrant;
-/** True iff we've called tor_threads_init() */
-static int threads_initialized = 0;
/** Initialize <b>mutex</b> so it can be locked. Every mutex must be set
* up with tor_mutex_init() or tor_mutex_new(); not both. */
void
@@ -2778,6 +2782,8 @@ tor_threads_init(void)
if (!threads_initialized) {
pthread_mutexattr_init(&attr_reentrant);
pthread_mutexattr_settype(&attr_reentrant, PTHREAD_MUTEX_RECURSIVE);
+ tor_assert(0==pthread_attr_init(&attr_detached));
+ tor_assert(0==pthread_attr_setdetachstate(&attr_detached, 1));
threads_initialized = 1;
set_main_thread();
}