summaryrefslogtreecommitdiff
path: root/thread_pthread.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-14 02:59:19 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-14 02:59:19 +0000
commitfc3c60f6081d85f6274986a7a08b59db1515fcb5 (patch)
tree5b7e96855783c55294b0a9824a598b9b269a991d /thread_pthread.c
parent436b02b3322d6809c2bf4cbadbe8b324a53e07e4 (diff)
* gc.h (STACK_UPPER): moved from gc.c
* thread.c, thread_{pthread,win32}.c (ruby_init_stack, ruby_thread_init_stack): moved stack initialization from gc.c. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17155 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'thread_pthread.c')
-rw-r--r--thread_pthread.c87
1 files changed, 84 insertions, 3 deletions
diff --git a/thread_pthread.c b/thread_pthread.c
index 54b7677146..62ac28b808 100644
--- a/thread_pthread.c
+++ b/thread_pthread.c
@@ -11,6 +11,12 @@
#ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
+#include "gc.h"
+
+#ifdef HAVE_SYS_RESOURCE_H
+#include <sys/resource.h>
+#endif
+
static void native_mutex_lock(pthread_mutex_t *lock);
static void native_mutex_unlock(pthread_mutex_t *lock);
static int native_mutex_trylock(pthread_mutex_t *lock);
@@ -164,6 +170,84 @@ native_thread_destroy(rb_thread_t *th)
#define USE_THREAD_CACHE 0
+static struct {
+ rb_thread_id_t id;
+ size_t stack_maxsize;
+ VALUE *stack_start;
+#ifdef __ia64
+ VALUE *register_stack_start;
+#endif
+} native_main_thread;
+
+#undef ruby_init_stack
+void
+ruby_init_stack(VALUE *addr
+#ifdef __ia64
+ , void *bsp
+#endif
+ )
+{
+ native_main_thread.id = pthread_self();
+ if (!native_main_thread.stack_start ||
+ STACK_UPPER(&addr,
+ native_main_thread.stack_start > addr,
+ native_main_thread.stack_start < addr)) {
+ native_main_thread.stack_start = addr;
+ }
+#ifdef __ia64
+ if (!native_main_thread.register_stack_start ||
+ (VALUE*)bsp < native_main_thread.register_stack_start) {
+ native_main_thread.register_stack_start = (VALUE*)bsp;
+ }
+#endif
+#ifdef HAVE_GETRLIMIT
+ {
+ struct rlimit rlim;
+
+ if (getrlimit(RLIMIT_STACK, &rlim) == 0) {
+ unsigned int space = rlim.rlim_cur/5;
+
+ if (space > 1024*1024) space = 1024*1024;
+ native_main_thread.stack_maxsize = rlim.rlim_cur - space;
+ }
+ }
+#endif
+}
+
+#define CHECK_ERR(expr) \
+ {int err = (expr); if (err) {rb_bug("err: %d - %s", err, #expr);}}
+
+static int
+native_thread_init_stack(rb_thread_t *th)
+{
+ rb_thread_id_t curr = pthread_self();
+
+ if (pthread_equal(curr, native_main_thread.id)) {
+ th->machine_stack_start = native_main_thread.stack_start;
+ th->machine_stack_maxsize = native_main_thread.stack_maxsize;
+ }
+ else {
+#ifdef HAVE_PTHREAD_GETATTR_NP
+ pthread_attr_t attr;
+ CHECK_ERR(pthread_getattr_np(curr, &attr));
+# if defined HAVE_PTHREAD_ATTR_GETSTACK
+ CHECK_ERR(pthread_attr_getstack(&attr, &th->machine_stack_start, &th->machine_stack_maxsize));
+# elif defined HAVE_PTHREAD_ATTR_GETSTACKSIZE && defined HAVE_PTHREAD_ATTR_GETSTACKADDR
+ CHECK_ERR(pthread_attr_getstackaddr(&attr, &th->machine_stack_start));
+ CHECK_ERR(pthread_attr_getstacksize(&attr, &th->machine_stack_maxsize));
+# endif
+#else
+ rb_raise(rb_eNotImpError, "ruby engine can initialize only in the main thread");
+#endif
+ }
+#ifdef __ia64
+ th->machine_register_stack_start = native_main_thread.register_stack_start;
+ th->machine_stack_maxsize /= 2;
+ th->machine_register_stack_maxsize = th->machine_stack_maxsize;
+#endif
+ return 0;
+}
+
static void *
thread_start_func_1(void *th_ptr)
{
@@ -282,9 +366,6 @@ use_cached_thread(rb_thread_t *th)
return result;
}
-#define CHECK_ERR(expr) \
- { int err; if ((err = (expr)) != 0) { rb_bug("err: %d - %s", err, #expr); }}
-
static int
native_thread_create(rb_thread_t *th)
{