From 09585a4ea5291a60b4eafd167e5865399d8df299 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Thu, 28 Dec 2017 16:12:10 +0000 Subject: [PATCH] initialized sem_gcfin in vm_startup(). fixed a new problem in error message output bound for stderr. fixed wrong timeout value calculatin in exec.c --- moo/kernel/System.moo | 1 + moo/lib/err.c | 5 ++--- moo/lib/exec.c | 39 +++++++++++++++++++++++++++------------ moo/lib/main.c | 30 +++++++++++++++++++----------- moo/lib/moo.h | 7 ++++++- 5 files changed, 55 insertions(+), 27 deletions(-) diff --git a/moo/kernel/System.moo b/moo/kernel/System.moo index 1f306c2..7295b29 100644 --- a/moo/kernel/System.moo +++ b/moo/kernel/System.moo @@ -98,6 +98,7 @@ class System(Apex) method(#class,#primitive) _popCollectable. method(#class,#primitive) collectGarbage. + method(#class,#primitive) gc. method(#class,#primitive) return: object to: context. ## ======================================================================================= diff --git a/moo/lib/err.c b/moo/lib/err.c index 2952413..acb69ce 100644 --- a/moo/lib/err.c +++ b/moo/lib/err.c @@ -307,9 +307,8 @@ const moo_ooch_t* moo_geterrmsg (moo_t* moo) void moo_seterrwithsyserr (moo_t* moo, int syserr) { - moo_bch_t msgbuf[64]; - strerror_r (syserr, msgbuf, MOO_COUNTOF(msgbuf)); - moo_seterrbfmt (moo, moo_syserr_to_errnum(errno), "%hs", msgbuf); + strerror_r (syserr, moo->errmsg.tmpbuf.bch, MOO_COUNTOF(moo->errmsg.tmpbuf.bch)); + moo_seterrbfmt (moo, moo_syserr_to_errnum(errno), "%hs", moo->errmsg.tmpbuf.bch); } /* -------------------------------------------------------------------------- diff --git a/moo/lib/exec.c b/moo/lib/exec.c index 94fc28f..7a48ed1 100644 --- a/moo/lib/exec.c +++ b/moo/lib/exec.c @@ -140,6 +140,9 @@ static MOO_INLINE int vm_startup (moo_t* moo) moo->sem_io_map[i] = -1; } + moo->sem_gcfin = (moo_oop_semaphore_t)moo->_nil; + moo->sem_gcfin_sigreq = 0; + if (moo->vmprim.vm_startup (moo) <= -1) return -1; moo->vmprim.vm_gettime (moo, &moo->exec_start_time); /* raw time. no adjustment */ @@ -181,6 +184,9 @@ static MOO_INLINE void vm_cleanup (moo_t* moo) moo->vmprim.vm_gettime (moo, &moo->exec_end_time); /* raw time. no adjustment */ moo->vmprim.vm_cleanup (moo); + + moo->sem_gcfin = (moo_oop_semaphore_t)moo->_nil; + moo->sem_gcfin_sigreq = 0; MOO_LOG0 (moo, MOO_LOG_VM | MOO_LOG_DEBUG, "VM cleaned up\n"); } @@ -1132,8 +1138,8 @@ static int add_to_sem_io (moo_t* moo, moo_oop_semaphore_t sem, moo_ooi_t io_hand tmp = moo_reallocmem (moo, moo->sem_io_map, MOO_SIZEOF(*tmp) * new_capa); if (!tmp) { - moo_copyoocstr (moo->errmsg.buf2, MOO_COUNTOF(moo->errmsg.buf2), moo->errmsg.buf); - moo_seterrbfmt (moo, moo->errnum, "handle %zd out of supported range - %js", moo->errmsg.buf2); + moo_copyoocstr (moo->errmsg.tmpbuf.ooch, MOO_COUNTOF(moo->errmsg.tmpbuf.ooch), moo->errmsg.buf); + moo_seterrbfmt (moo, moo->errnum, "handle %zd out of supported range - %js", moo->errmsg.tmpbuf.ooch); return -1; } @@ -2566,8 +2572,8 @@ static moo_pfrc_t __system_add_io_semaphore (moo_t* moo, moo_ooi_t nargs, moo_se if (add_to_sem_io(moo, sem, MOO_OOP_TO_SMOOI(fd), io_type) <= -1) { - moo_copyoocstr (moo->errmsg.buf2, MOO_COUNTOF(moo->errmsg.buf2), moo->errmsg.buf); - moo_seterrbfmt (moo, moo->errnum, "cannot add the handle %zd to the multiplexer - %js", MOO_OOP_TO_SMOOI(fd), moo->errmsg.buf2); + moo_copyoocstr (moo->errmsg.tmpbuf.ooch, MOO_COUNTOF(moo->errmsg.tmpbuf.ooch), moo->errmsg.buf); + moo_seterrbfmt (moo, moo->errnum, "cannot add the handle %zd to the multiplexer - %js", MOO_OOP_TO_SMOOI(fd), moo->errmsg.tmpbuf.ooch); return MOO_PF_FAILURE; } @@ -2626,8 +2632,8 @@ static moo_pfrc_t pf_system_remove_semaphore (moo_t* moo, moo_ooi_t nargs) if (delete_from_sem_io (moo, sem) <= -1) { - moo_copyoocstr (moo->errmsg.buf2, MOO_COUNTOF(moo->errmsg.buf2), moo->errmsg.buf); - moo_seterrbfmt (moo, moo->errnum, "cannot delete the handle %zd from the multiplexer - %js", MOO_OOP_TO_SMOOI(sem->io_handle), moo->errmsg.buf2); + moo_copyoocstr (moo->errmsg.tmpbuf.ooch, MOO_COUNTOF(moo->errmsg.tmpbuf.ooch), moo->errmsg.buf); + moo_seterrbfmt (moo, moo->errnum, "cannot delete the handle %zd from the multiplexer - %js", MOO_OOP_TO_SMOOI(sem->io_handle), moo->errmsg.tmpbuf.ooch); return MOO_PF_FAILURE; } @@ -3328,6 +3334,7 @@ static pf_t pftab[] = { "System_collectGarbage", { moo_pf_system_collect_garbage, 0, 0 } }, { "System_free", { moo_pf_system_free, 1, 1 } }, { "System_free:", { moo_pf_system_free, 1, 1 } }, + { "System_gc", { moo_pf_system_collect_garbage, 0, 0 } }, { "System_getBytes", { moo_pf_system_get_bytes, 5, 5 } }, { "System_getInt16", { moo_pf_system_get_int16, 2, 2 } }, { "System_getInt32", { moo_pf_system_get_int32, 2, 2 } }, @@ -3872,7 +3879,7 @@ static MOO_INLINE int switch_process_if_needed (moo_t* moo) } else if (moo->processor->active == moo->nil_process) { - /* no running process */ + /* no running process. before firing time. */ MOO_SUBNTIME (&ft, &ft, (moo_ntime_t*)&now); if (moo->sem_io_wait_count > 0) @@ -3907,23 +3914,31 @@ static MOO_INLINE int switch_process_if_needed (moo_t* moo) if (moo->sem_io_wait_count > 0) { - moo_ntime_t now; - if (moo->processor->active == moo->nil_process) { + moo_ntime_t ft; + /* no runnable process while there is an io semaphore being waited */ if ((moo_oop_t)moo->sem_gcfin != moo->_nil && moo->sem_gcfin_sigreq) goto signal_sem_gcfin; do { - vm_gettime (moo, &now); - now.sec += 3; /* TODO: use a configured value? */ - vm_muxwait (moo, &now); + + MOO_INITNTIME (&ft, 3, 0); /* TODO: use a configured time */ + vm_muxwait (moo, &ft); } while (moo->processor->active == moo->nil_process && !moo->abort_req); } else { + /* well, there is a process waiting on one or more semaphores while + * there are other normal processes to run. check IO activities + * before proceeding to handle normal process scheduling */ + + /* NOTE: the check with the multiplexer may happen too frequently + * because this is called everytime process switching is requested + * the actual callback implementation may avoid using actual + * system calls for the check too frequently */ vm_muxwait (moo, MOO_NULL); } } diff --git a/moo/lib/main.c b/moo/lib/main.c index b3ff6d8..5cc5996 100644 --- a/moo/lib/main.c +++ b/moo/lib/main.c @@ -725,15 +725,22 @@ static void log_write (moo_t* moo, moo_oow_t mask, const moo_ooch_t* msg, moo_oo xtn_t* xtn = moo_getxtn(moo); int logfd; - 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 if (mask & MOO_LOG_STDERR) logfd = 2; + if (mask & MOO_LOG_STDERR) + { + /* the messages that go to STDERR don't get masked out */ + logfd = 2; + } else { - logfd = xtn->logfd; - if (logfd <= -1) return; + 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; + } } /* TODO: beautify the log message. @@ -1583,7 +1590,7 @@ static void vm_muxwait (moo_t* moo, const moo_ntime_t* dur, moo_vmprim_muxwait_c #endif { moo_uint8_t u8; - while (read (xtn->p[0], &u8, MOO_SIZEOF(u8)) > 0) + while (read(xtn->p[0], &u8, MOO_SIZEOF(u8)) > 0) { /* consume as much as possible */; if (u8 == 'Q') xtn->iothr_abort = 1; @@ -1707,7 +1714,8 @@ static void vm_muxwait (moo_t* moo, const moo_ntime_t* dur, moo_vmprim_muxwait_c if (n <= -1) { - MOO_DEBUG2 (moo, "Warning: multiplexer wait failure - %d, %hs\n", errno, strerror(errno)); + moo_seterrwithsyserr (moo, errno); + MOO_DEBUG2 (moo, "Warning: multiplexer wait failure - %d, %s\n", errno, moo_geterrmsg(moo)); } else { @@ -2316,7 +2324,7 @@ int main (int argc, char* argv[]) } else { - moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, "ERROR: cannot compile code - [%d] %js\n", moo_geterrnum(moo), moo_geterrstr(moo)); + moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, "ERROR: cannot compile code - [%d] %js\n", moo_geterrnum(moo), moo_geterrmsg(moo)); } close_moo (moo); @@ -2339,7 +2347,7 @@ int main (int argc, char* argv[]) mthname.len = 4; if (moo_invoke (moo, &objname, &mthname) <= -1) { - moo_logbfmt (moo, MOO_LOG_ERROR, "ERROR: cannot execute code - [%d] %js\n", moo_geterrnum(moo), moo_geterrmsg(moo)); + moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, "ERROR: cannot execute code - [%d] %js\n", moo_geterrnum(moo), moo_geterrmsg(moo)); xret = -1; } diff --git a/moo/lib/moo.h b/moo/lib/moo.h index eae5384..3551f7f 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -1164,7 +1164,12 @@ struct moo_t struct { - moo_ooch_t buf2[2048]; + union + { + moo_ooch_t ooch[2048]; + moo_bch_t bch[2048]; + moo_uch_t uch[2048]; + } tmpbuf; moo_ooch_t buf[2048]; moo_oow_t len; } errmsg;