This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/C/signals.c

429 lines
9.6 KiB
C
Raw Normal View History

2013-02-08 16:36:45 +00:00
/*************************************************************************
* *
* YAP Prolog *
* *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* *
* Copyright L.Damas, V. Santos Costa and Universidade do Porto 1985-- *
* *
**************************************************************************
* *
* File: signal.c *
* comments: Signal Handling & Debugger Support *
* *
* *
* *
*************************************************************************/
#ifdef SCCS
static char SccsId[] = "%W% %G%";
#endif
#define HAS_CACHE_REGS 1
#include "Yap.h"
#include "Yatom.h"
#include "YapHeap.h"
#include "eval.h"
#include "yapio.h"
#ifdef TABLING
#include "tab.macros.h"
#endif /* TABLING */
#include <stdio.h>
#if HAVE_STRING_H
#include <string.h>
#endif
#if HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <wchar.h>
2014-03-06 14:07:33 +00:00
#ifndef THREADS
#define worker_id 0
#endif
2013-02-08 16:36:45 +00:00
inline static void
2014-03-06 02:09:48 +00:00
do_signal(int wid, yap_signals sig USES_REGS)
2013-02-08 16:36:45 +00:00
{
2014-03-06 14:07:33 +00:00
#if THREADS
2014-03-06 02:09:48 +00:00
LOCK(REMOTE_SignalLock(wid));
if (!REMOTE_InterruptsDisabled(wid)) {
REMOTE_ThreadHandle(wid).current_yaam_regs->CreepFlag_ =
Unsigned(REMOTE_ThreadHandle(wid).current_yaam_regs->LCL0_);
2013-12-12 14:24:40 +00:00
if (sig != YAP_CREEP_SIGNAL)
2014-03-06 02:09:48 +00:00
REMOTE_ThreadHandle(wid).current_yaam_regs->EventFlag_ =
Unsigned(REMOTE_ThreadHandle(wid).current_yaam_regs->LCL0_);
}
2014-03-06 14:07:33 +00:00
UInt i = REMOTE_FirstActiveSignal(wid);
if (REMOTE_FirstActiveSignal(wid) != REMOTE_LastActiveSignal(wid)) {
do {
if (sig == REMOTE_ActiveSignals(wid)[i]) {
UNLOCK(REMOTE_SignalLock(wid));
return;
}
i++;
if (i == REMOTE_MaxActiveSignals(wid))
i = 0;
} while (i != REMOTE_LastActiveSignal(wid));
}
REMOTE_ActiveSignals(wid)[i] = sig;
REMOTE_LastActiveSignal(wid)++;
if (REMOTE_LastActiveSignal(wid) == REMOTE_MaxActiveSignals(wid))
REMOTE_LastActiveSignal(wid) = 0;
UNLOCK(REMOTE_SignalLock(wid));
#else
if (!LOCAL_InterruptsDisabled) {
Yap_regp->CreepFlag_ =
Unsigned(Yap_regp->LCL0_);
if (sig != YAP_CREEP_SIGNAL)
Yap_regp->EventFlag_ =
Unsigned(Yap_regp->LCL0_);
}
2014-03-06 02:09:48 +00:00
UInt i = LOCAL_FirstActiveSignal;
if (LOCAL_FirstActiveSignal != LOCAL_LastActiveSignal) {
do {
if (sig == LOCAL_ActiveSignals[i]) {
return;
}
i++;
if (i == LOCAL_MaxActiveSignals)
i = 0;
} while (i != LOCAL_LastActiveSignal);
}
LOCAL_ActiveSignals[i] = sig;
LOCAL_LastActiveSignal++;
if (LOCAL_LastActiveSignal == LOCAL_MaxActiveSignals)
LOCAL_LastActiveSignal = 0;
2014-03-06 14:07:33 +00:00
#endif
2013-02-08 16:36:45 +00:00
}
2014-03-06 02:09:48 +00:00
inline static int
2013-02-08 16:36:45 +00:00
undo_signal(yap_signals sig USES_REGS)
{
LOCK(LOCAL_SignalLock);
2014-03-06 02:09:48 +00:00
UInt i = LOCAL_FirstActiveSignal;
if (LOCAL_FirstActiveSignal != LOCAL_LastActiveSignal) {
do {
if (sig == LOCAL_ActiveSignals[i])
break;
i++;
if (i == LOCAL_MaxActiveSignals)
i = 0;
} while (i != LOCAL_LastActiveSignal);
}
if (i == LOCAL_LastActiveSignal) {
UNLOCK(LOCAL_SignalLock);
return FALSE;
}
while ((i+1) % LOCAL_MaxActiveSignals != LOCAL_LastActiveSignal) {
LOCAL_ActiveSignals[i] = LOCAL_ActiveSignals[(i+1) % LOCAL_MaxActiveSignals];
i++;
}
if (LOCAL_LastActiveSignal == 0)
LOCAL_LastActiveSignal = LOCAL_MaxActiveSignals-1;
else
LOCAL_LastActiveSignal--;
if (LOCAL_FirstActiveSignal != LOCAL_LastActiveSignal) {
2013-12-12 14:24:40 +00:00
CalculateStackGap( PASS_REGS1 );
2013-02-08 16:36:45 +00:00
}
UNLOCK(LOCAL_SignalLock);
2014-03-06 02:09:48 +00:00
return TRUE;
2013-02-08 16:36:45 +00:00
}
static Int
p_creep( USES_REGS1 )
{
Atom at;
PredEntry *pred;
at = AtomCreep;
pred = RepPredProp(PredPropByFunc(Yap_MkFunctor(at, 1),0));
CreepCode = pred;
2014-03-06 02:09:48 +00:00
do_signal(worker_id, YAP_CREEP_SIGNAL PASS_REGS);
2013-02-08 16:36:45 +00:00
return TRUE;
}
2013-12-11 09:34:43 +00:00
static Int
p_creep_fail( USES_REGS1 )
{
Atom at;
PredEntry *pred;
at = AtomCreep;
pred = RepPredProp(PredPropByFunc(Yap_MkFunctor(at, 1),0));
CreepCode = pred;
2014-03-06 02:09:48 +00:00
do_signal(worker_id, YAP_CREEP_SIGNAL PASS_REGS);
2013-12-11 09:34:43 +00:00
return FALSE;
}
2013-02-08 16:36:45 +00:00
static Int
p_stop_creeping( USES_REGS1 )
{
2014-03-06 02:09:48 +00:00
undo_signal( YAP_CREEP_SIGNAL PASS_REGS );
2013-02-08 16:36:45 +00:00
return TRUE;
}
static Int
p_creep_allowed( USES_REGS1 )
{
if (PP != NULL) {
2014-03-06 02:09:48 +00:00
undo_signal(YAP_CREEP_SIGNAL PASS_REGS);
2013-02-08 16:36:45 +00:00
LOCK(LOCAL_SignalLock);
2014-03-06 02:09:48 +00:00
if (!LOCAL_InterruptsDisabled) {
if (LOCAL_FirstActiveSignal == LOCAL_LastActiveSignal)
2013-12-12 14:24:40 +00:00
CalculateStackGap( PASS_REGS1 );
2013-02-08 16:36:45 +00:00
UNLOCK(LOCAL_SignalLock);
} else {
UNLOCK(LOCAL_SignalLock);
}
return TRUE;
}
2014-03-06 02:09:48 +00:00
UNLOCK(LOCAL_SignalLock);
2013-02-08 16:36:45 +00:00
return FALSE;
}
void
Yap_signal(yap_signals sig)
{
CACHE_REGS
2014-03-06 02:09:48 +00:00
do_signal(worker_id, sig PASS_REGS);
2013-02-08 16:36:45 +00:00
}
void
2014-03-06 02:09:48 +00:00
Yap_external_signal(int wid, yap_signals sig)
2013-02-08 16:36:45 +00:00
{
2014-03-06 14:07:33 +00:00
#if THREADS
2014-03-06 02:09:48 +00:00
REGSTORE *regcache = REMOTE_ThreadHandle(wid).current_yaam_regs;
2014-03-06 14:07:33 +00:00
#endif
2014-03-06 02:09:48 +00:00
do_signal(wid, sig PASS_REGS);
}
int
Yap_undo_signal__(yap_signals sig USES_REGS)
{
return undo_signal(sig PASS_REGS);
}
int
Yap_has_signal__(yap_signals sig USES_REGS)
{
LOCK(LOCAL_SignalLock);
UInt i = LOCAL_FirstActiveSignal;
if (LOCAL_FirstActiveSignal != LOCAL_LastActiveSignal) {
do {
if (sig == LOCAL_ActiveSignals[i]) {
UNLOCK(LOCAL_SignalLock);
return TRUE;
}
i++;
if (i == LOCAL_MaxActiveSignals)
i = 0;
} while (i != LOCAL_LastActiveSignal);
}
UNLOCK(LOCAL_SignalLock);
return FALSE;
}
int
Yap_has_signals__(yap_signals sig1, yap_signals sig2 USES_REGS)
{
LOCK(LOCAL_SignalLock);
UInt i = LOCAL_FirstActiveSignal;
if (LOCAL_FirstActiveSignal != LOCAL_LastActiveSignal) {
do {
if (sig1 == LOCAL_ActiveSignals[i] ||
sig2 == LOCAL_ActiveSignals[i]) {
UNLOCK(LOCAL_SignalLock);
return TRUE;
}
i++;
if (i == LOCAL_MaxActiveSignals)
i = 0;
} while (i != LOCAL_LastActiveSignal);
}
UNLOCK(LOCAL_SignalLock);
return FALSE;
}
int
Yap_only_has_signal__(yap_signals sig USES_REGS)
{
LOCK(LOCAL_SignalLock);
UInt i = LOCAL_FirstActiveSignal;
if (LOCAL_FirstActiveSignal != LOCAL_LastActiveSignal) {
do {
if (sig != LOCAL_ActiveSignals[i]) {
UNLOCK(LOCAL_SignalLock);
return FALSE;
}
i++;
if (i == LOCAL_MaxActiveSignals)
i = 0;
} while (i != LOCAL_LastActiveSignal);
} else {
UNLOCK(LOCAL_SignalLock);
return FALSE;
}
UNLOCK(LOCAL_SignalLock);
return TRUE;
}
int
Yap_only_has_signals__(yap_signals sig1, yap_signals sig2 USES_REGS)
{
LOCK(LOCAL_SignalLock);
UInt i = LOCAL_FirstActiveSignal;
if (LOCAL_FirstActiveSignal != LOCAL_LastActiveSignal) {
do {
if (sig1 != LOCAL_ActiveSignals[i] &&
sig2 != LOCAL_ActiveSignals[i]) {
UNLOCK(LOCAL_SignalLock);
return FALSE;
}
i++;
if (i == LOCAL_MaxActiveSignals)
i = 0;
} while (i != LOCAL_LastActiveSignal);
} else {
UNLOCK(LOCAL_SignalLock);
return FALSE;
}
UNLOCK(LOCAL_SignalLock);
return TRUE;
2013-02-08 16:36:45 +00:00
}
#ifdef DEBUG
static Int
p_debug( USES_REGS1 )
{ /* $debug(+Flag) */
int i = IntOfTerm(Deref(ARG1));
if (i >= 'a' && i <= 'z')
GLOBAL_Option[i - 96] = !GLOBAL_Option[i - 96];
return (1);
}
#endif
static Int
p_first_signal( USES_REGS1 )
{
2014-03-06 02:09:48 +00:00
Atom at;
yap_signals sig;
2013-02-08 16:36:45 +00:00
LOCK(LOCAL_SignalLock);
2013-11-12 08:34:26 +00:00
MUTEX_LOCK(&(LOCAL_ThreadHandle.tlock));
2013-02-08 16:36:45 +00:00
/* always do wakeups first, because you don't want to keep the
non-backtrackable variable bad */
2014-03-06 02:09:48 +00:00
if (LOCAL_FirstActiveSignal != LOCAL_LastActiveSignal) {
sig = LOCAL_ActiveSignals[LOCAL_FirstActiveSignal];
LOCAL_FirstActiveSignal++;
if (LOCAL_FirstActiveSignal == LOCAL_MaxActiveSignals)
LOCAL_FirstActiveSignal = 0;
} else {
sig = YAP_NO_SIGNAL;
}
switch (sig) {
case YAP_INT_SIGNAL:
at = AtomSigInt;
break;
case YAP_CREEP_SIGNAL:
at = AtomSigCreep;
break;
case YAP_TRACE_SIGNAL:
at = AtomSigTrace;
break;
case YAP_DEBUG_SIGNAL:
at = AtomSigDebug;
break;
case YAP_BREAK_SIGNAL:
at = AtomSigBreak;
break;
case YAP_FAIL_SIGNAL:
at = AtomFail;
break;
case YAP_STACK_DUMP_SIGNAL:
at = AtomSigStackDump;
break;
case YAP_STATISTICS_SIGNAL:
at = AtomSigStatistics;
break;
#ifdef SIGALRM
case YAP_ALARM_SIGNAL:
#endif
case YAP_WINTIMER_SIGNAL:
at = AtomSigAlarm;
break;
#ifdef SIGVTALRM
case YAP_VTALARM_SIGNAL:
at = AtomSigVTAlarm;
break;
#endif
case YAP_WAKEUP_SIGNAL:
at = AtomSigWakeUp;
break;
case YAP_ITI_SIGNAL:
at = AtomSigIti;
break;
#ifdef SIGPIPE
case YAP_PIPE_SIGNAL:
at = AtomSigPipe;
break;
#endif
#ifdef SIGHUP
case YAP_HUP_SIGNAL:
at = AtomSigHup;
break;
#endif
#ifdef SIGUSR1
case YAP_USR1_SIGNAL:
at = AtomSigUsr1;
break;
#endif
#ifdef SIGUSR2
case YAP_USR2_SIGNAL:
at = AtomSigUsr2;
break;
#endif
default:
2013-11-12 08:34:26 +00:00
MUTEX_UNLOCK(&(LOCAL_ThreadHandle.tlock));
2013-02-08 16:36:45 +00:00
UNLOCK(LOCAL_SignalLock);
2014-03-06 02:09:48 +00:00
return FALSE;
2013-02-08 16:36:45 +00:00
}
UNLOCK(LOCAL_SignalLock);
2014-03-06 02:09:48 +00:00
return Yap_unify(ARG1, MkAtomTerm(at));
2013-02-08 16:36:45 +00:00
}
static Int
p_continue_signals( USES_REGS1 )
{
2014-03-06 02:09:48 +00:00
yap_signals sig;
2013-02-08 16:36:45 +00:00
/* hack to force the signal anew */
2014-03-06 02:09:48 +00:00
if (LOCAL_FirstActiveSignal != LOCAL_LastActiveSignal) {
sig = LOCAL_ActiveSignals[LOCAL_FirstActiveSignal];
Yap_signal(sig);
2013-02-08 16:36:45 +00:00
}
2013-11-12 08:34:26 +00:00
MUTEX_UNLOCK(&(LOCAL_ThreadHandle.tlock));
2013-02-08 16:36:45 +00:00
return TRUE;
}
void
Yap_InitSignalCPreds(void)
{
/* Basic predicates for the debugger */
Yap_InitCPred("$creep", 0, p_creep, SafePredFlag);
2013-12-11 09:34:43 +00:00
Yap_InitCPred("$creep_fail", 0, p_creep_fail, SafePredFlag);
2013-02-08 16:36:45 +00:00
Yap_InitCPred("$stop_creeping", 0, p_stop_creeping, SafePredFlag);
Yap_InitCPred ("$first_signal", 1, p_first_signal, SafePredFlag|SyncPredFlag);
Yap_InitCPred ("$continue_signals", 0, p_continue_signals, SafePredFlag|SyncPredFlag);
Yap_InitCPred("$creep_allowed", 0, p_creep_allowed, 0);
#ifdef DEBUG
Yap_InitCPred("$debug", 1, p_debug, SafePredFlag|SyncPredFlag);
#endif
}
2014-03-06 02:09:48 +00:00
2014-03-08 00:03:58 +00:00
void *Yap_InitSignals(int wid)
2014-03-06 02:09:48 +00:00
{
2014-03-08 00:03:58 +00:00
void *ptr = (void *)malloc(sizeof(UInt)*REMOTE_MaxActiveSignals(wid));
return ptr;
2014-03-06 02:09:48 +00:00
}