(empth_init_signals): Don't catch SIGINT and SIGTERM.
(empth_wait_for_shutdown): New. (main): Use it to wait for shutdown signal, then shut down. Closes #770492. (empth_exit): Remove the weird special case for main thread. Implement empth_wait_for_shutdown() for EMPTH_LWP: [EMPTH_LWP] (lwpInitSigWait, lwpSigWait, lwpSigWakeup): New. Declaration of lwpSigWait was accidentally committed in the previous revision of lwp.h. [EMPTH_LWP] (lwpInitSystem): New parameter waitset, pass it on to lwpInitSigWait(). [EMPTH_LWP] (lwpReschedule): Call lwpSigWakeup(). [EMPTH_LWP] (empth_init): Declare signals needed by empth_wait_for_shutdown(). (empth_wait_for_shutdown): Implement on top of lwpSigWait(). Implement empth_wait_for_shutdown() for EMPTH_POSIX: [EMPTH_POSIX] (empth_init): Block signals, so that empth_wait_for_shutdown() can use sigwait() safely. (empth_wait_for_shutdown): Implement on top of sigwait(). Implement empth_wait_for_shutdown() for EMPTH_W32: (empth_wait_for_shutdown): Implement on top of loc_BlockMainThread().
This commit is contained in:
parent
f51769659c
commit
7183516d91
10 changed files with 251 additions and 71 deletions
|
@ -33,12 +33,10 @@
|
|||
|
||||
#include <config.h>
|
||||
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include "empthread.h"
|
||||
|
||||
/* The thread `created' by lwpInitSystem() */
|
||||
static empth_t *empth_main;
|
||||
|
||||
/* Flags that were passed to empth_init() */
|
||||
static int empth_flags;
|
||||
|
||||
|
@ -46,9 +44,14 @@ static int empth_flags;
|
|||
int
|
||||
empth_init(void **ctx, int flags)
|
||||
{
|
||||
sigset_t set;
|
||||
|
||||
empth_flags = flags;
|
||||
empth_init_signals();
|
||||
empth_main = lwpInitSystem(PP_MAIN, (char **)ctx, flags);
|
||||
sigemptyset(&set);
|
||||
sigaddset(&set, SIGINT);
|
||||
sigaddset(&set, SIGTERM);
|
||||
lwpInitSystem(PP_MAIN, (char **)ctx, flags, &set);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -71,16 +74,6 @@ empth_self(void)
|
|||
void
|
||||
empth_exit(void)
|
||||
{
|
||||
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) */
|
||||
if (LwpCurrent == empth_main) {
|
||||
while (1) {
|
||||
time(&now);
|
||||
lwpSleepUntil(now + 60);
|
||||
}
|
||||
}
|
||||
lwpExit();
|
||||
}
|
||||
|
||||
|
@ -114,6 +107,26 @@ empth_sleep(time_t until)
|
|||
lwpSleepUntil(until);
|
||||
}
|
||||
|
||||
int
|
||||
empth_wait_for_shutdown(void)
|
||||
{
|
||||
sigset_t set;
|
||||
int sig, err;
|
||||
time_t now;
|
||||
|
||||
sigemptyset(&set);
|
||||
sigaddset(&set, SIGINT);
|
||||
sigaddset(&set, SIGTERM);
|
||||
for (;;) {
|
||||
err = lwpSigWait(&set, &sig);
|
||||
if (CANT_HAPPEN(err)) {
|
||||
time(&now);
|
||||
lwpSleepUntil(now + 60);
|
||||
continue;
|
||||
}
|
||||
return sig;
|
||||
}
|
||||
}
|
||||
|
||||
empth_sem_t *
|
||||
empth_sem_create(char *name, int cnt)
|
||||
|
|
|
@ -276,31 +276,6 @@ loc_Exit_Handler(DWORD fdwCtrlType)
|
|||
}
|
||||
}
|
||||
|
||||
/************************
|
||||
* empth_request_shutdown
|
||||
*
|
||||
* This wakes up the main thread so shutdown can proceed.
|
||||
* This is done by signalling hShutdownEvent.
|
||||
*/
|
||||
void
|
||||
empth_request_shutdown(void)
|
||||
{
|
||||
SetEvent(hShutdownEvent);
|
||||
}
|
||||
|
||||
/************************
|
||||
* loc_BlockMainThread
|
||||
*
|
||||
* This blocks up the main thread. loc_WakeupMainThread() is used
|
||||
* wakeup the main so shutdown can proceed.
|
||||
*/
|
||||
static void
|
||||
loc_BlockMainThread(void)
|
||||
{
|
||||
/* Get the MUTEX semaphore, wait the number of MS */
|
||||
WaitForSingleObject(hShutdownEvent, INFINITE);
|
||||
}
|
||||
|
||||
/************************
|
||||
* empth_threadMain
|
||||
*
|
||||
|
@ -490,19 +465,12 @@ empth_exit(void)
|
|||
{
|
||||
empth_t *pThread = TlsGetValue(dwTLSIndex);
|
||||
|
||||
loc_debug("empth_exit");
|
||||
loc_BlockThisThread();
|
||||
|
||||
loc_debug("empth_exit");
|
||||
|
||||
if (pThread->bMainThread) {
|
||||
loc_BlockMainThread();
|
||||
loc_RunThisThread();
|
||||
shutdwn(0);
|
||||
} else {
|
||||
TlsSetValue(dwTLSIndex, NULL);
|
||||
loc_FreeThreadInfo(pThread);
|
||||
_endthread();
|
||||
}
|
||||
TlsSetValue(dwTLSIndex, NULL);
|
||||
loc_FreeThreadInfo(pThread);
|
||||
_endthread();
|
||||
}
|
||||
|
||||
/************************
|
||||
|
@ -606,6 +574,27 @@ empth_sleep(time_t until)
|
|||
loc_RunThisThread();
|
||||
}
|
||||
|
||||
/************************
|
||||
* empth_request_shutdown
|
||||
*
|
||||
* This wakes up empth_wait_for_shutdown() so shutdown can proceed.
|
||||
* This is done by signalling hShutdownEvent.
|
||||
*/
|
||||
void
|
||||
empth_request_shutdown(void)
|
||||
{
|
||||
SetEvent(hShutdownEvent);
|
||||
}
|
||||
|
||||
int
|
||||
empth_wait_for_shutdown(void)
|
||||
{
|
||||
/* Get the MUTEX semaphore, wait the number of MS */
|
||||
WaitForSingleObject(hShutdownEvent, INFINITE);
|
||||
|
||||
loc_RunThisThread();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/************************
|
||||
* empth_sem_create
|
||||
|
|
|
@ -46,9 +46,6 @@ empth_init_signals(void)
|
|||
|
||||
act.sa_flags = 0;
|
||||
sigemptyset(&act.sa_mask);
|
||||
act.sa_handler = shutdwn;
|
||||
sigaction(SIGTERM, &act, NULL);
|
||||
sigaction(SIGINT, &act, NULL);
|
||||
act.sa_handler = panic;
|
||||
sigaction(SIGBUS, &act, NULL);
|
||||
sigaction(SIGSEGV, &act, NULL);
|
||||
|
|
|
@ -144,12 +144,17 @@ int
|
|||
empth_init(void **ctx_ptr, int flags)
|
||||
{
|
||||
empth_t *ctx;
|
||||
sigset_t set;
|
||||
struct sigaction act;
|
||||
|
||||
empth_flags = flags;
|
||||
udata = ctx_ptr;
|
||||
|
||||
empth_init_signals();
|
||||
sigemptyset(&set);
|
||||
sigaddset(&set, SIGINT);
|
||||
sigaddset(&set, SIGTERM);
|
||||
pthread_sigmask(SIG_BLOCK, &set, NULL);
|
||||
act.sa_flags = 0;
|
||||
sigemptyset(&act.sa_mask);
|
||||
act.sa_handler = empth_alarm;
|
||||
|
@ -256,20 +261,9 @@ empth_self(void)
|
|||
void
|
||||
empth_exit(void)
|
||||
{
|
||||
empth_t *ctx_ptr;
|
||||
|
||||
pthread_mutex_unlock(&mtx_ctxsw);
|
||||
empth_status("empth_exit");
|
||||
ctx_ptr = pthread_getspecific(ctx_key);
|
||||
/* 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) {
|
||||
sleep(60);
|
||||
}
|
||||
}
|
||||
|
||||
free(ctx_ptr);
|
||||
pthread_mutex_unlock(&mtx_ctxsw);
|
||||
free(pthread_getspecific(ctx_key));
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
|
@ -351,7 +345,6 @@ empth_select(int fd, int flags)
|
|||
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
empth_alarm(int sig)
|
||||
{
|
||||
|
@ -386,6 +379,26 @@ empth_sleep(time_t until)
|
|||
empth_restorectx();
|
||||
}
|
||||
|
||||
int
|
||||
empth_wait_for_shutdown(void)
|
||||
{
|
||||
sigset_t set;
|
||||
int sig, err;
|
||||
|
||||
sigemptyset(&set);
|
||||
sigaddset(&set, SIGINT);
|
||||
sigaddset(&set, SIGTERM);
|
||||
for (;;) {
|
||||
empth_status("waiting for signals");
|
||||
err = sigwait(&set, &sig);
|
||||
if (CANT_HAPPEN(err)) {
|
||||
sleep(60);
|
||||
continue;
|
||||
}
|
||||
empth_status("got awaited signal %d", sig);
|
||||
return sig;
|
||||
}
|
||||
}
|
||||
|
||||
empth_sem_t *
|
||||
empth_sem_create(char *name, int cnt)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue