[svn:parrot] r48672 - in branches/gsoc_threads: include/parrot src
Chandon at svn.parrot.org
Chandon at svn.parrot.org
Thu Aug 26 17:19:02 UTC 2010
Author: Chandon
Date: Thu Aug 26 17:19:01 2010
New Revision: 48672
URL: https://trac.parrot.org/parrot/changeset/48672
Log:
[gsoc_threads] Use thread local storage for thread id.
Modified:
branches/gsoc_threads/include/parrot/interpreter.h
branches/gsoc_threads/include/parrot/thr_pthread.h
branches/gsoc_threads/include/parrot/thread.h
branches/gsoc_threads/include/parrot/threads.h
branches/gsoc_threads/src/scheduler.c
branches/gsoc_threads/src/threads.c
Modified: branches/gsoc_threads/include/parrot/interpreter.h
==============================================================================
--- branches/gsoc_threads/include/parrot/interpreter.h Thu Aug 26 16:16:21 2010 (r48671)
+++ branches/gsoc_threads/include/parrot/interpreter.h Thu Aug 26 17:19:01 2010 (r48672)
@@ -273,7 +273,6 @@
PMC *current_task; /* there's always one running task */
Parrot_mutex interp_lock; /* Enforce one running thread per interp */
- INTVAL active_thread; /* Index of the active thread in thread_table */
struct Thread_table *thread_table; /* Array of this interpreter's threads */
Parrot_atomic_integer thread_signal; /* Flag. Set if threads need rescheduling */
Modified: branches/gsoc_threads/include/parrot/thr_pthread.h
==============================================================================
--- branches/gsoc_threads/include/parrot/thr_pthread.h Thu Aug 26 16:16:21 2010 (r48671)
+++ branches/gsoc_threads/include/parrot/thr_pthread.h Thu Aug 26 17:19:01 2010 (r48672)
@@ -60,9 +60,16 @@
# define THREAD_EXIT(s) pthread_exit((void*) (s))
+# define Parrot_tls_key pthread_key_t
+# define TLS_KEY_INIT(k) assert(pthread_key_create(&(k), free) == 0)
+# define TLS_KEY_FREE(k) assert(pthread_key_delete(k) == 0)
+# define TLS_SET(k, v) assert(pthread_setspecific(k, v) == 0)
+# define TLS_GET(k) pthread_getspecific(k)
+
typedef pthread_mutex_t Parrot_mutex;
typedef pthread_cond_t Parrot_cond;
typedef pthread_t Parrot_thread;
+typedef pthread_key_t Parrot_tls_key;
typedef void (*Cleanup_Handler)(void *);
Modified: branches/gsoc_threads/include/parrot/thread.h
==============================================================================
--- branches/gsoc_threads/include/parrot/thread.h Thu Aug 26 16:16:21 2010 (r48671)
+++ branches/gsoc_threads/include/parrot/thread.h Thu Aug 26 17:19:01 2010 (r48672)
@@ -42,6 +42,12 @@
# define THREAD_EXIT(s) exit(s)
+# define TLS_KEY_INIT(k)
+# define TLS_KEY_FREE(k)
+# define TLS_SET(k, v)
+# define TLS_GET(k)
+
+# define Parrot_tls_key int
# define Parrot_mutex int
# define Parrot_cond int
# define Parrot_thread int
Modified: branches/gsoc_threads/include/parrot/threads.h
==============================================================================
--- branches/gsoc_threads/include/parrot/threads.h Thu Aug 26 16:16:21 2010 (r48671)
+++ branches/gsoc_threads/include/parrot/threads.h Thu Aug 26 17:19:01 2010 (r48672)
@@ -31,9 +31,10 @@
} Thread_info;
typedef struct Thread_table {
- Thread_info *threads;
- INTVAL size;
- INTVAL count;
+ Thread_info *threads;
+ INTVAL size;
+ INTVAL count;
+ Parrot_tls_key tid_key;
} Thread_table;
typedef struct Thread_args {
Modified: branches/gsoc_threads/src/scheduler.c
==============================================================================
--- branches/gsoc_threads/src/scheduler.c Thu Aug 26 16:16:21 2010 (r48671)
+++ branches/gsoc_threads/src/scheduler.c Thu Aug 26 17:19:01 2010 (r48672)
@@ -232,7 +232,7 @@
Parrot_cx_run_scheduler(PARROT_INTERP, ARGMOD(PMC *scheduler), ARGIN(opcode_t *next))
{
ASSERT_ARGS(Parrot_cx_run_scheduler)
- INTVAL tid = interp->active_thread;
+ INTVAL tid = Parrot_threads_current(interp);
Parrot_cx_check_alarms(interp, scheduler);
Parrot_cx_check_quantum(interp, scheduler);
Modified: branches/gsoc_threads/src/threads.c
==============================================================================
--- branches/gsoc_threads/src/threads.c Thu Aug 26 16:16:21 2010 (r48671)
+++ branches/gsoc_threads/src/threads.c Thu Aug 26 17:19:01 2010 (r48672)
@@ -40,6 +40,8 @@
{
ASSERT_ARGS(Parrot_threads_init)
Thread_table *tbl;
+ INTVAL *zero = (INTVAL*) malloc(sizeof(INTVAL));
+ *zero = 0;
MUTEX_INIT(interp->interp_lock);
PARROT_ATOMIC_INT_INIT(interp->thread_signal);
@@ -48,13 +50,13 @@
MUTEX_INIT(interp->thread_lock);
interp->thread_table = (Thread_table*) malloc(sizeof(Thread_table));
- interp->active_thread = 0;
-
tbl = interp->thread_table;
tbl->size = 8; /* arbitrarily */
tbl->count = 1;
tbl->threads = (Thread_info*) malloc(sizeof(Thread_info) * tbl->size);
+ TLS_KEY_INIT(tbl->tid_key);
+ TLS_SET(tbl->tid_key, zero);
tbl->threads[0].id = THREAD_SELF();
tbl->threads[0].state = 0;
@@ -83,6 +85,7 @@
COND_DESTROY(tbl->threads[0].cvar);
free(tbl->threads);
+ TLS_KEY_FREE(tbl->tid_key);
free(interp->thread_table);
}
@@ -105,18 +108,21 @@
ASSERT_ARGS(Parrot_threads_main)
Thread_args *args = (Thread_args*) args_ptr;
Interp *interp = args->interp;
- INTVAL tidx = args->idx;
+ INTVAL *tidx = (INTVAL*) malloc(sizeof(INTVAL));
Thread_table *tbl = interp->thread_table;
free(args_ptr);
Parrot_alarm_mask(interp);
+ *tidx = args->idx;
/* Yay stack scanning */
LOCK(interp->thread_lock);
- tbl->threads[tidx].lo_var_ptr = &tidx;
+ tbl->threads[*tidx].lo_var_ptr = &tidx;
UNLOCK(interp->thread_lock);
- Parrot_threads_outer_runloop(interp, tidx);
+ TLS_SET(tbl->tid_key, tidx);
+
+ Parrot_threads_outer_runloop(interp, *tidx);
return 0;
}
@@ -143,7 +149,6 @@
for (;;) {
LOCK_INTERP(interp);
- interp->active_thread = tidx;
interp->current_runloop_level = 0;
LOCK(interp->thread_lock);
@@ -192,9 +197,8 @@
Thread_table *tbl = interp->thread_table;
int next, i, have_runnable;
- *tidx = interp->active_thread;
-
- next = Parrot_threads_next_to_run(interp, *tidx);
+ *tidx = Parrot_threads_current(interp);
+ next = Parrot_threads_next_to_run(interp, *tidx);
LOCK(interp->thread_lock);
@@ -249,7 +253,6 @@
LOCK(interp->thread_lock);
tbl = interp->thread_table;
- interp->active_thread = tidx;
THREAD_STATE_SET(interp, tidx, RESTLESS);
UNLOCK(interp->thread_lock);
@@ -351,10 +354,11 @@
{
ASSERT_ARGS(Parrot_threads_reap)
Thread_table *tbl = interp->thread_table;
+ INTVAL tidx = Parrot_threads_current(interp);
int i;
LOCK(interp->thread_lock);
- for (i = tbl->count - 1; i > interp->active_thread; --i) {
+ for (i = tbl->count - 1; i > tidx; --i) {
void *rv;
if (THREAD_STATE_TEST(interp, i, SCAN_STACK))
@@ -409,7 +413,6 @@
Thread_table *tbl = interp->thread_table;
THREAD_STATE_SET(interp, tidx, RUNNABLE);
COND_WAIT(tbl->threads[tidx].cvar, interp->interp_lock);
- interp->active_thread = tidx;
}
/*
@@ -473,21 +476,9 @@
Parrot_threads_current(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_threads_current)
-
- INTVAL idx;
Thread_table *tbl = interp->thread_table;
- Parrot_thread tid = THREAD_SELF();
-
- LOCK(interp->thread_lock);
- for(idx = 0; idx < tbl->count; ++idx) {
- if (THREAD_EQUAL(tbl->threads[idx].id, tid)) {
- UNLOCK(interp->thread_lock);
- return idx;
- }
- }
- UNLOCK(interp->thread_lock);
-
- exit_fatal(1, "threads.c: Current thread is not in the threads table.");
+ INTVAL *tidp = (INTVAL*) TLS_GET(tbl->tid_key);
+ return *tidp;
}
/*
More information about the parrot-commits
mailing list