Implement Halt Hook (request from Bernd).

This commit is contained in:
Vitor Santos Costa 2010-09-24 14:00:53 +01:00
parent 691d66cca3
commit 99c5a17b12
15 changed files with 98 additions and 8 deletions

View File

@ -187,6 +187,7 @@ AtomAdjust(Atom a)
#define HoldEntryAdjust(P) (P)
#define CodeCharPAdjust(P) (P)
#define CodeVoidPAdjust(P) (P)
#define HaltHookAdjust(P) (P)
#define recompute_mask(dbr)

View File

@ -505,6 +505,7 @@ X_API Term STD_PROTO(YAP_TermNil,(void));
X_API int STD_PROTO(YAP_AtomGetHold,(Atom));
X_API int STD_PROTO(YAP_AtomReleaseHold,(Atom));
X_API Agc_hook STD_PROTO(YAP_AGCRegisterHook,(Agc_hook));
X_API int STD_PROTO(YAP_HaltRegisterHook,(HaltHookFunc, void *));
X_API char *STD_PROTO(YAP_cwd,(void));
X_API Term STD_PROTO(YAP_OpenList,(int));
X_API Term STD_PROTO(YAP_ExtendList,(Term, Term));
@ -2957,6 +2958,12 @@ YAP_AGCRegisterHook(Agc_hook hook)
return old;
}
X_API int
YAP_HaltRegisterHook(HaltHookFunc hook, void * closure)
{
return Yap_HaltRegisterHook(hook, closure);
}
X_API char *
YAP_cwd(void)
{

View File

@ -1333,6 +1333,33 @@ Yap_InitWorkspace(UInt Heap, UInt Stack, UInt Trail, UInt Atts, UInt max_table_s
}
}
int
Yap_HaltRegisterHook (HaltHookFunc f, void * env)
{
struct halt_hook *h;
if (!(h = (struct halt_hook *)Yap_AllocCodeSpace(sizeof(struct halt_hook))))
return FALSE;
h->environment = env;
h->hook = f;
LOCK(BGL);
h->next = Yap_HaltHooks;
Yap_HaltHooks = h;
UNLOCK(BGL);
return TRUE;
}
static void
run_halt_hooks(int code)
{
struct halt_hook *hooke = Yap_HaltHooks;
while (hooke) {
hooke->hook(code, hooke->environment);
hooke = hooke->next;
}
}
void
Yap_exit (int value)
{
@ -1340,15 +1367,17 @@ Yap_exit (int value)
unmap_memory();
#endif /* YAPOR */
if (! (Yap_PrologMode & BootMode) ) {
#ifdef LOW_PROF
remove("PROFPREDS");
remove("PROFILING");
remove("PROFPREDS");
remove("PROFILING");
#endif
#if defined MYDDAS_MYSQL || defined MYDDAS_ODBC
Yap_MYDDAS_delete_all_myddas_structs();
Yap_MYDDAS_delete_all_myddas_structs();
#endif
if (! (Yap_PrologMode & BootMode) )
run_halt_hooks(value);
Yap_ShutdownLoadForeign();
}
exit(value);
}

View File

@ -62,6 +62,16 @@ typedef struct gc_ma_hash_entry_struct {
struct gc_ma_hash_entry_struct *next;
} gc_ma_hash_entry;
typedef void (*HaltHookFunc)(int, void *);
typedef struct halt_hook {
void * environment;
HaltHookFunc hook;
struct halt_hook *next;
} halt_hook_entry;
int STD_PROTO(Yap_HaltRegisterHook,(HaltHookFunc, void *));
typedef struct atom_hash_entry {
#if defined(YAPOR) || defined(THREADS)
rwlock_t AERWLock;

View File

@ -263,6 +263,8 @@
#define Stream Yap_heap_regs->yap_streams
#define Yap_HaltHooks Yap_heap_regs->yap_halt_hook
#define NOfFileAliases Yap_heap_regs->n_of_file_aliases
#define SzOfFileAliases Yap_heap_regs->sz_of_file_aliases
#define FileAliases Yap_heap_regs->file_aliases

View File

@ -263,6 +263,8 @@
struct stream_desc *yap_streams;
struct halt_hook *yap_halt_hook;
UInt n_of_file_aliases;
UInt sz_of_file_aliases;
struct AliasDescS *file_aliases;

View File

@ -263,6 +263,8 @@
Yap_heap_regs->yap_streams = NULL;
Yap_heap_regs->yap_halt_hook = NULL;
Yap_heap_regs->n_of_file_aliases = 0;
Yap_heap_regs->sz_of_file_aliases = 0;
Yap_heap_regs->file_aliases = NULL;

View File

@ -899,6 +899,18 @@ RestoreDBErasedIList(void)
}
}
static void
RestoreHaltHooks(void)
{
struct halt_hook *hooke = Yap_HaltHooks = HaltHookAdjust(Yap_HaltHooks);
while (hooke) {
hooke->next = HaltHookAdjust(hooke->next);
hooke = hooke->next;
}
}
static void
RestoreStreams(void)
{

View File

@ -263,6 +263,8 @@
RestoreStreams();
RestoreHaltHooks();
RestoreAliases();

View File

@ -610,7 +610,15 @@ CodeVoidPAdjust (void * addr)
return addr + HDiff;
}
inline EXTERN struct halt_hook *HaltHookAdjust (struct halt_hook *);
inline EXTERN struct halt_hook *
HaltHookAdjust (struct halt_hook * addr)
{
if (!addr)
return NULL;
return (struct halt_hook *) (CharP (addr) + HDiff);
}
inline EXTERN BlockHeader *BlockAdjust (BlockHeader *);
@ -620,8 +628,6 @@ BlockAdjust (BlockHeader * addr)
return (BlockHeader *) ((BlockHeader *) (CharP (addr) + HDiff));
}
inline EXTERN yamop *PtoOpAdjust (yamop *);
inline EXTERN yamop *

View File

@ -1384,7 +1384,7 @@ anonymous variables.
Punctuation tokens consist of one of the following characters:
@example
@center ( ) , [ ] @{ @} |
( ) , [ ] @{ @} |
@end example
These characters are used to group terms.
@ -3346,7 +3346,7 @@ Also refer to @code{copy_term/2}.
True when @var{List} is a proper list. That is, @var{List}
is bound to the empty list (nil) or a term with functor '.' and arity 2.
@item ?@var{Term1} =@= ?@var{Term2}
@item ?@var{Term1} =@@= ?@var{Term2}
@findex =@=/2
@syindex =@=/2
@cnindex =@=/2
@ -16695,6 +16695,14 @@ only two boolean flags are accepted: @code{YAPC_ENABLE_GC} and
@code{YAPC_ENABLE_AGC}. The first enables/disables the standard garbage
collector, the second does the same for the atom garbage collector.`
@item @code{int} YAP_HaltRegisterHook(@code{YAP_halt_hook f, void *closure})
@findex YAP_HaltRegisterHook (C-Interface function)
Register the function @var{f} to be called if YAP is halted. The
function is called with two arguments: the exit code of the process (@code{0}
if this cannot be determined on your operating system) and the closure
argument @var{closure}.
@c See also @code{at_halt/1}.
@end table

View File

@ -470,6 +470,9 @@ extern X_API int PROTO(YAP_AtomReleaseHold,(YAP_Atom));
/* void YAP_AtomReleaseHold(YAP_Atom) */
extern X_API YAP_agc_hook PROTO(YAP_AGCRegisterHook,(YAP_agc_hook));
/* void YAP_AtomReleaseHold(YAP_Atom) */
extern X_API int PROTO(YAP_HaltRegisterHook,(YAP_halt_hook, void *));
/* char *YAP_cwd(void) */
extern X_API char * PROTO(YAP_cwd,(void));

View File

@ -176,6 +176,8 @@ typedef struct {
typedef int (*YAP_agc_hook)(void *_Atom);
typedef void (*YAP_halt_hook)(int exit_code, void *closure);
/********* execution mode ***********************/
typedef enum

View File

@ -3249,6 +3249,7 @@ X_API void (*PL_signal(int sig, void (*func)(int)))(int)
X_API void PL_on_halt(void (*f)(int, void *), void *closure)
{
Yap_HaltRegisterHook((HaltHookFunc)f,closure);
}
void Yap_swi_install(void);

View File

@ -295,6 +295,9 @@ struct operator_entry *op_list OpList =NULL OpListAdjust
/* stream array */
struct stream_desc *yap_streams Stream =NULL RestoreStreams()
/* halt hooks */
struct halt_hook *yap_halt_hook Yap_HaltHooks =NULL RestoreHaltHooks()
/* stream aliases */
UInt n_of_file_aliases NOfFileAliases =0 void
UInt sz_of_file_aliases SzOfFileAliases =0 void