Indented with src/scripts/indent-emp.

This commit is contained in:
Markus Armbruster 2003-09-02 20:48:48 +00:00
parent 5f263a7753
commit 9b7adfbecc
437 changed files with 52211 additions and 51052 deletions

View file

@ -38,7 +38,7 @@
#ifdef _EMPTH_LWP
int
empth_init (char **ctx, int flags)
empth_init(char **ctx, int flags)
{
lwpInitSystem(7, ctx, flags);
return 0;
@ -46,11 +46,11 @@ empth_init (char **ctx, int flags)
empth_t *
empth_create (int prio, void (*entry)(), int size, int flags, char *name,
char *desc, void *ud)
empth_create(int prio, void (*entry) (), int size, int flags, char *name,
char *desc, void *ud)
{
/* inherit flags */
if(!flags)
if (!flags)
flags = LwpCurrent->flags;
return lwpCreate(prio, entry, size, flags, name, desc, 0, 0, ud);
}
@ -72,23 +72,23 @@ empth_self(void)
}
void
empth_exit (void)
empth_exit(void)
{
time_t now;
time_t now;
/* We want to leave the main thread around forever, until it's time
for it to die for real (in a shutdown) */
/* We want to leave the main thread around forever, until it's time
for it to die for real (in a shutdown) */
if (!strcmp(LwpCurrent->name, "Main")) {
while(1) {
time(&now);
lwpSleepUntil(now + 60);
}
while (1) {
time(&now);
lwpSleepUntil(now + 60);
}
}
lwpExit();
}
void
empth_yield (void)
empth_yield(void)
{
/* a hack! */
lwpReschedule();
@ -132,7 +132,7 @@ empth_sem_signal(empth_sem_t *sm)
}
void
empth_sem_wait (empth_sem_t *sm)
empth_sem_wait(empth_sem_t *sm)
{
lwpWait(sm);
}
@ -144,4 +144,3 @@ empth_alarm(int sig)
panic(sig);
}
#endif

View file

@ -85,13 +85,13 @@ typedef struct {
BOOL bKilled;
/* The entry function for the thread. */
void (*pfnEntry)(void *);
void (*pfnEntry) (void *);
/* The system thread ID. */
unsigned long ulThreadID;
unsigned long ulThreadID;
/* An Event sem that the thread will wait/sleep on. */
HANDLE hThreadEvent;
HANDLE hThreadEvent;
} loc_Thread_t;
@ -104,31 +104,30 @@ typedef struct {
*/
typedef struct {
char szName[17];
char szName[17];
/* An exclusion semaphore for this sem. */
HANDLE hMutex;
HANDLE hMutex;
/* An Event sem that the thread(s) will sleep on. */
HANDLE hEvent;
HANDLE hEvent;
/* The count variable */
int count;
} loc_Sem_t;
static struct
{
static struct {
/* This is the thread exclusion/non-premption mutex. */
/* The running thread has this MUTEX, and all others are */
/* either blocked on it, or waiting for some OS response. */
HANDLE hThreadMutex;
HANDLE hThreadMutex;
/* This is the thread startup event sem. */
/* We use this to lockstep when we are starting up threads. */
HANDLE hThreadStartEvent;
HANDLE hThreadStartEvent;
/* The Thread Local Storage index. We store the pThread pointer */
/* for each thread at this index. */
DWORD dwTLSIndex;
DWORD dwTLSIndex;
/* The current running thread. */
loc_Thread_t *pCurThread;
@ -158,35 +157,31 @@ loc_debug(const char *pszFmt, ...)
unsigned long ulRunTick;
unsigned long ulMs, ulSec, ulMin, ulHr;
loc_Thread_t *pThread =
(loc_Thread_t *) TlsGetValue(loc_GVAR.dwTLSIndex);
char buf[1024];
(loc_Thread_t *)TlsGetValue(loc_GVAR.dwTLSIndex);
char buf[1024];
if ((loc_GVAR.flags & EMPTH_PRINT) != 0) {
/* Ticks are in milliseconds */
ulCurTick = GetTickCount();
/* Ticks are in milliseconds */
ulCurTick = GetTickCount();
ulRunTick = ulCurTick - loc_GVAR.ulTickAtStart;
ulMs = ulRunTick % 1000L;
ulSec = (ulRunTick / 1000L) % 60L;
ulMin = (ulRunTick / (60L * 1000L)) % 60L;
ulHr = (ulRunTick / (60L * 60L * 1000L));
va_start(vaList, pszFmt);
vsprintf(buf, pszFmt, vaList);
va_end(vaList);
ulRunTick = ulCurTick - loc_GVAR.ulTickAtStart;
ulMs = ulRunTick % 1000L;
ulSec = (ulRunTick / 1000L) % 60L;
ulMin = (ulRunTick / (60L * 1000L)) % 60L;
ulHr = (ulRunTick / (60L * 60L * 1000L));
if (pThread) {
printf("%ld:%02ld:%02ld.%03ld %17s: %s\n",
ulHr, ulMin, ulSec, ulMs,
pThread->szName,
buf);
} else {
printf("%ld:%02ld:%02ld.%03ld %17s: %s\n",
ulHr, ulMin, ulSec, ulMs,
"UNKNOWN",
buf);
}
va_start(vaList, pszFmt);
vsprintf(buf, pszFmt, vaList);
va_end(vaList);
if (pThread) {
printf("%ld:%02ld:%02ld.%03ld %17s: %s\n",
ulHr, ulMin, ulSec, ulMs, pThread->szName, buf);
} else {
printf("%ld:%02ld:%02ld.%03ld %17s: %s\n",
ulHr, ulMin, ulSec, ulMs, "UNKNOWN", buf);
}
}
}
@ -199,10 +194,10 @@ static void
loc_FreeThreadInfo(loc_Thread_t *pThread)
{
if (pThread) {
if (pThread->hThreadEvent)
CloseHandle(pThread->hThreadEvent);
memset(pThread, 0, sizeof(*pThread));
free(pThread);
if (pThread->hThreadEvent)
CloseHandle(pThread->hThreadEvent);
memset(pThread, 0, sizeof(*pThread));
free(pThread);
}
}
@ -218,27 +213,27 @@ static void
loc_RunThisThread()
{
loc_Thread_t *pThread =
(loc_Thread_t *) TlsGetValue(loc_GVAR.dwTLSIndex);
(loc_Thread_t *)TlsGetValue(loc_GVAR.dwTLSIndex);
if (pThread->bKilled) {
if (!pThread->bMainThread) {
TlsSetValue(loc_GVAR.dwTLSIndex, NULL);
loc_FreeThreadInfo(pThread);
_endthread();
}
if (!pThread->bMainThread) {
TlsSetValue(loc_GVAR.dwTLSIndex, NULL);
loc_FreeThreadInfo(pThread);
_endthread();
}
}
/* Get the MUTEX semaphore, wait forever. */
WaitForSingleObject(loc_GVAR.hThreadMutex, INFINITE);
if (!loc_GVAR.pCurThread) {
/* Set the globals to this thread. */
*loc_GVAR.ppvUserData = pThread->pvUserData;
/* Set the globals to this thread. */
*loc_GVAR.ppvUserData = pThread->pvUserData;
loc_GVAR.pCurThread = pThread;
loc_GVAR.pCurThread = pThread;
} else {
/* Hmm, a problem, eh? */
logerror("RunThisThread, someone already running.");
/* Hmm, a problem, eh? */
logerror("RunThisThread, someone already running.");
}
}
@ -251,19 +246,19 @@ static void
loc_BlockThisThread()
{
loc_Thread_t *pThread =
(loc_Thread_t *) TlsGetValue(loc_GVAR.dwTLSIndex);
(loc_Thread_t *)TlsGetValue(loc_GVAR.dwTLSIndex);
if (loc_GVAR.pCurThread == pThread) {
/* Reset the globals back to original */
/* Reset the globals back to original */
loc_GVAR.pCurThread = NULL;
*loc_GVAR.ppvUserData = NULL;
loc_GVAR.pCurThread = NULL;
*loc_GVAR.ppvUserData = NULL;
/* Release the MUTEX */
ReleaseMutex(loc_GVAR.hThreadMutex);
/* Release the MUTEX */
ReleaseMutex(loc_GVAR.hThreadMutex);
} else {
/* Hmm, this thread was not the running one. */
logerror("BlockThisThread, not running.");
/* Hmm, this thread was not the running one. */
logerror("BlockThisThread, not running.");
}
}
@ -275,7 +270,7 @@ static void
loc_SleepThisThread(unsigned long ulMillisecs)
{
loc_Thread_t *pThread =
(loc_Thread_t *) TlsGetValue(loc_GVAR.dwTLSIndex);
(loc_Thread_t *)TlsGetValue(loc_GVAR.dwTLSIndex);
/* Make sure the event thread is clean. */
ResetEvent(pThread->hThreadEvent);
@ -297,7 +292,8 @@ empth_threadMain(void *pvData)
loc_Thread_t *pThread = (loc_Thread_t *)pvData;
/* Out of here... */
if (!pvData) return;
if (!pvData)
return;
/* Store pThread on this thread. */
TlsSetValue(loc_GVAR.dwTLSIndex, pvData);
@ -313,7 +309,7 @@ empth_threadMain(void *pvData)
/* Run the thread. */
if (pThread->pfnEntry)
pThread->pfnEntry(pThread->pvUserData);
pThread->pfnEntry(pThread->pvUserData);
/* Kill the thread. */
empth_exit();
@ -328,7 +324,7 @@ empth_threadMain(void *pvData)
* main line.
*/
int
empth_init (char **ctx_ptr, int flags)
empth_init(char **ctx_ptr, int flags)
{
loc_Thread_t *pThread = NULL;
@ -341,28 +337,29 @@ empth_init (char **ctx_ptr, int flags)
/* Initally unowned. */
loc_GVAR.hThreadMutex = CreateMutex(NULL, FALSE, NULL);
if (!loc_GVAR.hThreadMutex) {
logerror("Failed to create mutex");
return 0;
logerror("Failed to create mutex");
return 0;
}
/* Create the thread start event sem. */
/* Automatic state reset. */
loc_GVAR.hThreadStartEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!loc_GVAR.hThreadStartEvent) {
logerror("Failed to create mutex");
return 0;
logerror("Failed to create mutex");
return 0;
}
/* Create the global Thread context. */
pThread = (loc_Thread_t *) malloc(sizeof(*pThread));
pThread = (loc_Thread_t *)malloc(sizeof(*pThread));
if (!pThread) {
logerror("not enough memory to create main thread.");
return 0;
logerror("not enough memory to create main thread.");
return 0;
}
memset(pThread, 0, sizeof(*pThread));
strncpy(pThread->szName, "Main", sizeof(pThread->szName)-1);
strncpy(pThread->szDesc, "The main process", sizeof(pThread->szDesc)-1);
strncpy(pThread->szName, "Main", sizeof(pThread->szName) - 1);
strncpy(pThread->szDesc, "The main process",
sizeof(pThread->szDesc) - 1);
pThread->ulThreadID = GetCurrentThreadId();
pThread->bMainThread = TRUE;
@ -393,22 +390,23 @@ empth_init (char **ctx_ptr, int flags)
* It is also passed to the entry function...
*/
empth_t *
empth_create (int prio, void (*entry)(), int size, int flags,
char *name, char *desc, void *ud)
empth_create(int prio, void (*entry) (), int size, int flags,
char *name, char *desc, void *ud)
{
loc_Thread_t *pThread = NULL;
loc_debug("creating new thread %s:%s", name, desc);
pThread = (loc_Thread_t *) malloc(sizeof(*pThread));
pThread = (loc_Thread_t *)malloc(sizeof(*pThread));
if (!pThread) {
logerror("not enough memory to create thread: %s (%s)", name, desc);
return NULL;
logerror("not enough memory to create thread: %s (%s)", name,
desc);
return NULL;
}
memset(pThread, 0, sizeof(*pThread));
strncpy(pThread->szName, name, sizeof(pThread->szName)-1);
strncpy(pThread->szDesc, desc, sizeof(pThread->szDesc)-1);
strncpy(pThread->szName, name, sizeof(pThread->szName) - 1);
strncpy(pThread->szDesc, desc, sizeof(pThread->szDesc) - 1);
pThread->pvUserData = ud;
pThread->pfnEntry = entry;
pThread->bMainThread = FALSE;
@ -417,22 +415,22 @@ empth_create (int prio, void (*entry)(), int size, int flags,
pThread->hThreadEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (size < loc_MIN_THREAD_STACK)
size = loc_MIN_THREAD_STACK;
size = loc_MIN_THREAD_STACK;
pThread->ulThreadID = _beginthread(empth_threadMain, size,
(void *)pThread);
(void *)pThread);
if (pThread->ulThreadID == -1) {
logerror("can not create thread: %s (%s): %s", name, desc,
strerror(errno));
goto bad;
logerror("can not create thread: %s (%s): %s", name, desc,
strerror(errno));
goto bad;
}
loc_debug("new thread id is %ld", pThread->ulThreadID);
return pThread;
bad:
bad:
if (pThread) {
loc_FreeThreadInfo(pThread);
loc_FreeThreadInfo(pThread);
}
return NULL;
}
@ -445,7 +443,7 @@ empth_t *
empth_self(void)
{
loc_Thread_t *pThread =
(loc_Thread_t *) TlsGetValue(loc_GVAR.dwTLSIndex);
(loc_Thread_t *)TlsGetValue(loc_GVAR.dwTLSIndex);
return pThread;
}
@ -454,45 +452,45 @@ empth_self(void)
* empth_exit
*/
void
empth_exit (void)
empth_exit(void)
{
s_char *getlogfile();
s_char *getlogfile();
loc_Thread_t *pThread =
(loc_Thread_t *) TlsGetValue(loc_GVAR.dwTLSIndex);
(loc_Thread_t *)TlsGetValue(loc_GVAR.dwTLSIndex);
loc_BlockThisThread();
loc_debug("empth_exit");
if (pThread->bMainThread) {
char buf[20];
/* The main line. Wait forever. */
if (getlogfile() == (s_char *)0)
loginit();
printf("log file: %s\n", getlogfile());
while (1) {
printf("\nEmpire Server>");
fgets(buf, sizeof(buf), stdin);
if (!strnicmp(buf, "quit", 4))
break;
}
TlsSetValue(loc_GVAR.dwTLSIndex, NULL);
loc_FreeThreadInfo(pThread);
char buf[20];
/* The main line. Wait forever. */
if (getlogfile() == (s_char *)0)
loginit();
printf("log file: %s\n", getlogfile());
while (1) {
printf("\nEmpire Server>");
fgets(buf, sizeof(buf), stdin);
if (!strnicmp(buf, "quit", 4))
break;
}
TlsSetValue(loc_GVAR.dwTLSIndex, NULL);
loc_FreeThreadInfo(pThread);
} else {
TlsSetValue(loc_GVAR.dwTLSIndex, NULL);
loc_FreeThreadInfo(pThread);
_endthread();
TlsSetValue(loc_GVAR.dwTLSIndex, NULL);
loc_FreeThreadInfo(pThread);
_endthread();
}
}
/************************
* empth_yield
*
* Yield processing to another thread.
*/
void
empth_yield (void)
empth_yield(void)
{
loc_BlockThisThread();
loc_RunThisThread();
@ -506,7 +504,7 @@ empth_yield (void)
void
empth_terminate(empth_t *a)
{
loc_Thread_t *pThread = (loc_Thread_t *) a;
loc_Thread_t *pThread = (loc_Thread_t *)a;
loc_debug("killing thread %s", pThread->szName);
pThread->bKilled = TRUE;
@ -527,7 +525,7 @@ void
empth_select(int fd, int flags)
{
loc_Thread_t *pThread =
(loc_Thread_t *) TlsGetValue(loc_GVAR.dwTLSIndex);
(loc_Thread_t *)TlsGetValue(loc_GVAR.dwTLSIndex);
fd_set readmask;
fd_set writemask;
struct lwpProc *proc;
@ -535,54 +533,54 @@ empth_select(int fd, int flags)
int n;
loc_debug("%s select on %d",
flags == EMPTH_FD_READ ? "read" : "write", fd );
flags == EMPTH_FD_READ ? "read" : "write", fd);
loc_BlockThisThread();
while (1) {
tv.tv_sec = 1000000;
tv.tv_usec = 0;
tv.tv_sec = 1000000;
tv.tv_usec = 0;
FD_ZERO(&readmask);
FD_ZERO(&writemask);
switch(flags) {
case EMPTH_FD_READ:
FD_SET(fd, &readmask);
break;
case EMPTH_FD_WRITE:
FD_SET(fd, &writemask);
break;
default:
logerror("bad flag %d passed to empth_select", flags);
empth_exit();
}
FD_ZERO(&readmask);
FD_ZERO(&writemask);
n = select(fd + 1, &readmask, &writemask, (fd_set *)0, &tv);
if (n < 0) {
if (errno == EINTR) {
/* go handle the signal */
loc_debug("select broken by signal");
goto done;
return;
}
/* strange but we dont get EINTR on select broken by signal */
loc_debug("select failed (%s)", strerror(errno));
goto done;
return;
}
if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
loc_debug("input ready");
break;
}
if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
loc_debug("output ready");
break;
}
switch (flags) {
case EMPTH_FD_READ:
FD_SET(fd, &readmask);
break;
case EMPTH_FD_WRITE:
FD_SET(fd, &writemask);
break;
default:
logerror("bad flag %d passed to empth_select", flags);
empth_exit();
}
n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
if (n < 0) {
if (errno == EINTR) {
/* go handle the signal */
loc_debug("select broken by signal");
goto done;
return;
}
/* strange but we dont get EINTR on select broken by signal */
loc_debug("select failed (%s)", strerror(errno));
goto done;
return;
}
if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
loc_debug("input ready");
break;
}
if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
loc_debug("output ready");
break;
}
}
done:
done:
loc_RunThisThread();
}
@ -593,7 +591,7 @@ emp_sig_t
empth_alarm(int sig)
{
loc_Thread_t *pThread =
(loc_Thread_t *) TlsGetValue(loc_GVAR.dwTLSIndex);
(loc_Thread_t *)TlsGetValue(loc_GVAR.dwTLSIndex);
loc_debug("got alarm signal %d", sig);
@ -626,7 +624,7 @@ void
empth_sleep(long until)
{
loc_Thread_t *pThread =
(loc_Thread_t *) TlsGetValue(loc_GVAR.dwTLSIndex);
(loc_Thread_t *)TlsGetValue(loc_GVAR.dwTLSIndex);
unsigned long ulSec;
ulSec = until - time(0);
@ -653,14 +651,14 @@ empth_sem_create(char *name, int cnt)
{
loc_Sem_t *pSem;
pSem = (loc_Sem_t *) malloc(sizeof(*pSem));
pSem = (loc_Sem_t *)malloc(sizeof(*pSem));
if (!pSem) {
logerror("out of memory at %s:%d", __FILE__, __LINE__);
return NULL;
logerror("out of memory at %s:%d", __FILE__, __LINE__);
return NULL;
}
memset(pSem, 0, sizeof(pSem));
strncpy(pSem->szName, name, sizeof(pSem->szName)-1);
strncpy(pSem->szName, name, sizeof(pSem->szName) - 1);
pSem->hMutex = CreateMutex(NULL, FALSE, NULL);
pSem->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
@ -677,7 +675,7 @@ empth_sem_create(char *name, int cnt)
void
empth_sem_signal(empth_sem_t *sm)
{
loc_Sem_t *pSem = (loc_Sem_t *) sm;
loc_Sem_t *pSem = (loc_Sem_t *)sm;
loc_debug("signal on semaphore %s:%d", pSem->szName, pSem->count);
@ -685,7 +683,7 @@ empth_sem_signal(empth_sem_t *sm)
WaitForSingleObject(pSem->hMutex, INFINITE);
if (pSem->count++ < 0) {
SetEvent(pSem->hEvent);
SetEvent(pSem->hEvent);
}
ReleaseMutex(pSem->hMutex);
@ -698,11 +696,11 @@ empth_sem_signal(empth_sem_t *sm)
* to be signaled.
*/
void
empth_sem_wait (empth_sem_t *sm)
empth_sem_wait(empth_sem_t *sm)
{
loc_Thread_t *pThread =
(loc_Thread_t *) TlsGetValue(loc_GVAR.dwTLSIndex);
loc_Sem_t *pSem = (loc_Sem_t *) sm;
(loc_Thread_t *)TlsGetValue(loc_GVAR.dwTLSIndex);
loc_Sem_t *pSem = (loc_Sem_t *)sm;
loc_debug("wait on semaphore %s:%d", pSem->szName, pSem->count);
@ -712,17 +710,16 @@ empth_sem_wait (empth_sem_t *sm)
/* Wait for the Semaphore */
WaitForSingleObject(pSem->hMutex, INFINITE);
if (--pSem->count < 0) {
loc_debug("blocking");
ReleaseMutex(pSem->hMutex);
loc_debug("blocking");
ReleaseMutex(pSem->hMutex);
WaitForSingleObject(pSem->hEvent, INFINITE);
WaitForSingleObject(pSem->hEvent, INFINITE);
loc_debug("waking up");
}
else
ReleaseMutex(pSem->hMutex);
loc_debug("waking up");
} else
ReleaseMutex(pSem->hMutex);
loc_RunThisThread();
}
#endif /* _WIN32 */
#endif /* _WIN32 */

View file

@ -47,13 +47,13 @@
#include "prototypes.h"
#include <stdarg.h>
#ifdef _EMPTH_POSIX
static pthread_key_t ctx_key;
static int empth_flags;
static char **udata; /* pointer to out global context */
static char **udata; /* pointer to out global context */
static pthread_mutex_t mtx_ctxsw; /* thread in critical section */
static pthread_mutex_t mtx_ctxsw; /* thread in critical section */
#if 0
@ -68,25 +68,25 @@ empth_start(void *ctx)
extern emp_sig_t panic();
extern emp_sig_t shutdwn();
/* actually it should inherit all this from main but... */
/* actually it should inherit all this from main but... */
#ifdef SA_SIGINFO
act.sa_flags = SA_SIGINFO;
#endif
sigemptyset (&act.sa_mask);
sigemptyset(&act.sa_mask);
act.sa_handler = shutdwn;
/* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
a Linux box running POSIX threads -- STM */
/* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
a Linux box running POSIX threads -- STM */
#if !(defined(__linux__) && defined(_EMPTH_POSIX))
sigaction (SIGUSR1, &act, NULL);
sigaction(SIGUSR1, &act, NULL);
#endif
sigaction (SIGTERM, &act, NULL);
sigaction (SIGINT, &act, NULL);
sigaction(SIGTERM, &act, NULL);
sigaction(SIGINT, &act, NULL);
act.sa_handler = panic;
sigaction (SIGBUS, &act, NULL);
sigaction (SIGSEGV, &act, NULL);
sigaction (SIGILL, &act, NULL);
sigaction (SIGFPE, &act, NULL);
sigaction(SIGBUS, &act, NULL);
sigaction(SIGSEGV, &act, NULL);
sigaction(SIGILL, &act, NULL);
sigaction(SIGFPE, &act, NULL);
act.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &act, NULL);
@ -100,62 +100,61 @@ empth_start(void *ctx)
((empth_t *)ctx)->ep(((empth_t *)ctx)->ud);
empth_exit();
return NULL;
}
}
static void
empth_status(char *format, ...)
{
va_list ap;
static struct timeval startTime;
struct timeval tv;
char buf[1024];
int sec, msec;
empth_t *a;
va_start(ap, format);
if (empth_flags & EMPTH_PRINT) {
if (startTime.tv_sec == 0)
gettimeofday(&startTime, 0);
gettimeofday(&tv, 0);
sec = tv.tv_sec - startTime.tv_sec;
msec = (tv.tv_usec - startTime.tv_usec) / 1000;
if (msec < 0) {
sec++;
msec += 1000;
}
vsprintf(buf, format, ap);
a = empth_self();
printf("%d:%02d.%03d %17s: %s\n", sec/60, sec%60, msec/10,
a->name,
buf);
va_list ap;
static struct timeval startTime;
struct timeval tv;
char buf[1024];
int sec, msec;
empth_t *a;
va_start(ap, format);
if (empth_flags & EMPTH_PRINT) {
if (startTime.tv_sec == 0)
gettimeofday(&startTime, 0);
gettimeofday(&tv, 0);
sec = tv.tv_sec - startTime.tv_sec;
msec = (tv.tv_usec - startTime.tv_usec) / 1000;
if (msec < 0) {
sec++;
msec += 1000;
}
va_end(ap);
vsprintf(buf, format, ap);
a = empth_self();
printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
a->name, buf);
}
va_end(ap);
}
int
empth_init (char **ctx_ptr, int flags)
empth_init(char **ctx_ptr, int flags)
{
empth_t *ctx;
struct sigaction act;
pthread_key_create(&ctx_key, 0);
pthread_key_create(&ctx_key, 0);
#ifdef _DECTHREADS_
pthread_mutex_init(&mtx_ctxsw, pthread_mutexattr_default);
#else
pthread_mutex_init(&mtx_ctxsw, 0);
#endif
act.sa_flags = 0;
sigemptyset (&act.sa_mask);
sigemptyset(&act.sa_mask);
act.sa_handler = empth_alarm;
sigaction(SIGALRM, &act, NULL);
udata = ctx_ptr;
ctx = (empth_t *)malloc(sizeof(empth_t));
if(!ctx) {
if (!ctx) {
logerror("pthread init failed: not enough memory");
exit(1);
}
@ -164,15 +163,15 @@ empth_init (char **ctx_ptr, int flags)
ctx->ep = 0;
ctx->ud = 0;
ctx->id = pthread_self();
ctx->state = 0;
ctx->state = 0;
pthread_setspecific(ctx_key, ctx);
pthread_mutex_lock(&mtx_ctxsw);
pthread_mutex_lock(&mtx_ctxsw);
empth_flags = flags;
logerror("pthreads initialized");
return 0;
}
/*
* prio can be used for setting scheeduling policy but...
* it seems to be optional in POSIX threads and Solaris
@ -180,25 +179,26 @@ empth_init (char **ctx_ptr, int flags)
* More then that priority is not needed even in lwp threads.
*/
empth_t *
empth_create (int prio, void (*entry)(), int size, int flags,
char *name, char *desc, void *ud)
empth_create(int prio, void (*entry) (), int size, int flags,
char *name, char *desc, void *ud)
{
pthread_t t;
pthread_attr_t attr;
empth_t *ctx;
int eno;
empth_status("creating new thread %s:%s", name, desc);
ctx = (empth_t *)malloc(sizeof(empth_t));
if(!ctx) {
logerror("not enough memoty to create thread: %s (%s)", name, desc);
if (!ctx) {
logerror("not enough memoty to create thread: %s (%s)", name,
desc);
return NULL;
}
ctx->name = strdup(name);
ctx->desc = strdup(desc);
ctx->ud = ud;
ctx->state = 0;
ctx->state = 0;
ctx->ep = entry;
#ifdef _DECTHREADS_
@ -206,27 +206,26 @@ empth_create (int prio, void (*entry)(), int size, int flags,
#else
eno = pthread_attr_init(&attr);
#endif
if(eno) {
if (eno) {
logerror("can not create thread attribute %s (%s): %s", name, desc,
strerror(eno));
goto bad;
}
#if defined(__linux__)
/* Linux doesn't let you adjust the stack */
/* Linux doesn't let you adjust the stack */
#elif defined(_DECTHREADS_)
/* DEC does not have PTHREAD_STACK_MIN constant */
/* Do not go below default size */
if(size > pthread_attr_getstacksize(attr))
pthread_attr_setstacksize(&attr, size);
if (size > pthread_attr_getstacksize(attr))
pthread_attr_setstacksize(&attr, size);
#else
if(size < PTHREAD_STACK_MIN)
size = PTHREAD_STACK_MIN + 1;
if (size < PTHREAD_STACK_MIN)
size = PTHREAD_STACK_MIN + 1;
pthread_attr_setstacksize(&attr, size);
#endif
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
#ifdef _DECTHREADS_
eno = pthread_create(&t, attr, empth_start, (void *)ctx) ? errno : 0;
@ -234,14 +233,14 @@ empth_create (int prio, void (*entry)(), int size, int flags,
eno = pthread_create(&t, &attr, empth_start, (void *)ctx);
#endif
if (eno) {
logerror("can not create thread: %s (%s): %s", name, desc,
strerror(eno));
goto bad;
logerror("can not create thread: %s (%s): %s", name, desc,
strerror(eno));
goto bad;
}
empth_status("new thread id is %d", t);
return ctx;
pthread_attr_destroy(&attr);
bad:
bad:
pthread_attr_destroy(&attr);
free(ctx);
return NULL;
@ -255,7 +254,7 @@ empth_setctx(void *ct)
empth_t *ctx_ptr;
#ifdef _DECTHREADS_
pthread_getspecific(ctx_key, (pthread_addr_t *)&ctx_ptr);
pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
#else
ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
#endif
@ -266,13 +265,13 @@ empth_setctx(void *ct)
}
#endif
static void
static void
empth_restorectx(void)
{
empth_t *ctx_ptr;
#ifdef _DECTHREADS_
pthread_getspecific(ctx_key, (pthread_addr_t *)&ctx_ptr);
pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
#else
ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
#endif
@ -288,47 +287,47 @@ empth_t *
empth_self(void)
{
#ifdef _DECTHREADS_
empth_t *ctx_ptr;
pthread_getspecific(ctx_key, (pthread_addr_t *)&ctx_ptr);
return ctx_ptr;
empth_t *ctx_ptr;
pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
return ctx_ptr;
#else
return (empth_t *)pthread_getspecific(ctx_key);
return (empth_t *)pthread_getspecific(ctx_key);
#endif
}
void
empth_exit (void)
empth_exit(void)
{
empth_t *ctx_ptr;
pthread_mutex_unlock(&mtx_ctxsw);
empth_status("empth_exit");
#ifdef _DECTHREADS_
pthread_getspecific(ctx_key, (pthread_addr_t *)&ctx_ptr);
pthread_getspecific(ctx_key, (pthread_addr_t *) & ctx_ptr);
#else
ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
#endif
/* We want to leave the main thread around forever, until it's time
for it to die for real (in a shutdown) */
/* We want to leave the main thread around forever, until it's time
for it to die for real (in a shutdown) */
if (!strcmp(ctx_ptr->name, "Main")) {
while(1) {
while (1) {
#ifdef _DECTHREADS_
pthread_yield();
pthread_yield();
#endif
sleep(60);
}
sleep(60);
}
}
free(ctx_ptr);
pthread_exit(0);
}
void
empth_yield (void)
empth_yield(void)
{
pthread_mutex_unlock(&mtx_ctxsw);
sleep(10); /* take a nap pthread_yield(); */
sleep(10); /* take a nap pthread_yield(); */
pthread_mutex_lock(&mtx_ctxsw);
empth_restorectx();
}
@ -337,16 +336,16 @@ void
empth_terminate(empth_t *a)
{
/* logerror("calling non supported function empth_terminate: %s:%d",
__FILE__, __LINE__); */
__FILE__, __LINE__); */
empth_status("killing thread %s", a->name);
a->state = EMPTH_KILLED;
#ifndef _DECTHREADS_
/* DEC and OSX do not have pthread_kill. Not sure that cancel is correct. */
#if (!defined __ppc__)
pthread_kill(a->id, SIGALRM);
#endif
#if (!defined __ppc__)
pthread_kill(a->id, SIGALRM);
#endif
return;
#endif
return;
}
void
@ -357,18 +356,18 @@ empth_select(int fd, int flags)
fd_set writemask;
struct timeval tv;
int n;
pthread_mutex_unlock(&mtx_ctxsw);
empth_status("%s select on %d",
flags == EMPTH_FD_READ ? "read" : "write", fd );
flags == EMPTH_FD_READ ? "read" : "write", fd);
while (1) {
tv.tv_sec = 1000000;
tv.tv_usec = 0;
FD_ZERO(&readmask);
FD_ZERO(&writemask);
switch(flags) {
switch (flags) {
case EMPTH_FD_READ:
FD_SET(fd, &readmask);
break;
@ -380,8 +379,8 @@ empth_select(int fd, int flags)
empth_exit();
}
n = select(fd + 1, &readmask, &writemask, (fd_set *)0, &tv);
n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
if (n < 0) {
if (errno == EINTR) {
/* go handle the signal */
@ -391,10 +390,10 @@ empth_select(int fd, int flags)
}
/* strange but we dont get EINTR on select broken by signal */
empth_status("select failed (%s)", strerror(errno));
goto done;
goto done;
return;
}
if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
empth_status("input ready");
break;
@ -404,11 +403,11 @@ empth_select(int fd, int flags)
break;
}
}
done:
done:
pthread_mutex_lock(&mtx_ctxsw);
empth_restorectx();
}
@ -420,7 +419,7 @@ empth_alarm(int sig)
#ifdef SA_RESTART
act.sa_flags &= ~SA_RESTART;
#endif
sigemptyset (&act.sa_mask);
sigemptyset(&act.sa_mask);
act.sa_handler = empth_alarm;
sigaction(SIGALRM, &act, NULL);
}
@ -430,9 +429,9 @@ empth_wakeup(empth_t *a)
{
empth_status("waking up thread %s", a->name);
#ifndef _DECTHREADS_
#if (!defined __ppc__)
pthread_kill(a->id, SIGALRM);
#endif
#if (!defined __ppc__)
pthread_kill(a->id, SIGALRM);
#endif
#endif
empth_status("waiting for it to run");
/* empth_yield(); */
@ -448,7 +447,7 @@ empth_sleep(long until)
tv.tv_sec = until - time(NULL);
tv.tv_usec = 0;
do {
select (0, NULL, NULL, NULL, &tv);
select(0, NULL, NULL, NULL, &tv);
} while ((tv.tv_sec = until - time(NULL)) > 0);
empth_status("sleep done. Waiting for lock");
pthread_mutex_lock(&mtx_ctxsw);
@ -462,11 +461,11 @@ empth_sem_create(char *name, int cnt)
empth_sem_t *sm;
sm = (empth_sem_t *)malloc(sizeof(empth_sem_t));
if(!sm) {
if (!sm) {
logerror("out of memory at %s:%d", __FILE__, __LINE__);
return NULL;
}
strncpy(sm->name, name, sizeof(sm->name)-1);
strncpy(sm->name, name, sizeof(sm->name) - 1);
sm->count = cnt;
#ifdef _DECTHREADS_
pthread_mutex_init(&sm->mtx_update, pthread_mutexattr_default);
@ -485,22 +484,21 @@ empth_sem_signal(empth_sem_t *sm)
{
empth_status("signal on semaphore %s:%d", sm->name, sm->count);
pthread_mutex_lock(&sm->mtx_update);
if(sm->count++ < 0) {
if (sm->count++ < 0) {
pthread_mutex_unlock(&sm->mtx_update);
pthread_mutex_lock(&sm->mtx_sem);
pthread_cond_signal(&sm->cnd_sem);
pthread_mutex_unlock(&sm->mtx_sem);
}
else
} else
pthread_mutex_unlock(&sm->mtx_update);
}
void
empth_sem_wait (empth_sem_t *sm)
empth_sem_wait(empth_sem_t *sm)
{
empth_status("wait on semaphore %s:%d", sm->name, sm->count);
pthread_mutex_lock(&sm->mtx_update);
if(--sm->count < 0) {
if (--sm->count < 0) {
pthread_mutex_unlock(&sm->mtx_update);
empth_status("blocking");
pthread_mutex_unlock(&mtx_ctxsw);
@ -510,8 +508,7 @@ empth_sem_wait (empth_sem_t *sm)
pthread_mutex_unlock(&sm->mtx_sem);
pthread_mutex_lock(&mtx_ctxsw);
empth_restorectx();
}
else
} else
pthread_mutex_unlock(&sm->mtx_update);
}