summaryrefslogtreecommitdiff
path: root/thread_win32.c
diff options
context:
space:
mode:
authorKoichi Sasada <ko1@atdot.net>2020-11-17 16:40:47 +0900
committerKoichi Sasada <ko1@atdot.net>2020-11-18 03:52:41 +0900
commit5e3259ea7490a2542d78c433eb8c9d44c7819e61 (patch)
tree1a280e725a2c66ca776086661e3b707fd73f4577 /thread_win32.c
parent0683912db888b0421ce4c40ad450ccf75ad7e3f4 (diff)
fix public interface
To make some kind of Ractor related extensions, some functions should be exposed. * include/ruby/thread_native.h * rb_native_mutex_* * rb_native_cond_* * include/ruby/ractor.h * RB_OBJ_SHAREABLE_P(obj) * rb_ractor_shareable_p(obj) * rb_ractor_std*() * rb_cRactor and rm ractor_pub.h and rename srcdir/ractor.h to srcdir/ractor_core.h (to avoid conflict with include/ruby/ractor.h)
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3775
Diffstat (limited to 'thread_win32.c')
-rw-r--r--thread_win32.c39
1 files changed, 14 insertions, 25 deletions
diff --git a/thread_win32.c b/thread_win32.c
index 132e52c..f4b4ea2 100644
--- a/thread_win32.c
+++ b/thread_win32.c
@@ -52,12 +52,12 @@ w32_error(const char *func)
}
static int
-w32_mutex_lock(HANDLE lock)
+w32_mutex_lock(HANDLE lock, bool try)
{
DWORD result;
while (1) {
thread_debug("rb_native_mutex_lock: %p\n", lock);
- result = w32_wait_events(&lock, 1, INFINITE, 0);
+ result = w32_wait_events(&lock, 1, try ? 0 : INFINITE, 0);
switch (result) {
case WAIT_OBJECT_0:
/* get mutex object */
@@ -70,7 +70,7 @@ w32_mutex_lock(HANDLE lock)
return 0;
case WAIT_TIMEOUT:
thread_debug("timeout mutex: %p\n", lock);
- break;
+ return EBUSY;
case WAIT_ABANDONED:
rb_bug("win32_mutex_lock: WAIT_ABANDONED");
break;
@@ -97,7 +97,7 @@ w32_mutex_create(void)
static void
gvl_acquire(rb_global_vm_lock_t *gvl, rb_thread_t *th)
{
- w32_mutex_lock(gvl->lock);
+ w32_mutex_lock(gvl->lock, false);
if (GVL_DEBUG) fprintf(stderr, "gvl acquire (%p): acquire\n", th);
}
@@ -323,41 +323,30 @@ void
rb_native_mutex_lock(rb_nativethread_lock_t *lock)
{
#if USE_WIN32_MUTEX
- w32_mutex_lock(lock->mutex);
+ w32_mutex_lock(lock->mutex, false);
#else
EnterCriticalSection(&lock->crit);
#endif
}
-void
-rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
+int
+rb_native_mutex_trylock(rb_nativethread_lock_t *lock)
{
#if USE_WIN32_MUTEX
- thread_debug("release mutex: %p\n", lock->mutex);
- ReleaseMutex(lock->mutex);
+ return w32_mutex_lock(lock->mutex, true);
#else
- LeaveCriticalSection(&lock->crit);
+ return TryEnterCriticalSection(&lock->crit) == 0 ? EBUSY : 0;
#endif
}
-RBIMPL_ATTR_MAYBE_UNUSED()
-static int
-native_mutex_trylock(rb_nativethread_lock_t *lock)
+void
+rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
{
#if USE_WIN32_MUTEX
- int result;
- thread_debug("native_mutex_trylock: %p\n", lock->mutex);
- result = w32_wait_events(&lock->mutex, 1, 1, 0);
- thread_debug("native_mutex_trylock result: %d\n", result);
- switch (result) {
- case WAIT_OBJECT_0:
- return 0;
- case WAIT_TIMEOUT:
- return EBUSY;
- }
- return EINVAL;
+ thread_debug("release mutex: %p\n", lock->mutex);
+ ReleaseMutex(lock->mutex);
#else
- return TryEnterCriticalSection(&lock->crit) == 0;
+ LeaveCriticalSection(&lock->crit);
#endif
}