From eb35c3d86517764a858b95d81063a1e592c9993f Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Sat, 3 Feb 2018 07:16:29 +0000 Subject: [PATCH] added moo_purgesemiosbyhandle() --- moo/lib/exec.c | 28 ++++++++++++++++++++++++++++ moo/lib/main.c | 2 +- moo/lib/moo.h | 9 +++++++++ moo/mod/sck.c | 1 + 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/moo/lib/exec.c b/moo/lib/exec.c index bbdc8cf..d01f5bc 100644 --- a/moo/lib/exec.c +++ b/moo/lib/exec.c @@ -1341,6 +1341,34 @@ static int delete_from_sem_io (moo_t* moo, moo_oop_semaphore_t sem, int force) return 0; } +void moo_purgesemiosbyhandle (moo_t* moo, moo_ooi_t io_handle) +{ + moo_ooi_t index; + moo_oop_semaphore_t sem; + + if (io_handle < moo->sem_io_map_capa) + { + index = moo->sem_io_map[io_handle]; + if (index >= 0) + { + MOO_ASSERT(moo, moo->sem_io_tuple[index].handle == io_handle); + sem = moo->sem_io_tuple[index].sem[MOO_SEMAPHORE_IO_TYPE_INPUT]; + if (sem) delete_from_sem_io (moo, sem, 0); + } + } + + if (io_handle < moo->sem_io_map_capa) + { + index = moo->sem_io_map[io_handle]; + if (index >= 0) + { + MOO_ASSERT(moo, moo->sem_io_tuple[index].handle == io_handle); + sem = moo->sem_io_tuple[index].sem[MOO_SEMAPHORE_IO_TYPE_OUTPUT]; + if (sem) delete_from_sem_io (moo, sem, 0); + } + } +} + static void _signal_io_semaphore (moo_t* moo, moo_oop_semaphore_t sem) { moo_oop_process_t proc; diff --git a/moo/lib/main.c b/moo/lib/main.c index 0f8d2ec..23e377c 100644 --- a/moo/lib/main.c +++ b/moo/lib/main.c @@ -599,7 +599,7 @@ static void* dl_open (moo_t* moo, const moo_ooch_t* name, int flags) static void dl_close (moo_t* moo, void* handle) { -#if defined(USE_LTDL) | defined(USE_DLFCN) +#if defined(USE_LTDL) || defined(USE_DLFCN) MOO_DEBUG1 (moo, "Closed DL handle %p\n", handle); sys_dl_close (handle); diff --git a/moo/lib/moo.h b/moo/lib/moo.h index 3473f7d..10e4caa 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -2152,6 +2152,15 @@ MOO_EXPORT const moo_ooch_t* moo_errnum_to_errstr ( moo_errnum_t errnum ); +/** + * The moo_purgesemiosbyhandle() function deletes IO semaphores associated + * with the given IO handle. It can be called usefaully when you close + * a IO handle with dangling semaphores associated. + */ +MOO_EXPORT void moo_purgesemiosbyhandle ( + moo_t* moo, + moo_ooi_t io_handle +); #if defined(MOO_INCLUDE_COMPILER) diff --git a/moo/mod/sck.c b/moo/mod/sck.c index 9df0284..e36c7e6 100644 --- a/moo/mod/sck.c +++ b/moo/mod/sck.c @@ -132,6 +132,7 @@ static moo_pfrc_t pf_close_socket (moo_t* moo, moo_ooi_t nargs) fd = MOO_OOP_TO_SMOOI(sck->handle); if (fd >= 0) { + moo_purgesemiobyhandle (moo, MOO_OOP_TO_SMOOI(sck->handle)); if (close(MOO_OOP_TO_SMOOI(sck->handle)) == -1) { moo_seterrwithsyserr (moo, errno);