aboutsummaryrefslogtreecommitdiffstats
path: root/thread_pthread.ci
diff options
context:
space:
mode:
authorko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-02-08 20:24:55 +0000
committerko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-02-08 20:24:55 +0000
commita9026242f2292cc7df06628652f4254c55c37e22 (patch)
tree958b2e49be9f42936803085f5ccadc41d75b2c49 /thread_pthread.ci
parent990ae267cdc73f954a3fa0b43135f2bbec9f76e8 (diff)
downloadruby-a9026242f2292cc7df06628652f4254c55c37e22.tar.gz
* thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
move cleanup function to thread_start_func_2(). * thread.c, thread_pthread.ci, thread_win32.ci: add more destruct functions. (native_thread_destroy() and native_mutex_destroy()) * thread_pthread.ci, thread_pthread.h: make native_mutex_* functions (check error, etc), it's not macro any more. * thread_win32.ci (thread_start_func_1): store some values before running thread (to release these after running thread). * thread_win32.ci (native_thread_create): fix spaces. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11671 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'thread_pthread.ci')
-rw-r--r--thread_pthread.ci89
1 files changed, 71 insertions, 18 deletions
diff --git a/thread_pthread.ci b/thread_pthread.ci
index 56abaa7d57..20f24b0b91 100644
--- a/thread_pthread.ci
+++ b/thread_pthread.ci
@@ -12,10 +12,59 @@
#ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
-#define native_mutex_initialize(lock) do { \
- pthread_mutex_t _lock = PTHREAD_MUTEX_INITIALIZER; \
- ((*lock) = _lock); \
-} while (0)
+void
+native_mutex_lock(pthread_mutex_t *lock)
+{
+ int r;
+ r = pthread_mutex_trylock(lock);
+ if (r == EBUSY) {
+ r = pthread_mutex_lock(lock);
+
+ if (r != 0) {
+ rb_bug("pthread_mutex_lock: %d", r);
+ }
+ }
+}
+
+void
+native_mutex_unlock(pthread_mutex_t *lock)
+{
+ int r;
+ if ((r = pthread_mutex_unlock(lock)) != 0) {
+ rb_bug("native_mutex_unlock return non-zero: %d", r);
+ }
+}
+
+inline int
+native_mutex_trylock(pthread_mutex_t *lock)
+{
+ int r;
+ if ((r = pthread_mutex_trylock(lock)) != 0) {
+ if (r == EBUSY) {
+ return EBUSY;
+ }
+ else {
+ rb_bug("native_mutex_unlock return non-zero: %d", r);
+ }
+ }
+ return 0;
+}
+
+void
+native_mutex_initialize(pthread_mutex_t *lock)
+{
+ if (pthread_mutex_init(lock, 0) != 0) {
+ rb_bug("native_mutex_initialize return non-zero");
+ }
+}
+
+void
+native_mutex_destroy(pthread_mutex_t *lock)
+{
+ if (pthread_mutex_destroy(lock) != 0) {
+ rb_bug("native_mutex_destroy return non-zero");
+ }
+}
#define native_cleanup_push pthread_cleanup_push
#define native_cleanup_pop pthread_cleanup_pop
@@ -40,9 +89,13 @@ Init_native_thread()
posix_signal(SIGVTALRM, null_func);
}
-NOINLINE(static int
- thread_start_func_2(rb_thread_t *th, VALUE *stack_start));
-void static thread_cleanup_func(void *th_ptr);
+NOINLINE(static int thread_start_func_2(rb_thread_t *th, VALUE *stack_start));
+
+static void
+native_thread_destroy(rb_thread_t *th)
+{
+ pthread_cond_destroy(&th->native_thread_data.sleep_cond);
+}
#define USE_THREAD_CACHE 0
@@ -55,16 +108,9 @@ thread_start_func_1(void *th_ptr)
{
rb_thread_t *th = th_ptr;
VALUE stack_start;
- /* ignore self and klass */
-
- native_cleanup_push(thread_cleanup_func, th);
/* run */
thread_start_func_2(th, &stack_start);
-
- /* cleanup */
- thread_cleanup_func(th);
- native_cleanup_pop(0);
}
#if USE_THREAD_CACHE
if (1) {
@@ -171,6 +217,9 @@ use_cached_thread(rb_thread_t *th)
return result;
}
+#define CHECK_ERR(expr) \
+ { int err; if ((err = (expr)) != 0) { printf("err: %d - " #expr, err); exit(1); }}
+
static int
native_thread_create(rb_thread_t *th)
{
@@ -190,16 +239,20 @@ native_thread_create(rb_thread_t *th)
#endif
thread_debug("create: %p, stack size: %ld\n", th, stack_size);
- pthread_attr_init(&attr);
+ CHECK_ERR(pthread_attr_init(&attr));
+
#ifdef PTHREAD_STACK_MIN
- pthread_attr_setstacksize(&attr, stack_size);
+ CHECK_ERR(pthread_attr_setstacksize(&attr, stack_size));
#endif
- pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+
+ CHECK_ERR(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
+ CHECK_ERR(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
err = pthread_create(&th->thread_id, &attr, thread_start_func_1, th);
+ CHECK_ERR(pthread_attr_destroy(&attr));
if (err != 0) {
+ st_delete_wrap(th->vm->living_threads, th->self);
th->status = THREAD_KILLED;
rb_raise(rb_eThreadError, "can't create Thread (%d)", err);
}