fixed bugs in mutex protection in sio.

added qse_open_stdsios_with_flags
This commit is contained in:
hyung-hwan 2018-09-04 15:07:42 +00:00
parent cbe34cc526
commit f93bf8d053
8 changed files with 60 additions and 42 deletions

View File

@ -401,6 +401,10 @@ QSE_EXPORT int qse_open_stdsios (
void
);
QSE_EXPORT int qse_open_stdsios_with_flags (
int flags
);
QSE_EXPORT void qse_close_stdsios (
void
);

View File

@ -115,14 +115,14 @@ int fmtout (const char_t* fmt, fmtout_t* data, va_list ap)
struct
{
qse_mchar_t sbuf[32];
qse_mchar_t sbuf[32];
qse_mchar_t* ptr;
qse_size_t capa;
} fltfmt;
struct
{
char_t sbuf[96];
char_t sbuf[96];
char_t* ptr;
qse_size_t capa;
} fltout;
@ -513,7 +513,7 @@ reswitch:
}
width -= n;
if (!(flagc & FLAGC_LEFTADJ) && width > 0)
{
while (width--) PUT_CHAR(padc);

View File

@ -456,8 +456,6 @@ static qse_ssize_t putwc_no_mutex (qse_sio_t* sio, qse_wchar_t c)
{
qse_ssize_t n;
LOCK (sio);
sio->errnum = QSE_SIO_ENOERR;
#if defined(__OS2__)
if (c == QSE_WT('\n') && (sio->status & STATUS_LINE_BREAK))
@ -470,7 +468,6 @@ static qse_ssize_t putwc_no_mutex (qse_sio_t* sio, qse_wchar_t c)
if (n <= -1 && sio->errnum == QSE_SIO_ENOERR)
sio->errnum = tio_errnum_to_sio_errnum (&sio->tio.io);
UNLOCK (sio);
return n;
}
@ -875,15 +872,20 @@ static qse_sio_t* sio_stdout = QSE_NULL;
static qse_sio_t* sio_stderr = QSE_NULL;
/* TODO: add sio_stdin, qse_getmbs, etc */
int qse_open_stdsios (void)
int qse_open_stdsios ()
{
return qse_open_stdsios_with_flags (QSE_SIO_LINEBREAK | QSE_SIO_REENTRANT);
}
int qse_open_stdsios_with_flags (int flags)
{
if (sio_stdout == QSE_NULL)
{
sio_stdout = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_LINEBREAK | QSE_SIO_REENTRANT);
sio_stdout = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, flags);
}
if (sio_stderr == QSE_NULL)
{
sio_stderr = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDERR, QSE_SIO_LINEBREAK | QSE_SIO_REENTRANT);
sio_stderr = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDERR, flags);
}
if (sio_stdout == QSE_NULL || sio_stderr == QSE_NULL)
@ -1031,9 +1033,11 @@ qse_ssize_t qse_errputwcsf (const qse_wchar_t* fmt, ...)
fo.conv = mbs_to_wcs;
va_start (ap, fmt);
LOCK (sio_stderr);
x = qse_wfmtout(fmt, &fo, ap);
UNLOCK (sio_stderr);
va_end (ap);
return (x <= -1)? -1: fo.count;

View File

@ -3,17 +3,16 @@
#include <qse/si/sio.h>
#include <qse/cmn/mem.h>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#include <unistd.h>
#include <signal.h>
#include <string.h>
static int g_stopreq = 0;
static qse_ntime_t sleep_interval = { 1, 0 };
struct thr_xtn_t
{
@ -34,7 +33,7 @@ static int thr_func (qse_thr_t* thr, void* ctx)
qse_printf (QSE_T("%s: [% 16d] [% 16d] [% 16d]\n"), xtn->name, i, i, i);
qse_spl_unlock (xtn->spl);
i++;
/*sleep (1);*/
/*qse_sleep (&sleep_interval);*/
}
return i;
@ -153,7 +152,7 @@ int main ()
}
else
{
sprintf (locale, ".%u", (unsigned int)codepage);
qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
}
@ -164,7 +163,7 @@ int main ()
set_signal (SIGINT, handle_sigint);
qse_open_stdsios ();
qse_open_stdsios_with_flags (QSE_SIO_LINEBREAK); /* to disable mutex protection on stdout & stderr */
test1();
qse_close_stdsios ();

View File

@ -2,18 +2,20 @@
#include <qse/si/Thread.hpp>
#include <qse/si/mtx.h>
#include <qse/si/sio.h>
#include <qse/si/os.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#include <unistd.h>
#include <signal.h>
#include <string.h>
static int g_stopreq = 0;
static qse_ntime_t sleep_interval = { 1, 0 };
static QSE::SpinLock g_prmtx;
@ -32,7 +34,7 @@ public:
qse_printf (QSE_T("m %p -> %d\n"), this, i);
g_prmtx.unlock ();
i++;
sleep (1);
qse_sleep (&sleep_interval);
}
return i;
@ -55,7 +57,7 @@ public:
qse_printf (QSE_T("fc %p -> %d\n"), this, i);
g_prmtx.unlock ();
i++;
sleep (1);
qse_sleep (&sleep_interval);
}
return i;
@ -73,7 +75,7 @@ static int func_ptr (QSE::Thread* thr)
qse_printf (QSE_T("fp %p -> %d\n"), thr, i);
g_prmtx.unlock ();
i++;
sleep (1);
qse_sleep (&sleep_interval);
}
return i;
@ -95,7 +97,7 @@ static int test1 (void)
qse_printf (QSE_T("l %p -> %d\n"), thr, i);
g_prmtx.unlock ();
i++;
sleep (1);
qse_sleep (&sleep_interval);
}
return i;
@ -111,7 +113,7 @@ static int test1 (void)
qse_printf (QSE_T("lc %p -> %d\n"), thr, i);
g_prmtx.unlock ();
i++;
sleep (1);
qse_sleep (&sleep_interval);
}
return i;
@ -172,7 +174,7 @@ static int test1 (void)
qse_printf (QSE_T("tl %p -> %d\n"), thr, i);
g_prmtx.unlock ();
i++;
sleep (1);
qse_sleep (&sleep_interval);
}
return i;
@ -204,7 +206,7 @@ static int test1 (void)
thr5.getState() == QSE::Thread::TERMINATED &&
#endif
thr6.getState() == QSE::Thread::TERMINATED) break;
sleep (1);
qse_sleep (&sleep_interval);
}
if (g_stopreq)
@ -277,7 +279,7 @@ int main ()
}
else
{
sprintf (locale, ".%u", (unsigned int)codepage);
qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
}
@ -288,7 +290,7 @@ int main ()
set_signal (SIGINT, handle_sigint);
qse_open_stdsios ();
qse_open_stdsios_with_flags (QSE_SIO_LINEBREAK);
test1();
qse_close_stdsios ();

View File

@ -1,18 +1,19 @@
#include <qse/si/thr.h>
#include <qse/si/sio.h>
#include <qse/si/os.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#include <unistd.h>
#include <locale.h>
#include <signal.h>
#include <string.h>
static int g_stopreq = 0;
static qse_ntime_t sleep_interval = { 1, 0 };
struct thr_xtn_t
{
@ -20,16 +21,19 @@ struct thr_xtn_t
};
typedef struct thr_xtn_t thr_xtn_t;
static int thr_func (qse_thr_t* thr, void* ctx)
{
int i = 0;
thr_xtn_t* xtn = qse_thr_getxtn(thr);
while (!xtn->stopreq)
{
qse_printf (QSE_T("%d\n"), i);
i++;
sleep (1);
qse_sleep (&sleep_interval);
}
return i;
@ -58,7 +62,7 @@ static int test1 (void)
while (!g_stopreq)
{
if (qse_thr_getstate(thr) == QSE_THR_TERMINATED) break;
sleep (1);
qse_sleep (&sleep_interval);
}
if (g_stopreq)
@ -118,7 +122,7 @@ int main ()
}
else
{
sprintf (locale, ".%u", (unsigned int)codepage);
qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
}

View File

@ -1,7 +1,9 @@
#include <qse/si/Thread.hpp>
#include <qse/si/mtx.h>
#include <qse/si/sio.h>
#include <qse/si/os.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/HeapMmgr.hpp>
#include <locale.h>
@ -9,11 +11,12 @@
# include <windows.h>
#endif
#include <unistd.h>
#include <signal.h>
#include <string.h>
static int g_stopreq = 0;
static qse_ntime_t sleep_interval = { 1, 0 };
QSE::HeapMmgr g_heap_mmgr (QSE::Mmgr::getDFL(), 30000);
@ -30,7 +33,7 @@ public:
{
qse_printf (QSE_T("m %p -> %d\n"), this, i);
i++;
sleep (1);
qse_sleep (&sleep_interval);
}
return i;
@ -51,7 +54,7 @@ public:
{
qse_printf (QSE_T("fc %p -> %d\n"), this, i);
i++;
sleep (1);
qse_sleep (&sleep_interval);
}
return i;
@ -67,7 +70,7 @@ static int func_ptr (QSE::Thread* thr)
{
qse_printf (QSE_T("fp %p -> %d\n"), thr, i);
i++;
sleep (1);
qse_sleep (&sleep_interval);
}
return i;
@ -87,7 +90,7 @@ static int test1 (void)
{
qse_printf (QSE_T("l %p -> %d\n"), thr, i);
i++;
sleep (1);
qse_sleep (&sleep_interval);
}
return i;
@ -101,7 +104,7 @@ static int test1 (void)
{
qse_printf (QSE_T("lc %p -> %d\n"), thr, i);
i++;
sleep (1);
qse_sleep (&sleep_interval);
}
return i;
@ -160,7 +163,7 @@ static int test1 (void)
{
qse_printf (QSE_T("tl %p -> %d\n"), thr, i);
i++;
sleep (1);
qse_sleep (&sleep_interval);
}
return i;
@ -192,7 +195,7 @@ static int test1 (void)
thr5.getState() == QSE::Thread::TERMINATED &&
#endif
thr6.getState() == QSE::Thread::TERMINATED) break;
sleep (1);
qse_sleep (&sleep_interval);
}
if (g_stopreq)
@ -265,7 +268,7 @@ int main ()
}
else
{
sprintf (locale, ".%u", (unsigned int)codepage);
qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
}

View File

@ -2,7 +2,9 @@
#include <qse/si/Condition.hpp>
#include <qse/si/mtx.h>
#include <qse/si/sio.h>
#include <qse/si/os.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/HeapMmgr.hpp>
@ -11,11 +13,11 @@
# include <windows.h>
#endif
#include <unistd.h>
#include <signal.h>
#include <string.h>
static int g_stopreq = 0;
static qse_ntime_t sleep_interval = { 1, 0 };
QSE::HeapMmgr g_heap_mmgr (QSE::Mmgr::getDFL(), 30000);
@ -101,7 +103,7 @@ static int test1 (void)
qse_printf (QSE_T("signalling ....(nterm = %d)\n"), nterm);
rqdata.cnd.signal ();
sleep (1);
qse_sleep (&sleep_interval);
}
@ -161,7 +163,7 @@ int main ()
}
else
{
sprintf (locale, ".%u", (unsigned int)codepage);
qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
}