Skip to content

Commit

Permalink
Allow HPC-GAP to run as a forkable server process.
Browse files Browse the repository at this point in the history
This commit includes the following changes:

1. The Boehm GC is configured to properly handle fork().
2. The new --single-thread command line option now starts HPC-GAP up in
   single-threaded mode without any other threads running. This option
   implies -S (as the thread UI requires threads).
3. Normally, signals are handled by a separate thread; this is now being
   done in the function `InstallHPCGAPSignalHandling`. If started with
   --single-thread, this function needs to be called explicitly in order
   to enable proper handling of SIGINT etc.
  • Loading branch information
rbehrends authored and fingolfin committed Nov 15, 2019
1 parent d91899d commit ac4b898
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 15 deletions.
30 changes: 19 additions & 11 deletions hpcgap/lib/hpc/thread.g
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,27 @@ BindGlobal("NewInterruptID", function()
od;
end);

CreateThread(function()
local handlers;
handlers := rec(
SIGINT := DEFAULT_SIGINT_HANDLER,
SIGCHLD := DEFAULT_SIGCHLD_HANDLER,
SIGVTALRM := DEFAULT_SIGVTALRM_HANDLER,
SIGWINCH := DEFAULT_SIGWINCH_HANDLER
);
while true do
SIGWAIT(handlers);
od;
BindGlobal("InstallHPCGAPSignalHandling", function()
if not IsBound(SignalHandlerThread) then
BindGlobal("SignalHandlerThread", CreateThread(function()
local handlers;
handlers := rec(
SIGINT := DEFAULT_SIGINT_HANDLER,
SIGCHLD := DEFAULT_SIGCHLD_HANDLER,
SIGVTALRM := DEFAULT_SIGVTALRM_HANDLER,
SIGWINCH := DEFAULT_SIGWINCH_HANDLER
);
while true do
SIGWAIT(handlers);
od;
end));
fi;
end);

if IsHPCGAP and not SINGLE_THREAD_STARTUP() then
InstallHPCGAPSignalHandling();
fi;

#
# LockCounters are per region and per thread counters
# that count how many times a read or write lock
Expand Down
2 changes: 2 additions & 0 deletions lib/system.g
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ if IsHPCGAP then
rec( short:= "P", default := "0", arg := "<num>", help := ["set number of logical processors"] ),
rec( short:= "G", default := "0", arg := "<num>", help := ["set number of GC threads"] ),
rec( short:= "Z", default := false, help := ["enforce ordering of region locks"] ),
rec( long := "single-thread", default := false,
help := [ "enable/disable single-threaded startup" ]),
]);

MakeImmutable(GAPInfo.CommandLineOptionData);
Expand Down
1 change: 1 addition & 0 deletions src/boehm_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ void InitBags(UInt initial_size,
}
#endif
GC_set_all_interior_pointers(0);
GC_set_handle_fork(1);
GC_init();
GC_set_free_space_divisor(1);
TLAllocatorInit();
Expand Down
13 changes: 12 additions & 1 deletion src/gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1398,9 +1398,19 @@ static Obj FuncBREAKPOINT(Obj self, Obj arg)

static Obj FuncTHREAD_UI(Obj self)
{
return ThreadUI ? True : False;
return (ThreadUI && !SingleThreadStartup) ? True : False;
}

/****************************************************************************
**
*F FuncSINGLE_THREAD_STARTUP . . whether to start up in single-threaded mode
**
*/

static Obj FuncSINGLE_THREAD_STARTUP(Obj self)
{
return SingleThreadStartup ? True : False;
}

#endif

Expand Down Expand Up @@ -1486,6 +1496,7 @@ static StructGVarFunc GVarFuncs[] = {
GVAR_FUNC(KERNEL_INFO, 0, ""),
#ifdef HPCGAP
GVAR_FUNC(THREAD_UI, 0, ""),
GVAR_FUNC(SINGLE_THREAD_STARTUP, 0, ""),
#endif
GVAR_FUNC(MASTER_POINTER_NUMBER, 1, "ob"),
GVAR_FUNC(BREAKPOINT, 1, "integer"),
Expand Down
6 changes: 6 additions & 0 deletions src/hpc/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ UInt SyNumProcessors = 4;
*/
UInt SyNumGCThreads = 0;

/****************************************************************************
**
*V SingleThreadStartup . . . . . . . . . start HPC-GAP with just one thread
**
*/
UInt SingleThreadStartup = 0;

/****************************************************************************
**
Expand Down
7 changes: 7 additions & 0 deletions src/hpc/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ extern UInt SyNumProcessors;
*/
extern UInt SyNumGCThreads;

/****************************************************************************
**
*V SingleThreadStartup . . . . . . . . . start HPC-GAP with just one thread
**
*/
extern UInt SingleThreadStartup;

/****************************************************************************
**
*F MergeSort() . . . . . . . . . . . . . . . sort an array using mergesort.
Expand Down
7 changes: 4 additions & 3 deletions src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -1024,9 +1024,10 @@ static const struct optInfo options[] = {
{ 'q', "", toggle, &SyQuiet, 0 }, /* ?? */
#ifdef HPCGAP
{ 'S', "", toggle, &ThreadUI, 0 }, /* Thread UI */
{ 'Z', "", toggle, &DeadlockCheck, 0 }, /* Thread UI */
{ 'P', "", storePosInteger, &SyNumProcessors, 1 }, /* Thread UI */
{ 'G', "", storePosInteger, &SyNumGCThreads, 1 }, /* Thread UI */
{ 'Z', "", toggle, &DeadlockCheck, 0 }, /* Deadlock prevention */
{ 'P', "", storePosInteger, &SyNumProcessors, 1 }, /* number of CPUs */
{ 'G', "", storePosInteger, &SyNumGCThreads, 1 }, /* number of GC threads */
{ 0 , "single-thread", toggle, &SingleThreadStartup, 0 }, /* startup with one thread only */
#endif
/* The following options must be handled in the kernel so they are set up before loading the library */
{ 0 , "prof", enableProfilingAtStartup, 0, 1}, /* enable profiling at startup */
Expand Down

0 comments on commit ac4b898

Please sign in to comment.