From 559d1258740a1d947a7c3a44403f855a4638204e Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Wed, 30 Jan 2019 10:18:58 +0000 Subject: [PATCH] fixed error strings. changed time functions --- mio/lib/Makefile.in | 1 - mio/lib/err.c | 76 ++++++++++------------ mio/lib/generr.awk | 55 ++++++++++++++++ mio/lib/main.c | 20 +++--- mio/lib/mio-prv.h | 8 ++- mio/lib/mio-sck.c | 7 +- mio/lib/mio-sys.h | 32 ++++++++- mio/lib/mio.c | 26 ++++++-- mio/lib/mio.h | 55 +++++++++------- mio/lib/sys-log.c | 3 + mio/lib/sys-tim.c | 155 ++++---------------------------------------- mio/lib/sys.c | 6 ++ mio/lib/tmr.c | 4 +- 13 files changed, 215 insertions(+), 233 deletions(-) create mode 100644 mio/lib/generr.awk diff --git a/mio/lib/Makefile.in b/mio/lib/Makefile.in index b17aa12..d76fa83 100644 --- a/mio/lib/Makefile.in +++ b/mio/lib/Makefile.in @@ -365,7 +365,6 @@ pdfdir = @pdfdir@ prefix = @prefix@ program_transform_name = @program_transform_name@ psdir = @psdir@ -runstatedir = @runstatedir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ diff --git a/mio/lib/err.c b/mio/lib/err.c index 27285a0..8a03a5d 100644 --- a/mio/lib/err.c +++ b/mio/lib/err.c @@ -26,48 +26,44 @@ #include "mio-prv.h" -/* TODO: this table is wrong. the messages are wrong as of now. fix these... */ -static mio_ooch_t errstr_0[] = {'n','o',' ','e','r','r','o','r','\0'}; -static mio_ooch_t errstr_1[] = {'g','e','n','e','r','i','c',' ','e','r','r','o','r','\0'}; -static mio_ooch_t errstr_2[] = {'n','o','t',' ','i','m','p','l','e','m','e','n','t','e','d','\0'}; -static mio_ooch_t errstr_3[] = {'s','u','b','s','y','s','t','e','m',' ','e','r','r','o','r','\0'}; -static mio_ooch_t errstr_4[] = {'i','n','t','e','r','n','a','l',' ','e','r','r','o','r',' ','t','h','a','t',' ','s','h','o','u','l','d',' ','n','e','v','e','r',' ','h','a','v','e',' ','h','a','p','p','e','n','e','d','\0'}; -static mio_ooch_t errstr_5[] = {'i','n','s','u','f','f','i','c','i','e','n','t',' ','s','y','s','t','e','m',' ','m','e','m','o','r','y','\0'}; -static mio_ooch_t errstr_6[] = {'i','n','s','u','f','f','i','c','i','e','n','t',' ','o','b','j','e','c','t',' ','m','e','m','o','r','y','\0'}; -static mio_ooch_t errstr_7[] = {'i','n','v','a','l','i','d',' ','c','l','a','s','s','/','t','y','p','e','\0'}; -static mio_ooch_t errstr_8[] = {'i','n','v','a','l','i','d',' ','p','a','r','a','m','e','t','e','r',' ','o','r',' ','a','r','g','u','m','e','n','t','\0'}; -static mio_ooch_t errstr_9[] = {'d','a','t','a',' ','n','o','t',' ','f','o','u','n','d','\0'}; -static mio_ooch_t errstr_10[] = {'e','x','i','s','t','i','n','g','/','d','u','p','l','i','c','a','t','e',' ','d','a','t','a','\0'}; -static mio_ooch_t errstr_11[] = {'b','u','s','y','\0'}; -static mio_ooch_t errstr_12[] = {'a','c','c','e','s','s',' ','d','e','n','i','e','d','\0'}; -static mio_ooch_t errstr_13[] = {'o','p','e','r','a','t','i','o','n',' ','n','o','t',' ','p','e','r','m','i','t','t','e','d','\0'}; -static mio_ooch_t errstr_14[] = {'n','o','t',' ','a',' ','d','i','r','e','c','t','o','r','y','\0'}; -static mio_ooch_t errstr_15[] = {'i','n','t','e','r','r','u','p','t','e','d','\0'}; -static mio_ooch_t errstr_16[] = {'p','i','p','e',' ','e','r','r','o','r','\0'}; -static mio_ooch_t errstr_17[] = {'r','e','s','o','u','r','c','e',' ','t','e','m','p','o','r','a','r','i','l','y',' ','u','n','a','v','a','i','l','a','b','l','e','\0'}; -static mio_ooch_t errstr_18[] = {'b','a','d',' ','s','y','s','t','e','m',' ','h','a','n','d','l','e','\0'}; -static mio_ooch_t errstr_19[] = {'*','*','*',' ','u','n','d','e','f','i','n','e','d',' ','e','r','r','o','r',' ','*','*','*','\0'}; -static mio_ooch_t errstr_20[] = {'m','e','s','s','a','g','e',' ','r','e','c','e','i','v','e','r',' ','e','r','r','o','r','\0'}; -static mio_ooch_t errstr_21[] = {'m','e','s','s','a','g','e',' ','s','e','n','d','i','n','g',' ','e','r','r','o','r','\0'}; -static mio_ooch_t errstr_22[] = {'w','r','o','n','g',' ','n','u','m','b','e','r',' ','o','f',' ','a','r','g','u','m','e','n','t','s','\0'}; -static mio_ooch_t errstr_23[] = {'r','a','n','g','e',' ','e','r','r','o','r','\0'}; -static mio_ooch_t errstr_24[] = {'b','y','t','e','-','c','o','d','e',' ','f','u','l','l','\0'}; -static mio_ooch_t errstr_25[] = {'d','i','c','t','i','o','n','a','r','y',' ','f','u','l','l','\0'}; -static mio_ooch_t errstr_26[] = {'p','r','o','c','e','s','s','o','r',' ','f','u','l','l','\0'}; -static mio_ooch_t errstr_27[] = {'t','o','o',' ','m','a','n','y',' ','s','e','m','a','p','h','o','r','e','s','\0'}; -static mio_ooch_t errstr_28[] = {'*','*','*',' ','u','n','d','e','f','i','n','e','d',' ','e','r','r','o','r',' ','*','*','*','\0'}; -static mio_ooch_t errstr_29[] = {'d','i','v','i','d','e',' ','b','y',' ','z','e','r','o','\0'}; -static mio_ooch_t errstr_30[] = {'I','/','O',' ','e','r','r','o','r','\0'}; -static mio_ooch_t errstr_31[] = {'e','n','c','o','d','i','n','g',' ','c','o','n','v','e','r','s','i','o','n',' ','e','r','r','o','r','\0'}; -static mio_ooch_t errstr_32[] = {'i','n','s','u','f','f','i','c','i','e','n','t',' ','d','a','t','a',' ','f','o','r',' ','e','n','c','o','d','i','n','g',' ','c','o','n','v','e','r','s','i','o','n','\0'}; -static mio_ooch_t errstr_33[] = {'b','u','f','f','e','r',' ','f','u','l','l','\0'}; +static mio_ooch_t errstr_0[] = {'n', 'o', ' ', 'e', 'r', 'r', 'o', 'r', '\0' }; +static mio_ooch_t errstr_1[] = {'g', 'e', 'n', 'e', 'r', 'i', 'c', ' ', 'e', 'r', 'r', 'o', 'r', '\0' }; +static mio_ooch_t errstr_2[] = {'n', 'o', 't', ' ', 'i', 'm', 'p', 'l', 'e', 'm', 'e', 'n', 't', 'e', 'd', '\0' }; +static mio_ooch_t errstr_3[] = {'s', 'y', 's', 't', 'e', 'm', ' ', 'e', 'r', 'r', 'o', 'r', '\0' }; +static mio_ooch_t errstr_4[] = {'i', 'n', 't', 'e', 'r', 'n', 'a', 'l', ' ', 'e', 'r', 'r', 'o', 'r', '\0' }; +static mio_ooch_t errstr_5[] = {'i', 'n', 's', 'u', 'f', 'f', 'i', 'c', 'i', 'e', 'n', 't', ' ', 's', 'y', 's', 't', 'e', 'm', ' ', 'm', 'e', 'm', 'o', 'r', 'y', '\0' }; +static mio_ooch_t errstr_6[] = {'i', 'n', 's', 'u', 'f', 'f', 'i', 'c', 'i', 'e', 'n', 't', ' ', 'o', 'b', 'j', 'e', 'c', 't', ' ', 'm', 'e', 'm', 'o', 'r', 'y', '\0' }; +static mio_ooch_t errstr_7[] = {'i', 'n', 'v', 'a', 'l', 'i', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ', 'o', 'r', ' ', 'd', 'a', 't', 'a', '\0' }; +static mio_ooch_t errstr_8[] = {'d', 'a', 't', 'a', ' ', 'n', 'o', 't', ' ', 'f', 'o', 'u', 'n', 'd', '\0' }; +static mio_ooch_t errstr_9[] = {'e', 'x', 'i', 's', 't', 'i', 'n', 'g', '/', 'd', 'u', 'p', 'l', 'i', 'c', 'a', 't', 'e', ' ', 'd', 'a', 't', 'a', '\0' }; +static mio_ooch_t errstr_10[] = {'s', 'y', 's', 't', 'e', 'm', ' ', 'b', 'u', 's', 'y', '\0' }; +static mio_ooch_t errstr_11[] = {'a', 'c', 'c', 'e', 's', 's', ' ', 'd', 'e', 'n', 'i', 'e', 'd', '\0' }; +static mio_ooch_t errstr_12[] = {'o', 'p', 'e', 'r', 'a', 't', 'i', 'o', 'n', ' ', 'n', 'o', 't', ' ', 'p', 'e', 'r', 'm', 'i', 't', 't', 'e', 'd', '\0' }; +static mio_ooch_t errstr_13[] = {'n', 'o', 't', ' ', 'd', 'i', 'r', 'e', 'c', 't', 'o', 'r', 'y', '\0' }; +static mio_ooch_t errstr_14[] = {'i', 'n', 't', 'e', 'r', 'r', 'u', 'p', 't', 'e', 'd', '\0' }; +static mio_ooch_t errstr_15[] = {'p', 'i', 'p', 'e', ' ', 'e', 'r', 'r', 'o', 'r', '\0' }; +static mio_ooch_t errstr_16[] = {'r', 'e', 's', 'o', 'u', 'r', 'c', 'e', ' ', 't', 'e', 'm', 'p', 'o', 'r', 'a', 'r', 'i', 'l', 'y', ' ', 'u', 'n', 'a', 'v', 'a', 'i', 'l', 'a', 'b', 'l', 'e', '\0' }; +static mio_ooch_t errstr_17[] = {'b', 'a', 'd', ' ', 's', 'y', 's', 't', 'e', 'm', ' ', 'h', 'a', 'n', 'd', 'l', 'e', '\0' }; +static mio_ooch_t errstr_18[] = {'t', 'o', 'o', ' ', 'm', 'a', 'n', 'y', ' ', 'o', 'p', 'e', 'n', ' ', 'f', 'i', 'l', 'e', 's', '\0' }; +static mio_ooch_t errstr_19[] = {'t', 'o', 'o', ' ', 'm', 'a', 'n', 'y', ' ', 'o', 'p', 'e', 'n', ' ', 'f', 'i', 'l', 'e', 's', '\0' }; +static mio_ooch_t errstr_20[] = {'I', '/', 'O', ' ', 'e', 'r', 'r', 'o', 'r', '\0' }; +static mio_ooch_t errstr_21[] = {'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g', ' ', 'c', 'o', 'n', 'v', 'e', 'r', 's', 'i', 'o', 'n', ' ', 'e', 'r', 'r', 'o', 'r', '\0' }; +static mio_ooch_t errstr_22[] = {'i', 'n', 's', 'u', 'f', 'f', 'i', 'c', 'i', 'e', 'n', 't', ' ', 'd', 'a', 't', 'a', ' ', 'f', 'o', 'r', ' ', 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g', ' ', 'c', 'o', 'n', 'v', 'e', 'r', 's', 'i', 'o', 'n', '\0' }; +static mio_ooch_t errstr_23[] = {'b', 'u', 'f', 'f', 'e', 'r', ' ', 'f', 'u', 'l', 'l', '\0' }; +static mio_ooch_t errstr_24[] = {'c', 'o', 'n', 'n', 'e', 'c', 't', 'i', 'o', 'n', ' ', 'r', 'e', 'f', 'u', 's', 'e', 'd', '\0' }; +static mio_ooch_t errstr_25[] = {'c', 'o', 'n', 'n', 'e', 'c', 't', 'i', 'o', 'n', ' ', 'r', 'e', 's', 'e', 't', '\0' }; +static mio_ooch_t errstr_26[] = {'n', 'o', ' ', 'c', 'a', 'p', 'a', 'b', 'i', 'l', 'i', 't', 'y', '\0' }; +static mio_ooch_t errstr_27[] = {'t', 'i', 'm', 'e', 'd', ' ', 'o', 'u', 't', '\0' }; +static mio_ooch_t errstr_28[] = {'u', 'n', 'a', 'b', 'l', 'e', ' ', 't', 'o', ' ', 'm', 'a', 'k', 'e', ' ', 'd', 'e', 'v', 'i', 'c', 'e', '\0' }; +static mio_ooch_t errstr_29[] = {'d', 'e', 'v', 'i', 'c', 'e', ' ', 'e', 'r', 'r', 'o', 'r', '\0' }; static mio_ooch_t* errstr[] = { - errstr_0, errstr_1, errstr_2, errstr_3, errstr_4, errstr_5, errstr_6, errstr_7, - errstr_8, errstr_9, errstr_10, errstr_11, errstr_12, errstr_13, errstr_14, errstr_15, - errstr_16, errstr_17, errstr_18, errstr_19, errstr_20, errstr_21, errstr_22, errstr_23, - errstr_24, errstr_25, errstr_26, errstr_27, errstr_28, errstr_29, errstr_30, errstr_31, - errstr_32, errstr_33 + errstr_0, errstr_1, errstr_2, errstr_3, errstr_4, + errstr_5, errstr_6, errstr_7, errstr_8, errstr_9, + errstr_10, errstr_11, errstr_12, errstr_13, errstr_14, + errstr_15, errstr_16, errstr_17, errstr_18, errstr_19, + errstr_20, errstr_21, errstr_22, errstr_23, errstr_24, + errstr_25, errstr_26, errstr_27, errstr_28, errstr_29 }; /* -------------------------------------------------------------------------- diff --git a/mio/lib/generr.awk b/mio/lib/generr.awk new file mode 100644 index 0000000..e557a33 --- /dev/null +++ b/mio/lib/generr.awk @@ -0,0 +1,55 @@ +# +# qseawk --striprecspace=on -f generr.awk mio.h +# + +BEGIN { + FS = "[[:space:]]+"; + capture = 0; + msgcount = 0; +} + +capture == 1 { + if ($0 == "};") + { + capture = 0; + } + else if ($1 ~ /^MIO_E.+,/) + { + msg = ""; + for (i = 3; i < NF; i++) + { + if (i > 3) msg = msg OFS; + msg = msg $i; + } + + printf ("static mio_ooch_t errstr_%d[] = {", msgcount); + len = length(msg); + for (i = 1; i <= len; i++) + { + printf ("'%c', ", substr(msg, i, 1)); + } + printf ("'\\0' };\n"); + msgcount++; + } +} +/^enum mio_errnum_t$/ { + getline x; # consume the next line + capture = 1; +} + + +END { + printf ("static mio_ooch_t* errstr[] =\n"); + printf ("{\n\t"); + for (i = 0; i < msgcount; i++) + { + if (i > 0) + { + if (i % 5 == 0) printf (",\n\t"); + else printf (", "); + } + printf ("errstr_%d", i); + } + if ((i - 1) % 5 != 0) printf ("\n"); + printf ("};\n"); +} diff --git a/mio/lib/main.c b/mio/lib/main.c index 50bb2f7..fa92f76 100644 --- a/mio/lib/main.c +++ b/mio/lib/main.c @@ -445,6 +445,7 @@ static void on_icmp_due (mio_t* mio, const mio_ntime_t* now, mio_tmrjob_t* tmrjo static int schedule_icmp_wait (mio_dev_sck_t* dev) { + mio_t* mio = dev->mio; icmpxtn_t* icmpxtn; mio_tmrjob_t tmrjob; mio_ntime_t fire_after; @@ -454,7 +455,7 @@ static int schedule_icmp_wait (mio_dev_sck_t* dev) memset (&tmrjob, 0, MIO_SIZEOF(tmrjob)); tmrjob.ctx = dev; - mio_sys_gettime (&tmrjob.when); + mio_gettime (mio, &tmrjob.when); MIO_ADD_NTIME (&tmrjob.when, &tmrjob.when, &fire_after); tmrjob.handler = on_icmp_due; tmrjob.idxptr = &icmpxtn->tmout_jobidx; @@ -565,7 +566,7 @@ static int setup_ping4_tester (mio_t* mio) /* ========================================================================= */ -#if 0 +#if 1 static mio_t* g_mio; static void handle_signal (int sig) @@ -593,7 +594,7 @@ int main (int argc, char* argv[]) SSL_library_init (); #endif - mio = mio_open(&mmgr, 0, 512, MIO_NULL); + mio = mio_open(&mmgr, 0, MIO_NULL, 512, MIO_NULL); if (!mio) { printf ("Cannot open mio\n"); @@ -645,10 +646,10 @@ int main (int argc, char* argv[]) ts = (tcp_server_t*)(tcp[0] + 1); ts->tally = 0; - memset (&tcp_conn, 0, MIO_SIZEOF(tcp_conn)); { - in_addr_t ia = inet_addr("192.168.1.119"); + /* openssl s_server -accept 9999 -key localhost.key -cert localhost.crt */ + in_addr_t ia = inet_addr("127.0.0.1"); mio_sckaddr_initforip4 (&tcp_conn.remoteaddr, 9999, (mio_ip4addr_t*)&ia); } @@ -737,6 +738,7 @@ int main (int argc, char* argv[]) if (setup_arp_tester(mio) <= -1) goto oops; if (setup_ping4_tester(mio) <= -1) goto oops; +#if 1 for (i = 0; i < 5; i++) { mio_dev_pro_t* pro; @@ -745,8 +747,8 @@ for (i = 0; i < 5; i++) memset (&pro_make, 0, MIO_SIZEOF(pro_make)); pro_make.flags = MIO_DEV_PRO_READOUT | MIO_DEV_PRO_READERR | MIO_DEV_PRO_WRITEIN /*| MIO_DEV_PRO_FORGET_CHILD*/; //pro_make.cmd = "/bin/ls -laF /usr/bin"; - //pro_make.cmd = "/bin/ls -laF"; - pro_make.cmd = "./a"; + pro_make.cmd = "/bin/ls -laF"; + //pro_make.cmd = "./a"; pro_make.on_read = pro_on_read; pro_make.on_write = pro_on_write; pro_make.on_close = pro_on_close; @@ -754,7 +756,7 @@ for (i = 0; i < 5; i++) pro = mio_dev_pro_make(mio, 0, &pro_make); if (!pro) { - printf ("CANNOT CREATE PROCESS PIPE\n"); + MIO_INFO1 (mio, "CANNOT CREATE PROCESS PIPE - %js\n", mio_geterrmsg(mio)); goto oops; } @@ -764,7 +766,7 @@ for (i = 0; i < 5; i++) //mio_dev_pro_close (pro, MIO_DEV_PRO_OUT); //mio_dev_pro_close (pro, MIO_DEV_PRO_ERR); } - +#endif mio_loop (mio); g_mio = MIO_NULL; diff --git a/mio/lib/mio-prv.h b/mio/lib/mio-prv.h index 764df45..744f019 100644 --- a/mio/lib/mio-prv.h +++ b/mio/lib/mio-prv.h @@ -31,11 +31,8 @@ #include "mio-utl.h" #include - /*TODO: redefine and remove these */ #include -#include -/*TODO: redefine these */ #define MIO_MEMSET(dst,byte,count) memset(dst,byte,count) #define MIO_MEMCPY(dst,src,count) memcpy(dst,src,count) #define MIO_MEMMOVE(dst,src,count) memmove(dst,src,count) @@ -179,6 +176,11 @@ int mio_sys_waitmux ( mio_sys_mux_evtcb_t event_handler ); +void mio_sys_gettime ( + mio_t* mio, + mio_ntime_t* now +); + #ifdef __cplusplus } #endif diff --git a/mio/lib/mio-sck.c b/mio/lib/mio-sck.c index 56dbe9d..3dd5f65 100644 --- a/mio/lib/mio-sck.c +++ b/mio/lib/mio-sck.c @@ -399,14 +399,15 @@ static int schedule_timer_job_at (mio_dev_sck_t* dev, const mio_ntime_t* fire_at static int schedule_timer_job_after (mio_dev_sck_t* dev, const mio_ntime_t* fire_after, mio_tmrjob_handler_t handler) { + mio_t* mio = dev->mio; mio_ntime_t fire_at; - MIO_ASSERT (dev->mio, MIO_IS_POS_NTIME(fire_after)); + MIO_ASSERT (mio, MIO_IS_POS_NTIME(fire_after)); - mio_sys_gettime (&fire_at); + mio_gettime (mio, &fire_at); MIO_ADD_NTIME (&fire_at, &fire_at, fire_after); - return schedule_timer_job_at (dev, &fire_at, handler); + return schedule_timer_job_at(dev, &fire_at, handler); } /* ======================================================================== */ diff --git a/mio/lib/mio-sys.h b/mio/lib/mio-sys.h index 248e4e6..2f78c1e 100644 --- a/mio/lib/mio-sys.h +++ b/mio/lib/mio-sys.h @@ -27,6 +27,8 @@ #ifndef _MIO_SYS_H_ #define _MIO_SYS_H_ +/* this is a private header file used by sys-XXX files */ + #include "mio-prv.h" #if defined(HAVE_SYS_EPOLL_H) @@ -88,12 +90,33 @@ struct mio_sys_log_t }; typedef struct mio_sys_log_t mio_sys_log_t; +/* -------------------------------------------------------------------------- */ + +struct mio_sys_time_t +{ +#if defined(_WIN32) + HANDLE waitable_timer; + DWORD tc_last; + DWORD tc_overflow; +#elif defined(__OS2__) + ULONG tc_last; + moo_ntime_t tc_last_ret; +#elif defined(__DOS__) + clock_t tc_last; + moo_ntime_t tc_last_ret; +#else + /* nothing */ +#endif +}; + +typedef struct mio_sys_time_t mio_sys_time_t; + /* -------------------------------------------------------------------------- */ struct mio_sys_t { mio_sys_log_t log; mio_sys_mux_t mux; - /*mio_sys_time_t time;*/ + mio_sys_time_t time; }; /* -------------------------------------------------------------------------- */ @@ -118,6 +141,13 @@ void mio_sys_finimux ( mio_t* mio ); +int mio_sys_inittime ( + mio_t* mio +); + +void mio_sys_finitime ( + mio_t* mio +); #if defined(__cplusplus) } diff --git a/mio/lib/mio.c b/mio/lib/mio.c index 5c8667f..6ec945e 100644 --- a/mio/lib/mio.c +++ b/mio/lib/mio.c @@ -115,6 +115,7 @@ int mio_init (mio_t* mio, mio_mmgr_t* mmgr, mio_cmgr_t* cmgr, mio_oow_t tmrcapa) mio->tmr.capa = tmrcapa; MIO_CWQ_INIT (&mio->cwq); + mio_sys_gettime (mio, &mio->init_time); return 0; oops: @@ -422,7 +423,7 @@ static MIO_INLINE void handle_event (mio_t* mio, mio_dev_t* dev, int events, int MIO_MEMSET (&tmrjob, 0, MIO_SIZEOF(tmrjob)); tmrjob.ctx = dev; - mio_sys_gettime (&tmrjob.when); + mio_gettime (mio, &tmrjob.when); MIO_ADD_NTIME (&tmrjob.when, &tmrjob.when, &dev->rtmout); tmrjob.handler = on_read_timeout; tmrjob.idxptr = &dev->rtmridx; @@ -758,6 +759,7 @@ static void kill_zombie_job_handler (mio_t* mio, const mio_ntime_t* now, mio_tmr static int schedule_kill_zombie_job (mio_dev_t* dev) { + mio_t* mio = dev->mio; mio_tmrjob_t kill_zombie_job; mio_ntime_t tmout; @@ -765,12 +767,12 @@ static int schedule_kill_zombie_job (mio_dev_t* dev) MIO_MEMSET (&kill_zombie_job, 0, MIO_SIZEOF(kill_zombie_job)); kill_zombie_job.ctx = dev; - mio_sys_gettime (&kill_zombie_job.when); + mio_gettime (mio, &kill_zombie_job.when); MIO_ADD_NTIME (&kill_zombie_job.when, &kill_zombie_job.when, &tmout); kill_zombie_job.handler = kill_zombie_job_handler; /*kill_zombie_job.idxptr = &rdev->tmridx_kill_zombie;*/ - return mio_instmrjob (dev->mio, &kill_zombie_job) == MIO_TMRIDX_INVALID? -1: 0; + return mio_instmrjob (mio, &kill_zombie_job) == MIO_TMRIDX_INVALID? -1: 0; } void mio_killdev (mio_t* mio, mio_dev_t* dev) @@ -1021,7 +1023,7 @@ update_timer: MIO_MEMSET (&tmrjob, 0, MIO_SIZEOF(tmrjob)); tmrjob.ctx = dev; - mio_sys_gettime (&tmrjob.when); + mio_gettime (mio, &tmrjob.when); MIO_ADD_NTIME (&tmrjob.when, &tmrjob.when, tmout); tmrjob.handler = on_read_timeout; tmrjob.idxptr = &dev->rtmridx; @@ -1190,7 +1192,7 @@ enqueue_data: MIO_MEMSET (&tmrjob, 0, MIO_SIZEOF(tmrjob)); tmrjob.ctx = q; - mio_sys_gettime (&tmrjob.when); + mio_gettime (mio, &tmrjob.when); MIO_ADD_NTIME (&tmrjob.when, &tmrjob.when, tmout); tmrjob.handler = on_write_timeout; tmrjob.idxptr = &q->tmridx; @@ -1287,10 +1289,21 @@ int mio_makesyshndasync (mio_t* mio, mio_syshnd_t hnd) /* -------------------------------------------------------------------------- */ +void mio_gettime (mio_t* mio, mio_ntime_t* now) +{ + mio_sys_gettime (mio, now); + /* in mio_init(), mio->init_time has been set to the initialization time. + * the time returned here gets offset by moo->init_time and + * thus becomes relative to it. this way, it is kept small such that it + * can be represented in a small integer with leaving almost zero chance + * of overflow. */ + MIO_SUB_NTIME (now, now, &mio->init_time); /* now = now - init_time */ +} + +/* -------------------------------------------------------------------------- */ void* mio_allocmem (mio_t* mio, mio_oow_t size) { void* ptr; - ptr = MIO_MMGR_ALLOC (mio->mmgr, size); if (!ptr) mio_seterrnum (mio, MIO_ESYSMEM); return ptr; @@ -1299,7 +1312,6 @@ void* mio_allocmem (mio_t* mio, mio_oow_t size) void* mio_callocmem (mio_t* mio, mio_oow_t size) { void* ptr; - ptr = MIO_MMGR_ALLOC (mio->mmgr, size); if (!ptr) mio_seterrnum (mio, MIO_ESYSMEM); else MIO_MEMSET (ptr, 0, size); diff --git a/mio/lib/mio.h b/mio/lib/mio.h index fe63a69..2568f31 100644 --- a/mio/lib/mio.h +++ b/mio/lib/mio.h @@ -73,36 +73,35 @@ enum mio_errnum_t MIO_EINTERN, /**< internal error */ MIO_ESYSMEM, /**< insufficient system memory */ MIO_EOOMEM, /**< insufficient object memory */ - MIO_ETYPE, /**< invalid class/type */ MIO_EINVAL, /**< invalid parameter or data */ MIO_ENOENT, /**< data not found */ MIO_EEXIST, /**< existing/duplicate data */ - MIO_EBUSY, - MIO_EACCES, + MIO_EBUSY, /**< system busy */ + MIO_EACCES, /**< access denied */ MIO_EPERM, /**< operation not permitted */ - MIO_ENOTDIR, - MIO_EINTR, - MIO_EPIPE, - MIO_EAGAIN, - MIO_EBADHND, + MIO_ENOTDIR, /**< not directory */ + MIO_EINTR, /**< interrupted */ + MIO_EPIPE, /**< pipe error */ + MIO_EAGAIN, /**< resource temporarily unavailable */ + MIO_EBADHND, /**< bad system handle */ - MIO_EMFILE, /* too many open files */ - MIO_ENFILE, + MIO_EMFILE, /**< too many open files */ + MIO_ENFILE, /**< too many open files */ MIO_EIOERR, /**< I/O error */ MIO_EECERR, /**< encoding conversion error */ MIO_EECMORE, /**< insufficient data for encoding conversion */ MIO_EBUFFULL, /**< buffer full */ - MIO_ECONRF, /* connection refused */ - MIO_ECONRS, /* connection reset */ - MIO_ENOCAPA, /* no capability */ - MIO_ETMOUT, /* timed out */ + MIO_ECONRF, /**< connection refused */ + MIO_ECONRS, /**< connection reset */ + MIO_ENOCAPA, /**< no capability */ + MIO_ETMOUT, /**< timed out */ - MIO_EDEVMAKE, - MIO_EDEVERR, - MIO_EDEVHUP + MIO_EDEVMAKE, /**< unable to make device */ + MIO_EDEVERR, /**< device error */ + MIO_EDEVHUP /**< device hang-up */ }; typedef enum mio_errnum_t mio_errnum_t; @@ -535,11 +534,12 @@ struct mio_t mio_uint8_t bigbuf[65535]; /* TODO: make this dynamic depending on devices added. device may indicate a buffer size required??? */ + mio_ntime_t init_time; struct { mio_oow_t capa; mio_oow_t size; - mio_tmrjob_t* jobs; + mio_tmrjob_t* jobs; } tmr; mio_cwq_t cwq; @@ -789,6 +789,18 @@ MIO_EXPORT int mio_gettmrjobdeadline ( mio_ntime_t* deadline ); +/* ========================================================================= + * TIME + * ========================================================================= */ + +/** + * the mio_gettime() function returns the elapsed time since mio initialization. + */ +MIO_EXPORT void mio_gettime ( + mio_t* mio, + mio_ntime_t* now +); + /* ========================================================================= * SYSTEM MEMORY MANAGEMENT FUCNTIONS VIA MMGR * ========================================================================= */ @@ -1003,13 +1015,6 @@ MIO_EXPORT const mio_ooch_t* mio_errnum_to_errstr ( mio_errnum_t errnum ); -/** - * The mio_sys_gettime() function gets the current time. - */ -MIO_EXPORT void mio_sys_gettime ( - mio_ntime_t* now -); - #ifdef __cplusplus } #endif diff --git a/mio/lib/sys-log.c b/mio/lib/sys-log.c index 2032bb5..4870aee 100644 --- a/mio/lib/sys-log.c +++ b/mio/lib/sys-log.c @@ -79,6 +79,9 @@ #endif +#include /* for sprintf */ + + enum logfd_flag_t { LOGFD_TTY = (1 << 0), diff --git a/mio/lib/sys-tim.c b/mio/lib/sys-tim.c index 9980030..3b1c017 100644 --- a/mio/lib/sys-tim.c +++ b/mio/lib/sys-tim.c @@ -24,7 +24,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "mio-prv.h" +#include "mio-sys.h" #if defined(_WIN32) # include @@ -47,147 +47,20 @@ # include #endif -#define MIO_EPOCH_YEAR (1970) -#define MIO_EPOCH_MON (1) -#define MIO_EPOCH_DAY (1) -#define MIO_EPOCH_WDAY (4) - -/* windows specific epoch time */ -#define MIO_EPOCH_YEAR_WIN (1601) -#define MIO_EPOCH_MON_WIN (1) -#define MIO_EPOCH_DAY_WIN (1) - -#if defined(_WIN32) - #define EPOCH_DIFF_YEARS (MIO_EPOCH_YEAR-MIO_EPOCH_YEAR_WIN) - #define EPOCH_DIFF_DAYS ((mio_intptr_t)EPOCH_DIFF_YEARS*365+EPOCH_DIFF_YEARS/4-3) - #define EPOCH_DIFF_SECS ((mio_intptr_t)EPOCH_DIFF_DAYS*24*60*60) -#endif - -#if 0 -void mio_sys_gettime (mio_ntime_t* now) +int mio_sys_inittime (mio_t* mio) { -#if defined(_WIN32) - SYSTEMTIME st; - FILETIME ft; - ULARGE_INTEGER li; - - /* - * MSDN: The FILETIME structure is a 64-bit value representing the - * number of 100-nanosecond intervals since January 1, 1601 (UTC). - */ - - GetSystemTime (&st); - SystemTimeToFileTime (&st, &ft); /* this must not fail */ - - li.LowPart = ft.dwLowDateTime; - li.HighPart = ft.dwHighDateTime; - - /* li.QuadPart is in the 100-nanosecond intervals */ - now->sec = (li.QuadPart / (MIO_NSECS_PER_SEC / 100)) - EPOCH_DIFF_SECS; - now->nsec = (li.QuadPart % (MIO_NSECS_PER_SEC / 100)) * 100; - -#elif defined(__OS2__) - - DATETIME dt; - mio_btime_t bt; - - /* Can I use DosQuerySysInfo(QSV_TIME_LOW) and - * DosQuerySysInfo(QSV_TIME_HIGH) for this instead? - * Maybe, resolution too low as it returns values - * in seconds. */ - - DosGetDateTime (&dt); - /* DosGetDateTime() never fails. it always returns NO_ERROR */ - - bt.year = dt.year - MIO_BTIME_YEAR_BASE; - bt.mon = dt.month - 1; - bt.mday = dt.day; - bt.hour = dt.hours; - bt.min = dt.minutes; - bt.sec = dt.seconds; - /*bt.msec = dt.hundredths * 10;*/ - bt.isdst = -1; /* determine dst for me */ - - if (mio_timelocal(&bt, t) <= -1) - { - now->sec = time (MIO_NULL); - now->nsec = 0; - } - else - { - now->nsec = MIO_MSEC_TO_NSEC(dt.hundredths * 10); - } + /*mio_sys_time_t* tim = &mio->sysdep->time;*/ + /* nothing to do */ return 0; - -#elif defined(__DOS__) - - struct dostime_t dt; - struct dosdate_t dd; - mio_btime_t bt; - - _dos_gettime (&dt); - _dos_getdate (&dd); - - bt.year = dd.year - MIO_BTIME_YEAR_BASE; - bt.mon = dd.month - 1; - bt.mday = dd.day; - bt.hour = dt.hour; - bt.min = dt.minute; - bt.sec = dt.second; - /*bt.msec = dt.hsecond * 10; */ - bt.isdst = -1; /* determine dst for me */ - - if (mio_timelocal(&bt, t) <= -1) - { - now->sec = time (MIO_NULL); - now->nsec = 0; - } - else - { - now->nsec = MIO_MSEC_TO_NSEC(dt.hsecond * 10); - } - -#elif defined(macintosh) - unsigned long tv; - - GetDateTime (&tv); - now->sec = tv; - tv->nsec = 0; - -#elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_REALTIME) - struct timespec ts; - - if (clock_gettime(CLOCK_REALTIME, &ts) == -1 && errno == EINVAL) - { - #if defined(HAVE_GETTIMEOFDAY) - struct timeval tv; - gettimeofday (&tv, MIO_NULL); - now->sec = tv.tv_sec; - now->nsec = MIO_USEC_TO_NSEC(tv.tv_usec); - #else - now->sec = time (MIO_NULL); - now->nsec = 0; - #endif - } - - now->sec = ts.tv_sec; - now->nsec = ts.tv_nsec; - -#elif defined(HAVE_GETTIMEOFDAY) - struct timeval tv; - gettimeofday (&tv, MIO_NULL); - now->sec = tv.tv_sec; - now->nsec = MIO_USEC_TO_NSEC(tv.tv_usec); - -#else - now->sec = time(MIO_NULL); - now->nsec = 0; -#endif } -#else +void mio_sys_finitime (mio_t* mio) +{ + /*mio_sys_time_t* tim = &mio->sysdep->tim;*/ + /* nothing to do */ +} -void mio_sys_gettime (mio_ntime_t* now) +void mio_sys_gettime (mio_t* mio, mio_ntime_t* now) { #if defined(_WIN32) @@ -195,7 +68,7 @@ void mio_sys_gettime (mio_ntime_t* now) mio_uint64_t bigsec, bigmsec; bigmsec = GetTickCount64(); #else - xtn_t* xtn = GET_XTN(mio); + mio_sys_time_t* tim = &mio->sysdep->tim; mio_uint64_t bigsec, bigmsec; DWORD msec; @@ -216,7 +89,7 @@ void mio_sys_gettime (mio_ntime_t* now) MIO_INIT_NTIME(now, bigsec, MIO_MSEC_TO_NSEC(bigmsec)); #elif defined(__OS2__) - xtn_t* xtn = GET_XTN(mio); + mio_sys_time_t* tim = &mio->sysdep->tim; ULONG msec, elapsed; mio_ntime_t et; @@ -234,7 +107,7 @@ void mio_sys_gettime (mio_ntime_t* now) *now = xtn->tc_last_ret; #elif defined(__DOS__) && (defined(_INTELC32_) || defined(__WATCOMC__)) - xtn_t* xtn = GET_XTN(mio); + mio_sys_time_t* tim = &mio->sysdep->tim; clock_t c, elapsed; mio_ntime_t et; @@ -278,5 +151,3 @@ void mio_sys_gettime (mio_ntime_t* now) MIO_INIT_NTIME(now, tv.tv_sec, MIO_USEC_TO_NSEC(tv.tv_usec)); #endif } - -#endif diff --git a/mio/lib/sys.c b/mio/lib/sys.c index 68c7067..029c5b7 100644 --- a/mio/lib/sys.c +++ b/mio/lib/sys.c @@ -30,6 +30,7 @@ int mio_sys_init (mio_t* mio) { int log_inited = 0; int mux_inited = 0; + int time_inited = 0; mio->sysdep = (mio_sys_t*)mio_callocmem(mio, MIO_SIZEOF(*mio->sysdep)); if (!mio->sysdep) return -1; @@ -40,9 +41,13 @@ int mio_sys_init (mio_t* mio) if (mio_sys_initmux(mio) <= -1) goto oops; mux_inited = 1; + if (mio_sys_inittime(mio) <= -1) goto oops; + time_inited = 1; + return 0; oops: + if (time_inited) mio_sys_finitime (mio); if (mux_inited) mio_sys_finimux (mio); if (log_inited) mio_sys_finilog (mio); if (mio->sysdep) @@ -55,6 +60,7 @@ oops: void mio_sys_fini (mio_t* mio) { + mio_sys_finitime (mio); mio_sys_finimux (mio); mio_sys_finilog (mio); diff --git a/mio/lib/tmr.c b/mio/lib/tmr.c index 7b93537..f3b148e 100644 --- a/mio/lib/tmr.c +++ b/mio/lib/tmr.c @@ -169,7 +169,7 @@ void mio_firetmrjobs (mio_t* mio, const mio_ntime_t* tm, mio_oow_t* firecnt) /* if the current time is not specified, get it from the system */ if (tm) now = *tm; - else mio_sys_gettime (&now); + else mio_gettime (mio, &now); while (mio->tmr.size > 0) { @@ -198,7 +198,7 @@ int mio_gettmrtmout (mio_t* mio, const mio_ntime_t* tm, mio_ntime_t* tmout) /* if the current time is not specified, get it from the system */ if (tm) now = *tm; - else mio_sys_gettime (&now); + else mio_gettime (mio, &now); MIO_SUB_NTIME (tmout, &mio->tmr.jobs[0].when, &now); if (tmout->sec < 0) MIO_CLEAR_NTIME (tmout);