);
s = buf;
- if (!thread_current)
+ struct thread *tc;
+ tc = pthread_getspecific (thread_current);
+ if (!tc)
s = str_append (LOC, "no thread information available\n");
else
{
s = str_append (LOC, "in thread ");
- s = str_append (LOC, thread_current->funcname);
+ s = str_append (LOC, tc->funcname);
s = str_append (LOC, " scheduled from ");
- s = str_append (LOC, thread_current->schedfrom);
+ s = str_append (LOC, tc->schedfrom);
s = str_append (LOC, ":");
- s = num_append (LOC, thread_current->schedfrom_line);
+ s = num_append (LOC, tc->schedfrom_line);
s = str_append (LOC, "\n");
}
void zlog_thread_info (int log_level)
{
- if (thread_current)
+ struct thread *tc;
+ tc = pthread_getspecific (thread_current);
+
+ if (tc)
zlog(log_level, "Current thread function %s, scheduled from "
- "file %s, line %u", thread_current->funcname,
- thread_current->schedfrom, thread_current->schedfrom_line);
+ "file %s, line %u", tc->funcname,
+ tc->schedfrom, tc->schedfrom_line);
else
zlog(log_level, "Current thread not known/applicable");
}
write (m->io_pipe[1], &wakebyte, 1); \
} while (0);
+pthread_once_t init_once = PTHREAD_ONCE_INIT;
static pthread_mutex_t cpu_record_mtx = PTHREAD_MUTEX_INITIALIZER;
static struct hash *cpu_record = NULL;
+pthread_key_t thread_current;
static unsigned long
timeval_elapsed (struct timeval a, struct timeval b)
XFREE (MTYPE_TMP, cr);
}
+/* initializer, only ever called once */
+static void initializer ()
+{
+ if (cpu_record == NULL)
+ cpu_record = hash_create ((unsigned int (*) (void *))cpu_record_hash_key,
+ (int (*) (const void *, const void *))
+ cpu_record_hash_cmp);
+
+ pthread_key_create (&thread_current, NULL);
+}
+
/* Allocate new thread master. */
struct thread_master *
thread_master_create (void)
getrlimit(RLIMIT_NOFILE, &limit);
- pthread_mutex_lock (&cpu_record_mtx);
- {
- if (cpu_record == NULL)
- cpu_record = hash_create ((unsigned int (*) (void *))cpu_record_hash_key,
- (int (*) (const void *, const void *))
- cpu_record_hash_cmp);
- }
- pthread_mutex_unlock (&cpu_record_mtx);
+ pthread_once (&init_once, &initializer);
rv = XCALLOC (MTYPE_THREAD_MASTER, sizeof (struct thread_master));
if (rv == NULL)
listnode_add (thread->master->cancel_req, cr);
do_thread_cancel (thread->master);
}
+done:
pthread_mutex_unlock (&thread->master->mtx);
}
getrusage(RUSAGE_SELF, &(r->cpu));
}
-struct thread *thread_current = NULL;
-
/* We check thread consumed time. If the system has getrusage, we'll
use that to get in-depth stats on the performance of the thread in addition
to wall clock time stats from gettimeofday. */
GETRUSAGE (&before);
thread->real = before.real;
- thread_current = thread;
+ pthread_setspecific (thread_current, thread);
(*thread->func) (thread);
- thread_current = NULL;
+ pthread_setspecific (thread_current, NULL);
GETRUSAGE (&after);