ruby-changes:15754
From: nobu <ko1@a...>
Date: Sun, 9 May 2010 01:15:44 +0900 (JST)
Subject: [ruby-changes:15754] Ruby:r27682 (trunk, ruby_1_9_2): * cont.c (stackgrowdirection): removed duplicated code, use
nobu 2010-05-09 01:15:20 +0900 (Sun, 09 May 2010) New Revision: 27682 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=27682 Log: * cont.c (stackgrowdirection): removed duplicated code, use STACK_UPPER macro instead. * gc.h (STACK_DIR_UPPER): moved from thread_pthread.c. Modified files: branches/ruby_1_9_2/ChangeLog branches/ruby_1_9_2/cont.c branches/ruby_1_9_2/gc.h branches/ruby_1_9_2/thread_pthread.c trunk/ChangeLog trunk/cont.c trunk/gc.h trunk/thread_pthread.c Index: ChangeLog =================================================================== --- ChangeLog (revision 27681) +++ ChangeLog (revision 27682) @@ -1,3 +1,10 @@ +Sun May 9 01:15:18 2010 Nobuyoshi Nakada <nobu@r...> + + * cont.c (stackgrowdirection): removed duplicated code, use + STACK_UPPER macro instead. + + * gc.h (STACK_DIR_UPPER): moved from thread_pthread.c. + Sun May 9 00:35:56 2010 Yuki Sonoda (Yugui) <yugui@y...> * test/dl/test_base.rb (libc_so, libm_so): supports solaris. Index: thread_pthread.c =================================================================== --- thread_pthread.c (revision 27681) +++ thread_pthread.c (revision 27682) @@ -185,14 +185,6 @@ #define USE_THREAD_CACHE 0 -#if STACK_GROW_DIRECTION -#define STACK_GROW_DIR_DETECTION -#define STACK_DIR_UPPER(a,b) STACK_UPPER(0, a, b) -#else -#define STACK_GROW_DIR_DETECTION VALUE stack_grow_dir_detection -#define STACK_DIR_UPPER(a,b) STACK_UPPER(&stack_grow_dir_detection, a, b) -#endif - #if defined HAVE_PTHREAD_GETATTR_NP || defined HAVE_PTHREAD_ATTR_GET_NP #define STACKADDR_AVAILABLE 1 #elif defined HAVE_PTHREAD_GET_STACKADDR_NP && defined HAVE_PTHREAD_GET_STACKSIZE_NP Index: gc.h =================================================================== --- gc.h (revision 27681) +++ gc.h (revision 27682) @@ -74,4 +74,12 @@ # define STACK_UPPER(x, a, b) (stack_growup_p(x) ? a : b) #endif +#if STACK_GROW_DIRECTION +#define STACK_GROW_DIR_DETECTION +#define STACK_DIR_UPPER(a,b) STACK_UPPER(0, a, b) +#else +#define STACK_GROW_DIR_DETECTION VALUE stack_grow_dir_detection +#define STACK_DIR_UPPER(a,b) STACK_UPPER(&stack_grow_dir_detection, a, b) +#endif + #endif /* RUBY_GC_H */ Index: cont.c =================================================================== --- cont.c (revision 27681) +++ cont.c (revision 27682) @@ -45,9 +45,6 @@ #define PAGE_SIZE (pagesize) #define PAGE_MASK (~(PAGE_SIZE - 1)) static long pagesize; -static long stackgrowdirection; -#define STACK_GROW_DOWNWARD 1 -#define STACK_GROW_UPWARD 2 #define FIBER_MACHINE_STACK_ALLOCATION_SIZE (0x10000 / sizeof(VALUE)) #endif @@ -498,20 +495,10 @@ fiber_set_stack_location(void) { rb_thread_t *th = GET_THREAD(); - unsigned long ptr; + VALUE ptr; - SET_MACHINE_STACK_END((VALUE**)(&ptr)); - switch (stackgrowdirection) { - case STACK_GROW_DOWNWARD: - th->machine_stack_start = (void*)((ptr & PAGE_MASK) + PAGE_SIZE); - break; - case STACK_GROW_UPWARD: - th->machine_stack_start = (void*)(ptr & PAGE_MASK); - break; - default: - rb_bug("fiber_get_stacck_location: should not be reached"); - break; - } + SET_MACHINE_STACK_END(&ptr); + th->machine_stack_start = (void*)((ptr & PAGE_MASK) + STACK_UPPER(&ptr, 0, PAGE_SIZE)); } static VOID CALLBACK @@ -539,25 +526,14 @@ } } else { + STACK_GROW_DIR_DETECTION; ptr = (VALUE*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); - if ((int)ptr == -1) { + if (ptr == (VALUE*)(SIGNED_VALUE)-1) { rb_raise(rb_eFiberError, "can't alloc machine stack to fiber"); } - switch (stackgrowdirection) { - case STACK_GROW_DOWNWARD: - if (mprotect(ptr, PAGE_SIZE, PROT_READ | PROT_WRITE) < 0) { - rb_raise(rb_eFiberError, "mprotect failed"); - } - break; - case STACK_GROW_UPWARD: - if (mprotect(ptr + (size - PAGE_SIZE) / sizeof(VALUE), - PAGE_SIZE, PROT_READ | PROT_WRITE) < 0) { - rb_raise(rb_eFiberError, "mprotect failed"); - } - break; - default: - rb_bug("fiber_machine_stack_alloc: should not be reached"); - break; + if (mprotect(ptr + STACK_DIR_UPPER((size - PAGE_SIZE) / sizeof(VALUE), 0), + PAGE_SIZE, PROT_READ | PROT_WRITE) < 0) { + rb_raise(rb_eFiberError, "mprotect failed"); } } @@ -575,6 +551,7 @@ #else /* not WIN32 */ ucontext_t *context = &fib->context; VALUE *ptr; + STACK_GROW_DIR_DETECTION; getcontext(context); ptr = fiber_machine_stack_alloc(size); @@ -582,17 +559,7 @@ context->uc_stack.ss_sp = ptr; context->uc_stack.ss_size = size; makecontext(context, rb_fiber_start, 0); - switch (stackgrowdirection) { - case STACK_GROW_DOWNWARD: - sth->machine_stack_start = ptr + size / sizeof(VALUE); - break; - case STACK_GROW_UPWARD: - sth->machine_stack_start = ptr; - break; - default: - rb_bug("fiber_initialize_stackcontext: should not be reached"); - break; - } + sth->machine_stack_start = ptr + STACK_DIR_UPPER(0, size / sizeof(VALUE)); #endif sth->machine_stack_maxsize = size; @@ -622,19 +589,14 @@ /* save oldfib's machine stack */ if (oldfib->status != TERMINATED) { SET_MACHINE_STACK_END(&th->machine_stack_end); - switch (stackgrowdirection) { - case STACK_GROW_DOWNWARD: - oldfib->cont.machine_stack_size = th->machine_stack_start - th->machine_stack_end; - oldfib->cont.machine_stack = th->machine_stack_end; - break; - case STACK_GROW_UPWARD: - oldfib->cont.machine_stack_size = th->machine_stack_end - th->machine_stack_start; - oldfib->cont.machine_stack = th->machine_stack_start; - break; - default: - rb_bug("fiber_get_stacck_location: should not be reached"); - break; + if (STACK_DIR_UPPER(0, 1)) { + oldfib->cont.machine_stack_size = th->machine_stack_start - th->machine_stack_end; + oldfib->cont.machine_stack = th->machine_stack_end; } + else { + oldfib->cont.machine_stack_size = th->machine_stack_end - th->machine_stack_start; + oldfib->cont.machine_stack = th->machine_stack_start; + } } /* exchange machine_stack_start between oldfib and newfib */ oldfib->cont.saved_thread.machine_stack_start = th->machine_stack_start; @@ -1451,14 +1413,6 @@ pagesize = sysconf(_SC_PAGESIZE); #endif SET_MACHINE_STACK_END(&th->machine_stack_end); - if (th->machine_stack_start > th->machine_stack_end) { - /* stack grows downward */ - stackgrowdirection = STACK_GROW_DOWNWARD; - } - else { - /* stack grows upward */ - stackgrowdirection = STACK_GROW_UPWARD; - } #endif rb_cFiber = rb_define_class("Fiber", rb_cObject); Index: ruby_1_9_2/ChangeLog =================================================================== --- ruby_1_9_2/ChangeLog (revision 27681) +++ ruby_1_9_2/ChangeLog (revision 27682) @@ -1,3 +1,10 @@ +Sun May 9 01:15:18 2010 Nobuyoshi Nakada <nobu@r...> + + * cont.c (stackgrowdirection): removed duplicated code, use + STACK_UPPER macro instead. + + * gc.h (STACK_DIR_UPPER): moved from thread_pthread.c. + Sun May 9 00:35:56 2010 Yuki Sonoda (Yugui) <yugui@y...> * test/dl/test_base.rb (libc_so, libm_so): supports solaris. Index: ruby_1_9_2/thread_pthread.c =================================================================== --- ruby_1_9_2/thread_pthread.c (revision 27681) +++ ruby_1_9_2/thread_pthread.c (revision 27682) @@ -185,14 +185,6 @@ #define USE_THREAD_CACHE 0 -#if STACK_GROW_DIRECTION -#define STACK_GROW_DIR_DETECTION -#define STACK_DIR_UPPER(a,b) STACK_UPPER(0, a, b) -#else -#define STACK_GROW_DIR_DETECTION VALUE stack_grow_dir_detection -#define STACK_DIR_UPPER(a,b) STACK_UPPER(&stack_grow_dir_detection, a, b) -#endif - #if defined HAVE_PTHREAD_GETATTR_NP || defined HAVE_PTHREAD_ATTR_GET_NP #define STACKADDR_AVAILABLE 1 #elif defined HAVE_PTHREAD_GET_STACKADDR_NP && defined HAVE_PTHREAD_GET_STACKSIZE_NP Index: ruby_1_9_2/gc.h =================================================================== --- ruby_1_9_2/gc.h (revision 27681) +++ ruby_1_9_2/gc.h (revision 27682) @@ -74,4 +74,12 @@ # define STACK_UPPER(x, a, b) (stack_growup_p(x) ? a : b) #endif +#if STACK_GROW_DIRECTION +#define STACK_GROW_DIR_DETECTION +#define STACK_DIR_UPPER(a,b) STACK_UPPER(0, a, b) +#else +#define STACK_GROW_DIR_DETECTION VALUE stack_grow_dir_detection +#define STACK_DIR_UPPER(a,b) STACK_UPPER(&stack_grow_dir_detection, a, b) +#endif + #endif /* RUBY_GC_H */ Index: ruby_1_9_2/cont.c =================================================================== --- ruby_1_9_2/cont.c (revision 27681) +++ ruby_1_9_2/cont.c (revision 27682) @@ -45,9 +45,6 @@ #define PAGE_SIZE (pagesize) #define PAGE_MASK (~(PAGE_SIZE - 1)) static long pagesize; -static long stackgrowdirection; -#define STACK_GROW_DOWNWARD 1 -#define STACK_GROW_UPWARD 2 #define FIBER_MACHINE_STACK_ALLOCATION_SIZE (0x10000 / sizeof(VALUE)) #endif @@ -498,20 +495,10 @@ fiber_set_stack_location(void) { rb_thread_t *th = GET_THREAD(); - unsigned long ptr; + VALUE ptr; - SET_MACHINE_STACK_END((VALUE**)(&ptr)); - switch (stackgrowdirection) { - case STACK_GROW_DOWNWARD: - th->machine_stack_start = (void*)((ptr & PAGE_MASK) + PAGE_SIZE); - break; - case STACK_GROW_UPWARD: - th->machine_stack_start = (void*)(ptr & PAGE_MASK); - break; - default: - rb_bug("fiber_get_stacck_location: should not be reached"); - break; - } + SET_MACHINE_STACK_END(&ptr); + th->machine_stack_start = (void*)((ptr & PAGE_MASK) + STACK_UPPER(&ptr, 0, PAGE_SIZE)); } static VOID CALLBACK @@ -539,25 +526,14 @@ } } else { + STACK_GROW_DIR_DETECTION; ptr = (VALUE*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); - if ((int)ptr == -1) { + if (ptr == (VALUE*)(SIGNED_VALUE)-1) { rb_raise(rb_eFiberError, "can't alloc machine stack to fiber"); } - switch (stackgrowdirection) { - case STACK_GROW_DOWNWARD: - if (mprotect(ptr, PAGE_SIZE, PROT_READ | PROT_WRITE) < 0) { - rb_raise(rb_eFiberError, "mprotect failed"); - } - break; - case STACK_GROW_UPWARD: - if (mprotect(ptr + (size - PAGE_SIZE) / sizeof(VALUE), - PAGE_SIZE, PROT_READ | PROT_WRITE) < 0) { - rb_raise(rb_eFiberError, "mprotect failed"); - } - break; - default: - rb_bug("fiber_machine_stack_alloc: should not be reached"); - break; + if (mprotect(ptr + STACK_DIR_UPPER((size - PAGE_SIZE) / sizeof(VALUE), 0), + PAGE_SIZE, PROT_READ | PROT_WRITE) < 0) { + rb_raise(rb_eFiberError, "mprotect failed"); } } @@ -575,6 +551,7 @@ #else /* not WIN32 */ ucontext_t *context = &fib->context; VALUE *ptr; + STACK_GROW_DIR_DETECTION; getcontext(context); ptr = fiber_machine_stack_alloc(size); @@ -582,17 +559,7 @@ context->uc_stack.ss_sp = ptr; context->uc_stack.ss_size = size; makecontext(context, rb_fiber_start, 0); - switch (stackgrowdirection) { - case STACK_GROW_DOWNWARD: - sth->machine_stack_start = ptr + size / sizeof(VALUE); - break; - case STACK_GROW_UPWARD: - sth->machine_stack_start = ptr; - break; - default: - rb_bug("fiber_initialize_stackcontext: should not be reached"); - break; - } + sth->machine_stack_start = ptr + STACK_DIR_UPPER(0, size / sizeof(VALUE)); #endif sth->machine_stack_maxsize = size; @@ -622,19 +589,14 @@ /* save oldfib's machine stack */ if (oldfib->status != TERMINATED) { SET_MACHINE_STACK_END(&th->machine_stack_end); - switch (stackgrowdirection) { - case STACK_GROW_DOWNWARD: - oldfib->cont.machine_stack_size = th->machine_stack_start - th->machine_stack_end; - oldfib->cont.machine_stack = th->machine_stack_end; - break; - case STACK_GROW_UPWARD: - oldfib->cont.machine_stack_size = th->machine_stack_end - th->machine_stack_start; - oldfib->cont.machine_stack = th->machine_stack_start; - break; - default: - rb_bug("fiber_get_stacck_location: should not be reached"); - break; + if (STACK_DIR_UPPER(0, 1)) { + oldfib->cont.machine_stack_size = th->machine_stack_start - th->machine_stack_end; + oldfib->cont.machine_stack = th->machine_stack_end; } + else { + oldfib->cont.machine_stack_size = th->machine_stack_end - th->machine_stack_start; + oldfib->cont.machine_stack = th->machine_stack_start; + } } /* exchange machine_stack_start between oldfib and newfib */ oldfib->cont.saved_thread.machine_stack_start = th->machine_stack_start; @@ -1451,14 +1413,6 @@ pagesize = sysconf(_SC_PAGESIZE); #endif SET_MACHINE_STACK_END(&th->machine_stack_end); - if (th->machine_stack_start > th->machine_stack_end) { - /* stack grows downward */ - stackgrowdirection = STACK_GROW_DOWNWARD; - } - else { - /* stack grows upward */ - stackgrowdirection = STACK_GROW_UPWARD; - } #endif rb_cFiber = rb_define_class("Fiber", rb_cObject); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/