From ca182c122c2ea134d9c197f657ae63abd64fa82c Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Fri, 19 Jan 2018 08:26:58 +0000 Subject: [PATCH] added the syserrstrb and syserrstrb callback. either one must be supplied. added some conditionals for nanosleep, strerror_r, pthread --- moo/configure.ac | 3 ++- moo/lib/err.c | 14 +++++++++++--- moo/lib/main.c | 29 +++++++++++++++++++++-------- moo/lib/moo.c | 6 ++++++ moo/lib/moo.h | 4 ++++ 5 files changed, 44 insertions(+), 12 deletions(-) diff --git a/moo/configure.ac b/moo/configure.ac index 405e186..49bb9d5 100644 --- a/moo/configure.ac +++ b/moo/configure.ac @@ -171,7 +171,8 @@ dnl check functions AC_CHECK_FUNCS([gettimeofday settimeofday clock_gettime clock_settime getitimer setitimer]) AC_CHECK_FUNCS([backtrace backtrace_symbols]) AC_CHECK_FUNCS([makecontext swapcontext getcontext setcontext]) -AC_CHECK_FUNCS([snprintf _vsnprintf _vsnwprintf]) +AC_CHECK_FUNCS([clock_nanosleep nanosleep usleep]) +AC_CHECK_FUNCS([snprintf _vsnprintf _vsnwprintf strerror_r]) AC_CHECK_FUNCS([accept4]) AC_CHECK_LIB([dyncall_s], [dcNewCallVM], diff --git a/moo/lib/err.c b/moo/lib/err.c index cefc40f..aefc4de 100644 --- a/moo/lib/err.c +++ b/moo/lib/err.c @@ -27,7 +27,6 @@ #include "moo-prv.h" - /* BEGIN: GENERATED WITH generr.moo */ static moo_ooch_t errstr_0[] = {'n','o',' ','e','r','r','o','r','\0'}; @@ -309,8 +308,17 @@ const moo_ooch_t* moo_geterrmsg (moo_t* moo) void moo_seterrwithsyserr (moo_t* moo, int syserr) { - 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); + if (moo->vmprim.syserrstrb) + { + moo->vmprim.syserrstrb (moo, syserr, moo->errmsg.tmpbuf.bch, MOO_COUNTOF(moo->errmsg.tmpbuf.bch)); + moo_seterrbfmt (moo, moo_syserr_to_errnum(errno), "%hs", moo->errmsg.tmpbuf.bch); + } + else + { + MOO_ASSERT (moo, moo->vmprim.syserrstru != MOO_NULL); + moo->vmprim.syserrstru (moo, syserr, moo->errmsg.tmpbuf.uch, MOO_COUNTOF(moo->errmsg.tmpbuf.uch)); + moo_seterrbfmt (moo, moo_syserr_to_errnum(errno), "%ls", moo->errmsg.tmpbuf.uch); + } } /* -------------------------------------------------------------------------- diff --git a/moo/lib/main.c b/moo/lib/main.c index 15813c7..f9036fe 100644 --- a/moo/lib/main.c +++ b/moo/lib/main.c @@ -34,7 +34,7 @@ #include #include -#if !defined(__DOS__) +#if !defined(__DOS__) && defined(HAVE_PTHREAD) && defined(HAVE_STRERROR_R) # define USE_THREAD #endif @@ -818,6 +818,16 @@ static void log_write (moo_t* moo, moo_oow_t mask, const moo_ooch_t* msg, moo_oo #endif } +static void syserrstrb (moo_t* moo, int syserr, moo_bch_t* buf, moo_oow_t len) +{ +#if defined(HAVE_STRERROR_R) + strerror_r (syserr, buf, len); +#else + /* this is not thread safe */ + moo_copybcstr (buf, len, strerror(syserr)); +#endif +} + /* ========================================================================= */ static int _add_poll_fd (moo_t* moo, int fd, int event_mask) { @@ -1821,19 +1831,21 @@ static void vm_sleep (moo_t* moo, const moo_ntime_t* dur) } #else - #if defined(USE_THREAD) /* the sleep callback is called only if there is no IO semaphore * waiting. so i can safely call vm_muxwait() without a muxwait callback * when USE_THREAD is true */ - vm_muxwait (moo, dur, MOO_NULL); + vm_muxwait (moo, dur, MOO_NULL); + #elif defined(HAVE_NANOSLEEP) + struct timespec ts; + ts.tv_sec = dur->sec; + ts.tv_nsec = dur->nsec; + nanosleep (&ts, MOO_NULL); + #elif defined(HAVE_USLEEP) + usleep (MOO_SECNSEC_TO_USEC(dur->sec, dur->nsec)); #else - struct timespec ts; - ts.tv_sec = dur->sec; - ts.tv_nsec = dur->nsec; - nanosleep (&ts, MOO_NULL); + # error UNSUPPORT SLEEP #endif - #endif } /* ========================================================================= */ @@ -2205,6 +2217,7 @@ int main (int argc, char* argv[]) vmprim.dl_close = dl_close; vmprim.dl_getsym = dl_getsym; vmprim.log_write = log_write; + vmprim.syserrstrb = syserrstrb; vmprim.vm_startup = vm_startup; vmprim.vm_cleanup = vm_cleanup; vmprim.vm_gettime = vm_gettime; diff --git a/moo/lib/moo.c b/moo/lib/moo.c index bc23516..8de89b4 100644 --- a/moo/lib/moo.c +++ b/moo/lib/moo.c @@ -86,6 +86,12 @@ int moo_init (moo_t* moo, moo_mmgr_t* mmgr, moo_oow_t heapsz, const moo_vmprim_t { int modtab_inited = 0; + if (!vmprim->syserrstrb && !vmprim->syserrstru) + { + moo_seterrnum (moo, MOO_EINVAL); + return -1; + } + MOO_MEMSET (moo, 0, MOO_SIZEOF(*moo)); moo->mmgr = mmgr; moo->cmgr = moo_getutf8cmgr (); diff --git a/moo/lib/moo.h b/moo/lib/moo.h index 52de00f..100b020 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -928,6 +928,8 @@ typedef void (*moo_vmprim_dlclose_t) (moo_t* moo, void* handle); typedef void* (*moo_vmprim_dlsym_t) (moo_t* moo, void* handle, const moo_ooch_t* name); typedef void (*moo_log_write_t) (moo_t* moo, moo_oow_t mask, const moo_ooch_t* msg, moo_oow_t len); +typedef void (*moo_syserrstrb_t) (moo_t* moo, int syserr, moo_bch_t* buf, moo_oow_t len); +typedef void (*moo_syserrstru_t) (moo_t* moo, int syserr, moo_uch_t* buf, moo_oow_t len); typedef int (*moo_vmprim_startup_t) (moo_t* moo); typedef void (*moo_vmprim_cleanup_t) (moo_t* moo); @@ -973,6 +975,8 @@ struct moo_vmprim_t moo_vmprim_dlclose_t dl_close; moo_vmprim_dlsym_t dl_getsym; moo_log_write_t log_write; + moo_syserrstrb_t syserrstrb; + moo_syserrstru_t syserrstru; moo_vmprim_startup_t vm_startup; moo_vmprim_cleanup_t vm_cleanup;