diff --git a/moo/lib/exec.c b/moo/lib/exec.c index d01f5bc..edaa541 100644 --- a/moo/lib/exec.c +++ b/moo/lib/exec.c @@ -1341,34 +1341,6 @@ 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; @@ -1420,6 +1392,41 @@ static void signal_io_semaphore (moo_t* moo, moo_ooi_t io_handle, moo_ooi_t mask MOO_LOG1 (moo, MOO_LOG_WARN, "Warning - semaphore signaling requested on an unmapped handle %zd\n", io_handle); } } + +void moo_releaseiohandle (moo_t* moo, moo_ooi_t io_handle) +{ + /* TODO: optimize io semapore unmapping. since i'm to close the handle, + * i don't need to call delete_from_sem_io() seperately for input + * and output. */ + if (io_handle < moo->sem_io_map_capa) + { + moo_ooi_t index; + moo_oop_semaphore_t sem; + + 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) + { + moo_ooi_t index; + moo_oop_semaphore_t sem; + + 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 moo_oop_process_t start_initial_process (moo_t* moo, moo_oop_context_t c) diff --git a/moo/lib/main.c b/moo/lib/main.c index 23e377c..894e725 100644 --- a/moo/lib/main.c +++ b/moo/lib/main.c @@ -553,7 +553,10 @@ static void* dl_open (moo_t* moo, const moo_ooch_t* name, int flags) goto retry; } } - else MOO_DEBUG3 (moo, "Opened(ext) DL %hs[%js] handle %p\n", &bufptr[len], name, handle); + else + { + MOO_DEBUG3 (moo, "Opened(ext) DL %hs[%js] handle %p\n", &bufptr[len], name, handle); + } } else { @@ -749,12 +752,13 @@ static void log_write (moo_t* moo, moo_oow_t mask, const moo_ooch_t* msg, moo_oo } else { - if (!(xtn->logmask & mask & ~MOO_LOG_ALL_LEVELS)) return; /* check log types */ - if (!(xtn->logmask & mask & ~MOO_LOG_ALL_TYPES)) return; /* check log levels */ + if (!(xtn->logmask & mask & ~MOO_LOG_ALL_LEVELS)) return; /* check log types */ + if (!(xtn->logmask & mask & ~MOO_LOG_ALL_TYPES)) return; /* check log levels */ if (mask & MOO_LOG_STDOUT) logfd = 1; else { + logfd = xtn->logfd; if (logfd <= -1) return; } @@ -2045,7 +2049,7 @@ static void clear_sigterm (void) } /* ========================================================================= */ -static void close_moo (moo_t* moo) +static void fini_moo (moo_t* moo) { xtn_t* xtn = moo_getxtn(moo); if (xtn->logfd >= 0) @@ -2054,7 +2058,6 @@ static void close_moo (moo_t* moo) xtn->logfd = -1; xtn->logfd_istty = 0; } - moo_close (moo); } static int handle_logopt (moo_t* moo, const moo_bch_t* str) @@ -2171,6 +2174,7 @@ int main (int argc, char* argv[]) moo_oocs_t objname; moo_oocs_t mthname; moo_vmprim_t vmprim; + moo_cb_t moocb; int i, xret; moo_bci_t c; @@ -2300,11 +2304,15 @@ int main (int argc, char* argv[]) xtn->logfd = -1; xtn->logfd_istty = 0; + memset (&moocb, 0, MOO_SIZEOF(moocb)); + moocb.fini = fini_moo; + moo_regcb (moo, &moocb); + if (logopt) { if (handle_logopt (moo, logopt) <= -1) { - close_moo (moo); + moo_close (moo); return -1; } } @@ -2319,7 +2327,7 @@ int main (int argc, char* argv[]) { if (handle_dbgopt (moo, dbgopt) <= -1) { - close_moo (moo); + moo_close (moo); return -1; } } @@ -2328,7 +2336,7 @@ int main (int argc, char* argv[]) if (moo_ignite(moo) <= -1) { moo_logbfmt (moo, MOO_LOG_STDERR, "ERROR: cannot ignite moo - [%d] %js\n", moo_geterrnum(moo), moo_geterrstr(moo)); - close_moo (moo); + moo_close (moo); return -1; } @@ -2380,7 +2388,7 @@ int main (int argc, char* argv[]) moo_logbfmt (moo, MOO_LOG_STDERR, "ERROR: cannot compile code - [%d] %js\n", moo_geterrnum(moo), moo_geterrmsg(moo)); } - close_moo (moo); + moo_close (moo); #if defined(USE_LTDL) lt_dlexit (); #endif @@ -2411,7 +2419,7 @@ int main (int argc, char* argv[]) /*moo_dumpsymtab(moo); *moo_dumpdic(moo, moo->sysdic, "System dictionary");*/ - close_moo (moo); + moo_close (moo); #if defined(USE_LTDL) lt_dlexit (); diff --git a/moo/lib/moo.c b/moo/lib/moo.c index 8de89b4..f07d49d 100644 --- a/moo/lib/moo.c +++ b/moo/lib/moo.c @@ -203,14 +203,14 @@ void moo_fini (moo_t* moo) moo->proc_map_free_last = -1; } + moo_rbt_walk (&moo->modtab, unload_module, moo); /* unload all modules */ + moo_rbt_fini (&moo->modtab); + for (cb = moo->cblist; cb; cb = cb->next) { if (cb->fini) cb->fini (moo); } - moo_rbt_walk (&moo->modtab, unload_module, moo); /* unload all modules */ - moo_rbt_fini (&moo->modtab); - /* TOOD: persistency? storing objects to image file? */ moo_killheap (moo, moo->newheap); diff --git a/moo/lib/moo.h b/moo/lib/moo.h index 10e4caa..1469fa0 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -2153,11 +2153,11 @@ MOO_EXPORT const moo_ooch_t* moo_errnum_to_errstr ( ); /** - * 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. + * The moo_releaseiohandle() function deletes resources associated with + * the given IO handle. The resources include IO semaphores. You should call + * this function before closing an IO handle. */ -MOO_EXPORT void moo_purgesemiosbyhandle ( +MOO_EXPORT void moo_releaseiohandle ( moo_t* moo, moo_ooi_t io_handle ); diff --git a/moo/mod/sck.c b/moo/mod/sck.c index e36c7e6..3bbb144 100644 --- a/moo/mod/sck.c +++ b/moo/mod/sck.c @@ -132,7 +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)); + moo_releaseiohandle (moo, MOO_OOP_TO_SMOOI(sck->handle)); if (close(MOO_OOP_TO_SMOOI(sck->handle)) == -1) { moo_seterrwithsyserr (moo, errno);