2006-12-31 10:02:22 -05:00
|
|
|
/* -*-c-*- */
|
|
|
|
/**********************************************************************
|
|
|
|
|
2007-12-20 04:29:46 -05:00
|
|
|
thread_win32.c -
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
$Author$
|
|
|
|
|
* blockinlining.c, compile.c, compile.h, debug.c, debug.h,
id.c, insnhelper.h, insns.def, thread.c, thread_pthread.ci,
thread_pthread.h, thread_win32.ci, thread_win32.h, vm.h,
vm_dump.c, vm_evalbody.ci, vm_opts.h: fix comments and
copyright year.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13920 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 17:13:04 -05:00
|
|
|
Copyright (C) 2004-2007 Koichi Sasada
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
|
|
|
|
|
|
|
|
#include <process.h>
|
|
|
|
|
|
|
|
#define WIN32_WAIT_TIMEOUT 10 /* 10 ms */
|
|
|
|
#undef Sleep
|
|
|
|
|
|
|
|
#define native_thread_yield() Sleep(0)
|
2007-02-08 06:51:40 -05:00
|
|
|
#define remove_signal_thread_list(th)
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-08-18 04:40:13 -04:00
|
|
|
static volatile DWORD ruby_native_thread_key = TLS_OUT_OF_INDEXES;
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static int native_mutex_lock(rb_thread_lock_t *);
|
|
|
|
static int native_mutex_unlock(rb_thread_lock_t *);
|
|
|
|
static int native_mutex_trylock(rb_thread_lock_t *);
|
|
|
|
static void native_mutex_initialize(rb_thread_lock_t *);
|
|
|
|
|
|
|
|
static void native_cond_signal(rb_thread_cond_t *cond);
|
|
|
|
static void native_cond_broadcast(rb_thread_cond_t *cond);
|
|
|
|
static void native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex);
|
|
|
|
static void native_cond_initialize(rb_thread_cond_t *cond);
|
|
|
|
static void native_cond_destroy(rb_thread_cond_t *cond);
|
|
|
|
|
2007-08-18 04:40:13 -04:00
|
|
|
static rb_thread_t *
|
|
|
|
ruby_thread_from_native(void)
|
|
|
|
{
|
|
|
|
return TlsGetValue(ruby_native_thread_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ruby_thread_set_native(rb_thread_t *th)
|
|
|
|
{
|
|
|
|
return TlsSetValue(ruby_native_thread_key, th);
|
|
|
|
}
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
static void
|
2007-08-18 04:40:13 -04:00
|
|
|
Init_native_thread(void)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2007-08-18 04:40:13 -04:00
|
|
|
|
|
|
|
ruby_native_thread_key = TlsAlloc();
|
2006-12-31 10:02:22 -05:00
|
|
|
DuplicateHandle(GetCurrentProcess(),
|
|
|
|
GetCurrentThread(),
|
|
|
|
GetCurrentProcess(),
|
|
|
|
&th->thread_id, 0, FALSE, DUPLICATE_SAME_ACCESS);
|
|
|
|
|
|
|
|
th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
|
|
|
|
|
|
|
|
thread_debug("initial thread (th: %p, thid: %p, event: %p)\n",
|
|
|
|
th, GET_THREAD()->thread_id,
|
|
|
|
th->native_thread_data.interrupt_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-02-08 22:42:46 -05:00
|
|
|
w32_error(void)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
GetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR) & lpMsgBuf, 0, NULL);
|
2008-04-26 05:36:35 -04:00
|
|
|
rb_bug("%s", (char*)lpMsgBuf);
|
2007-02-08 22:42:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
w32_set_event(HANDLE handle)
|
|
|
|
{
|
|
|
|
if (SetEvent(handle) == 0) {
|
|
|
|
w32_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
w32_reset_event(HANDLE handle)
|
|
|
|
{
|
|
|
|
if (ResetEvent(handle) == 0) {
|
|
|
|
w32_error();
|
|
|
|
}
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-02-23 04:33:53 -05:00
|
|
|
w32_wait_events(HANDLE *events, int count, DWORD timeout, rb_thread_t *th)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2007-02-23 04:33:53 -05:00
|
|
|
HANDLE *targets = events;
|
2007-04-10 03:16:42 -04:00
|
|
|
HANDLE intr;
|
2006-12-31 10:02:22 -05:00
|
|
|
DWORD ret;
|
|
|
|
|
2007-02-23 04:33:53 -05:00
|
|
|
thread_debug(" w32_wait_events events:%p, count:%d, timeout:%ld, th:%p\n",
|
|
|
|
events, count, timeout, th);
|
2007-04-10 03:16:42 -04:00
|
|
|
if (th && (intr = th->native_thread_data.interrupt_event)) {
|
2007-02-08 22:42:46 -05:00
|
|
|
w32_reset_event(intr);
|
2008-01-18 10:15:40 -05:00
|
|
|
if (RUBY_VM_INTERRUPTED(th)) {
|
2007-02-08 22:42:46 -05:00
|
|
|
w32_set_event(intr);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
2007-02-23 04:33:53 -05:00
|
|
|
targets = ALLOCA_N(HANDLE, count + 1);
|
|
|
|
memcpy(targets, events, sizeof(HANDLE) * count);
|
|
|
|
|
|
|
|
targets[count++] = intr;
|
2006-12-31 10:02:22 -05:00
|
|
|
thread_debug(" * handle: %p (count: %d, intr)\n", intr, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_debug(" WaitForMultipleObjects start (count: %d)\n", count);
|
2007-02-23 04:33:53 -05:00
|
|
|
ret = WaitForMultipleObjects(count, targets, FALSE, timeout);
|
2008-04-26 05:36:35 -04:00
|
|
|
thread_debug(" WaitForMultipleObjects end (ret: %lu)\n", ret);
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
if (ret == WAIT_OBJECT_0 + count - 1 && th) {
|
|
|
|
errno = EINTR;
|
|
|
|
}
|
|
|
|
if (ret == -1 && THREAD_DEBUG) {
|
|
|
|
int i;
|
|
|
|
DWORD dmy;
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
thread_debug(" * error handle %d - %s\n", i,
|
2007-02-23 04:33:53 -05:00
|
|
|
GetHandleInformation(targets[i], &dmy) ? "OK" : "NG");
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-11-20 05:47:53 -05:00
|
|
|
static void ubf_handle(void *ptr);
|
2007-02-23 04:33:53 -05:00
|
|
|
#define ubf_select ubf_handle
|
|
|
|
|
2007-02-24 04:43:40 -05:00
|
|
|
int
|
|
|
|
rb_w32_wait_events_blocking(HANDLE *events, int num, DWORD timeout)
|
|
|
|
{
|
|
|
|
return w32_wait_events(events, num, timeout, GET_THREAD());
|
|
|
|
}
|
|
|
|
|
2007-02-23 04:33:53 -05:00
|
|
|
int
|
|
|
|
rb_w32_wait_events(HANDLE *events, int num, DWORD timeout)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2007-11-20 05:47:53 -05:00
|
|
|
BLOCKING_REGION(ret = rb_w32_wait_events_blocking(events, num, timeout),
|
2008-06-19 10:18:46 -04:00
|
|
|
ubf_handle, GET_THREAD());
|
2007-02-23 04:33:53 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-02-08 22:42:46 -05:00
|
|
|
static void
|
|
|
|
w32_close_handle(HANDLE handle)
|
|
|
|
{
|
|
|
|
if (CloseHandle(handle) == 0) {
|
|
|
|
w32_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
w32_resume_thread(HANDLE handle)
|
|
|
|
{
|
|
|
|
if (ResumeThread(handle) == -1) {
|
|
|
|
w32_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-21 04:48:29 -04:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define HAVE__BEGINTHREADEX 1
|
|
|
|
#else
|
|
|
|
#undef HAVE__BEGINTHREADEX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE__BEGINTHREADEX
|
|
|
|
#define start_thread (HANDLE)_beginthreadex
|
|
|
|
typedef unsigned long (_stdcall *w32_thread_start_func)(void*);
|
|
|
|
#else
|
|
|
|
#define start_thread CreateThread
|
|
|
|
typedef LPTHREAD_START_ROUTINE w32_thread_start_func;
|
|
|
|
#endif
|
|
|
|
|
2007-02-08 22:42:46 -05:00
|
|
|
static HANDLE
|
2007-07-21 04:48:29 -04:00
|
|
|
w32_create_thread(DWORD stack_size, w32_thread_start_func func, void *val)
|
2007-02-08 22:42:46 -05:00
|
|
|
{
|
2007-07-21 04:48:29 -04:00
|
|
|
return start_thread(0, stack_size, func, val, CREATE_SUSPENDED, 0);
|
2007-02-08 22:42:46 -05:00
|
|
|
}
|
|
|
|
|
2007-02-23 04:33:53 -05:00
|
|
|
int
|
|
|
|
rb_w32_sleep(unsigned long msec)
|
|
|
|
{
|
|
|
|
return w32_wait_events(0, 0, msec, GET_THREAD());
|
|
|
|
}
|
|
|
|
|
|
|
|
int WINAPI
|
|
|
|
rb_w32_Sleep(unsigned long msec)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2007-11-20 05:47:53 -05:00
|
|
|
BLOCKING_REGION(ret = rb_w32_sleep(msec),
|
2008-06-19 10:18:46 -04:00
|
|
|
ubf_handle, GET_THREAD());
|
2007-02-23 04:33:53 -05:00
|
|
|
return ret;
|
|
|
|
}
|
2007-02-08 06:51:40 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
static void
|
2008-07-16 15:19:36 -04:00
|
|
|
native_sleep(rb_thread_t *th, struct timeval *tv)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2008-07-09 09:41:19 -04:00
|
|
|
DWORD msec;
|
2008-06-12 09:20:55 -04:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
if (tv) {
|
|
|
|
msec = tv->tv_sec * 1000 + tv->tv_usec / 1000;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
msec = INFINITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GVL_UNLOCK_BEGIN();
|
|
|
|
{
|
2008-07-09 09:41:19 -04:00
|
|
|
DWORD ret;
|
|
|
|
|
2008-06-21 03:48:29 -04:00
|
|
|
native_mutex_lock(&th->interrupt_lock);
|
2008-05-29 21:52:38 -04:00
|
|
|
th->unblock.func = ubf_handle;
|
|
|
|
th->unblock.arg = th;
|
2008-06-21 03:48:29 -04:00
|
|
|
native_mutex_unlock(&th->interrupt_lock);
|
2007-12-24 23:16:06 -05:00
|
|
|
|
|
|
|
if (RUBY_VM_INTERRUPTED(th)) {
|
|
|
|
/* interrupted. return immediate */
|
|
|
|
}
|
|
|
|
else {
|
2008-04-26 05:36:35 -04:00
|
|
|
thread_debug("native_sleep start (%lu)\n", msec);
|
2007-12-24 23:16:06 -05:00
|
|
|
ret = w32_wait_events(0, 0, msec, th);
|
2008-04-26 05:36:35 -04:00
|
|
|
thread_debug("native_sleep done (%lu)\n", ret);
|
2007-12-24 23:16:06 -05:00
|
|
|
}
|
|
|
|
|
2008-06-21 03:48:29 -04:00
|
|
|
native_mutex_lock(&th->interrupt_lock);
|
2008-05-29 21:52:38 -04:00
|
|
|
th->unblock.func = 0;
|
|
|
|
th->unblock.arg = 0;
|
2008-06-21 03:48:29 -04:00
|
|
|
native_mutex_unlock(&th->interrupt_lock);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
GVL_UNLOCK_END();
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static int
|
* blockinlining.c, compile.c, compile.h, error.c, eval.c,
eval_intern.h, eval_jump.h, eval_load.c, eval_method.h,
eval_safe.h, gc.c, insnhelper.h, insns.def, iseq.c, proc.c,
process.c, signal.c, thread.c, thread_pthread.ci, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, vm_macro.def,
yarv.h, yarvcore.h, yarvcore.c: change type and macro names:
* yarv_*_t -> rb_*_t
* yarv_*_struct -> rb_*_struct
* yarv_tag -> rb_vm_tag
* YARV_* -> RUBY_VM_*
* proc.c, vm.c: move functions about env object creation
from proc.c to vm.c.
* proc.c, yarvcore.c: fix rb_cVM initialization place.
* inits.c: change Init_ISeq() order (after Init_VM).
* ruby.h, proc.c: change declaration place of rb_cEnv
from proc.c to ruby.c.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11651 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 14:00:03 -05:00
|
|
|
native_mutex_lock(rb_thread_lock_t *lock)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
|
|
|
#if USE_WIN32_MUTEX
|
|
|
|
DWORD result;
|
|
|
|
while (1) {
|
|
|
|
thread_debug("native_mutex_lock: %p\n", *lock);
|
2007-02-23 04:33:53 -05:00
|
|
|
result = w32_wait_events(&*lock, 1, INFINITE, 0);
|
2006-12-31 10:02:22 -05:00
|
|
|
switch (result) {
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
case WAIT_OBJECT_0:
|
2006-12-31 10:02:22 -05:00
|
|
|
/* get mutex object */
|
|
|
|
thread_debug("acquire mutex: %p\n", *lock);
|
|
|
|
return 0;
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
case WAIT_OBJECT_0 + 1:
|
2006-12-31 10:02:22 -05:00
|
|
|
/* interrupt */
|
|
|
|
errno = EINTR;
|
|
|
|
thread_debug("acquire mutex interrupted: %p\n", *lock);
|
|
|
|
return 0;
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
case WAIT_TIMEOUT:
|
2006-12-31 10:02:22 -05:00
|
|
|
thread_debug("timeout mutex: %p\n", *lock);
|
|
|
|
break;
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
case WAIT_ABANDONED:
|
2006-12-31 10:02:22 -05:00
|
|
|
rb_bug("win32_mutex_lock: WAIT_ABANDONED");
|
|
|
|
break;
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
default:
|
2006-12-31 10:02:22 -05:00
|
|
|
rb_bug("win32_mutex_lock: unknown result (%d)", result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
EnterCriticalSection(lock);
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static int
|
* blockinlining.c, compile.c, compile.h, error.c, eval.c,
eval_intern.h, eval_jump.h, eval_load.c, eval_method.h,
eval_safe.h, gc.c, insnhelper.h, insns.def, iseq.c, proc.c,
process.c, signal.c, thread.c, thread_pthread.ci, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, vm_macro.def,
yarv.h, yarvcore.h, yarvcore.c: change type and macro names:
* yarv_*_t -> rb_*_t
* yarv_*_struct -> rb_*_struct
* yarv_tag -> rb_vm_tag
* YARV_* -> RUBY_VM_*
* proc.c, vm.c: move functions about env object creation
from proc.c to vm.c.
* proc.c, yarvcore.c: fix rb_cVM initialization place.
* inits.c: change Init_ISeq() order (after Init_VM).
* ruby.h, proc.c: change declaration place of rb_cEnv
from proc.c to ruby.c.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11651 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 14:00:03 -05:00
|
|
|
native_mutex_unlock(rb_thread_lock_t *lock)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
|
|
|
#if USE_WIN32_MUTEX
|
|
|
|
thread_debug("release mutex: %p\n", *lock);
|
|
|
|
return ReleaseMutex(*lock);
|
|
|
|
#else
|
|
|
|
LeaveCriticalSection(lock);
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static int
|
* blockinlining.c, compile.c, compile.h, error.c, eval.c,
eval_intern.h, eval_jump.h, eval_load.c, eval_method.h,
eval_safe.h, gc.c, insnhelper.h, insns.def, iseq.c, proc.c,
process.c, signal.c, thread.c, thread_pthread.ci, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, vm_macro.def,
yarv.h, yarvcore.h, yarvcore.c: change type and macro names:
* yarv_*_t -> rb_*_t
* yarv_*_struct -> rb_*_struct
* yarv_tag -> rb_vm_tag
* YARV_* -> RUBY_VM_*
* proc.c, vm.c: move functions about env object creation
from proc.c to vm.c.
* proc.c, yarvcore.c: fix rb_cVM initialization place.
* inits.c: change Init_ISeq() order (after Init_VM).
* ruby.h, proc.c: change declaration place of rb_cEnv
from proc.c to ruby.c.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11651 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 14:00:03 -05:00
|
|
|
native_mutex_trylock(rb_thread_lock_t *lock)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2007-06-05 00:30:38 -04:00
|
|
|
#if USE_WIN32_MUTEX
|
2006-12-31 10:02:22 -05:00
|
|
|
int result;
|
|
|
|
thread_debug("native_mutex_trylock: %p\n", *lock);
|
2007-02-23 04:33:53 -05:00
|
|
|
result = w32_wait_events(&*lock, 1, 1, 0);
|
2006-12-31 10:02:22 -05:00
|
|
|
thread_debug("native_mutex_trylock result: %d\n", result);
|
|
|
|
switch (result) {
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
case WAIT_OBJECT_0:
|
2006-12-31 10:02:22 -05:00
|
|
|
return 0;
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
case WAIT_TIMEOUT:
|
2006-12-31 10:02:22 -05:00
|
|
|
return EBUSY;
|
|
|
|
}
|
|
|
|
return EINVAL;
|
|
|
|
#else
|
|
|
|
return TryEnterCriticalSection(lock) == 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
* blockinlining.c, compile.c, compile.h, error.c, eval.c,
eval_intern.h, eval_jump.h, eval_load.c, eval_method.h,
eval_safe.h, gc.c, insnhelper.h, insns.def, iseq.c, proc.c,
process.c, signal.c, thread.c, thread_pthread.ci, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, vm_macro.def,
yarv.h, yarvcore.h, yarvcore.c: change type and macro names:
* yarv_*_t -> rb_*_t
* yarv_*_struct -> rb_*_struct
* yarv_tag -> rb_vm_tag
* YARV_* -> RUBY_VM_*
* proc.c, vm.c: move functions about env object creation
from proc.c to vm.c.
* proc.c, yarvcore.c: fix rb_cVM initialization place.
* inits.c: change Init_ISeq() order (after Init_VM).
* ruby.h, proc.c: change declaration place of rb_cEnv
from proc.c to ruby.c.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11651 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 14:00:03 -05:00
|
|
|
native_mutex_initialize(rb_thread_lock_t *lock)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2007-06-05 00:30:38 -04:00
|
|
|
#if USE_WIN32_MUTEX
|
2006-12-31 10:02:22 -05:00
|
|
|
*lock = CreateMutex(NULL, FALSE, NULL);
|
2007-02-08 22:42:46 -05:00
|
|
|
if (*lock == NULL) {
|
|
|
|
w32_error();
|
|
|
|
}
|
* call_cfunc.ci, compile.c, compile.h, debug.h, eval.c,
eval_error.h, eval_jump.h, eval_load.c, eval_thread.c, gc.c,
insnhelper.h, insns.def, iseq.c, main.c, numeric.c, parse.y,
range.c, regenc.h, ruby.h, signal.c, thread.c, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fixed indents and non-C90 comments.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11620 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-02 11:26:04 -05:00
|
|
|
/* thread_debug("initialize mutex: %p\n", *lock); */
|
2006-12-31 10:02:22 -05:00
|
|
|
#else
|
|
|
|
InitializeCriticalSection(lock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2007-02-08 15:24:55 -05:00
|
|
|
native_mutex_destroy(rb_thread_lock_t *lock)
|
|
|
|
{
|
2007-06-05 00:30:38 -04:00
|
|
|
#if USE_WIN32_MUTEX
|
2007-02-08 22:42:46 -05:00
|
|
|
w32_close_handle(lock);
|
|
|
|
#else
|
|
|
|
DeleteCriticalSection(lock);
|
|
|
|
#endif
|
2007-02-08 15:24:55 -05:00
|
|
|
}
|
|
|
|
|
2007-08-27 12:48:14 -04:00
|
|
|
struct cond_event_entry {
|
|
|
|
struct cond_event_entry* next;
|
|
|
|
HANDLE event;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct rb_thread_cond_struct {
|
|
|
|
struct cond_event_entry *next;
|
|
|
|
struct cond_event_entry *last;
|
|
|
|
};
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2007-08-27 12:48:14 -04:00
|
|
|
native_cond_signal(rb_thread_cond_t *cond)
|
|
|
|
{
|
|
|
|
/* cond is guarded by mutex */
|
|
|
|
struct cond_event_entry *e = cond->next;
|
|
|
|
|
|
|
|
if (e) {
|
|
|
|
cond->next = e->next;
|
|
|
|
SetEvent(e->event);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_bug("native_cond_signal: no pending threads");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2007-08-27 12:48:14 -04:00
|
|
|
native_cond_broadcast(rb_thread_cond_t *cond)
|
|
|
|
{
|
|
|
|
/* cond is guarded by mutex */
|
|
|
|
struct cond_event_entry *e = cond->next;
|
|
|
|
cond->next = 0;
|
|
|
|
|
|
|
|
while (e) {
|
|
|
|
SetEvent(e->event);
|
|
|
|
e = e->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2007-08-27 12:48:14 -04:00
|
|
|
native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex)
|
|
|
|
{
|
|
|
|
DWORD r;
|
|
|
|
struct cond_event_entry entry;
|
|
|
|
|
|
|
|
entry.next = 0;
|
|
|
|
entry.event = CreateEvent(0, FALSE, FALSE, 0);
|
|
|
|
|
|
|
|
/* cond is guarded by mutex */
|
|
|
|
if (cond->next) {
|
|
|
|
cond->last->next = &entry;
|
|
|
|
cond->last = &entry;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cond->next = &entry;
|
|
|
|
cond->last = &entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
native_mutex_unlock(mutex);
|
|
|
|
{
|
|
|
|
r = WaitForSingleObject(entry.event, INFINITE);
|
|
|
|
if (r != WAIT_OBJECT_0) {
|
2008-04-26 05:36:35 -04:00
|
|
|
rb_bug("native_cond_wait: WaitForSingleObject returns %lu", r);
|
2007-08-27 12:48:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
native_mutex_lock(mutex);
|
|
|
|
|
|
|
|
w32_close_handle(entry.event);
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2007-08-27 12:48:14 -04:00
|
|
|
native_cond_initialize(rb_thread_cond_t *cond)
|
|
|
|
{
|
|
|
|
cond->next = 0;
|
|
|
|
cond->last = 0;
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2007-08-27 12:48:14 -04:00
|
|
|
native_cond_destroy(rb_thread_cond_t *cond)
|
|
|
|
{
|
|
|
|
/* */
|
|
|
|
}
|
2007-02-08 15:24:55 -05:00
|
|
|
|
2008-06-13 22:59:19 -04:00
|
|
|
void
|
|
|
|
ruby_init_stack(VALUE *addr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_ERR(expr) \
|
|
|
|
{if (!(expr)) {rb_bug("err: %lu - %s", GetLastError(), #expr);}}
|
|
|
|
|
|
|
|
static void
|
|
|
|
native_thread_init_stack(rb_thread_t *th)
|
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION mi;
|
|
|
|
char *base, *end;
|
|
|
|
DWORD size, space;
|
|
|
|
|
|
|
|
CHECK_ERR(VirtualQuery(&mi, &mi, sizeof(mi)));
|
|
|
|
base = mi.AllocationBase;
|
|
|
|
end = mi.BaseAddress;
|
|
|
|
end += mi.RegionSize;
|
|
|
|
size = end - base;
|
|
|
|
space = size / 5;
|
|
|
|
if (space > 1024*1024) space = 1024*1024;
|
|
|
|
th->machine_stack_start = (VALUE *)end - 1;
|
|
|
|
th->machine_stack_maxsize = size - space;
|
|
|
|
}
|
|
|
|
|
2007-02-08 15:24:55 -05:00
|
|
|
static void
|
|
|
|
native_thread_destroy(rb_thread_t *th)
|
|
|
|
{
|
2007-04-10 03:16:42 -04:00
|
|
|
HANDLE intr = th->native_thread_data.interrupt_event;
|
2008-01-18 01:57:08 -05:00
|
|
|
native_mutex_destroy(&th->interrupt_lock);
|
2007-04-10 03:16:42 -04:00
|
|
|
thread_debug("close handle - intr: %p, thid: %p\n", intr, th->thread_id);
|
|
|
|
th->native_thread_data.interrupt_event = 0;
|
|
|
|
w32_close_handle(intr);
|
2007-02-08 15:24:55 -05:00
|
|
|
}
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-07-21 04:48:29 -04:00
|
|
|
static unsigned long _stdcall
|
2006-12-31 10:02:22 -05:00
|
|
|
thread_start_func_1(void *th_ptr)
|
|
|
|
{
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
rb_thread_t *th = th_ptr;
|
2006-12-31 10:02:22 -05:00
|
|
|
VALUE stack_start;
|
2007-02-08 22:42:46 -05:00
|
|
|
volatile HANDLE thread_id = th->thread_id;
|
|
|
|
|
2008-06-13 22:59:19 -04:00
|
|
|
native_thread_init_stack(th);
|
2007-02-08 22:42:46 -05:00
|
|
|
th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-02-08 22:42:46 -05:00
|
|
|
/* run */
|
2006-12-31 10:02:22 -05:00
|
|
|
thread_debug("thread created (th: %p, thid: %p, event: %p)\n", th,
|
|
|
|
th->thread_id, th->native_thread_data.interrupt_event);
|
2007-07-11 21:19:18 -04:00
|
|
|
thread_start_func_2(th, &stack_start, 0);
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-02-08 22:42:46 -05:00
|
|
|
w32_close_handle(thread_id);
|
2006-12-31 10:02:22 -05:00
|
|
|
thread_debug("thread deleted (th: %p)\n", th);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
native_thread_create(rb_thread_t *th)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2007-02-08 15:24:55 -05:00
|
|
|
size_t stack_size = 4 * 1024; /* 4KB */
|
|
|
|
th->thread_id = w32_create_thread(stack_size, thread_start_func_1, th);
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-02-08 15:24:55 -05:00
|
|
|
if ((th->thread_id) == 0) {
|
|
|
|
st_delete_wrap(th->vm->living_threads, th->self);
|
2006-12-31 10:02:22 -05:00
|
|
|
rb_raise(rb_eThreadError, "can't create Thread (%d)", errno);
|
|
|
|
}
|
2007-02-08 22:42:46 -05:00
|
|
|
|
|
|
|
w32_resume_thread(th->thread_id);
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
if (THREAD_DEBUG) {
|
|
|
|
Sleep(0);
|
|
|
|
thread_debug("create: (th: %p, thid: %p, intr: %p), stack size: %d\n",
|
|
|
|
th, th->thread_id,
|
|
|
|
th->native_thread_data.interrupt_event, stack_size);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-07 04:47:52 -05:00
|
|
|
static void
|
|
|
|
native_thread_join(HANDLE th)
|
|
|
|
{
|
2007-02-23 04:33:53 -05:00
|
|
|
w32_wait_events(&th, 1, 0, 0);
|
2007-01-07 04:47:52 -05:00
|
|
|
}
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
static void
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
native_thread_apply_priority(rb_thread_t *th)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
|
|
|
int priority = th->priority;
|
|
|
|
if (th->priority > 0) {
|
|
|
|
priority = THREAD_PRIORITY_ABOVE_NORMAL;
|
|
|
|
}
|
|
|
|
else if (th->priority < 0) {
|
|
|
|
priority = THREAD_PRIORITY_BELOW_NORMAL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
priority = THREAD_PRIORITY_NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetThreadPriority(th->thread_id, priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-20 05:47:53 -05:00
|
|
|
ubf_handle(void *ptr)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2008-07-15 05:14:09 -04:00
|
|
|
static int checked = 0;
|
|
|
|
typedef BOOL (WINAPI *cancel_io_func_t)(HANDLE);
|
|
|
|
static cancel_io_func_t cancel_func = NULL;
|
2007-11-20 05:47:53 -05:00
|
|
|
rb_thread_t *th = (rb_thread_t *)ptr;
|
2007-02-08 06:51:40 -05:00
|
|
|
thread_debug("ubf_handle: %p\n", th);
|
2008-07-15 05:14:09 -04:00
|
|
|
|
|
|
|
if (!checked) {
|
|
|
|
cancel_func = (cancel_io_func_t)GetProcAddress(GetModuleHandle("kernel32"), "CancelSynchronousIo");
|
|
|
|
checked = 1;
|
|
|
|
}
|
|
|
|
if (cancel_func)
|
|
|
|
cancel_func((HANDLE)th->thread_id);
|
|
|
|
|
2007-02-08 22:42:46 -05:00
|
|
|
w32_set_event(th->native_thread_data.interrupt_event);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
2007-01-07 04:47:52 -05:00
|
|
|
static HANDLE timer_thread_id = 0;
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-07-21 04:48:29 -04:00
|
|
|
static unsigned long _stdcall
|
2006-12-31 10:02:22 -05:00
|
|
|
timer_thread_func(void *dummy)
|
|
|
|
{
|
|
|
|
thread_debug("timer_thread\n");
|
|
|
|
while (system_working) {
|
|
|
|
Sleep(WIN32_WAIT_TIMEOUT);
|
2008-07-05 09:22:29 -04:00
|
|
|
timer_thread_function(dummy);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
thread_debug("timer killed\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_thread_create_timer_thread(void)
|
|
|
|
{
|
2007-01-07 04:47:52 -05:00
|
|
|
if (timer_thread_id == 0) {
|
2008-07-09 05:17:09 -04:00
|
|
|
timer_thread_id = w32_create_thread(1024 + (THREAD_DEBUG ? BUFSIZ : 0),
|
|
|
|
timer_thread_func, GET_VM());
|
2007-02-08 22:42:46 -05:00
|
|
|
w32_resume_thread(timer_thread_id);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
|