1 /*  Part of SWI-Prolog
2 
3     Author:        Jan Wielemaker
4     E-mail:        J.Wielemaker@vu.nl
5     WWW:           http://www.swi-prolog.org
6     Copyright (c)  2019, University of Amsterdam
7                          VU University Amsterdam
8 		         CWI, Amsterdam
9     All rights reserved.
10 
11     Redistribution and use in source and binary forms, with or without
12     modification, are permitted provided that the following conditions
13     are met:
14 
15     1. Redistributions of source code must retain the above copyright
16        notice, this list of conditions and the following disclaimer.
17 
18     2. Redistributions in binary form must reproduce the above copyright
19        notice, this list of conditions and the following disclaimer in
20        the documentation and/or other materials provided with the
21        distribution.
22 
23     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27     COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34     POSSIBILITY OF SUCH DAMAGE.
35 */
36 
37 #ifndef _PL_EVENT_H
38 #define _PL_EVENT_H
39 
40 typedef enum pl_event_type
41 { PLEV_ABORT = 0,			/* Execution aborted */
42   PLEV_ERASED_CLAUSE,			/* clause was erased */
43   PLEV_ERASED_RECORD,			/* record was erased */
44   PLEV_BREAK,				/* a break-point was set */
45   PLEV_BREAK_EXISTS,			/* existing breakpoint */
46   PLEV_NOBREAK,				/* a break-point was cleared */
47   PLEV_GCNOBREAK,			/* cleared due to clause GC */
48   PLEV_FRAMEFINISHED,			/* A watched frame was discarded */
49   PLEV_UNTABLE,				/* Stop tabling some predicate */
50 					/* Keep these two at the end */
51   PLEV_THREAD_EXIT,			/* A thread has finished */
52   PLEV_THIS_THREAD_EXIT			/* This thread has finished */
53 } pl_event_type;
54 
55 typedef struct event_callback
56 { Module		 module;	/* context module */
57   Procedure		 procedure;	/* procedure to use */
58   int		       (*function)();	/* C-function */
59   union
60   { struct fastheap_term *term;		/* closure */
61     void		 *pointer;	/* for C functions */
62   } closure;
63   int			 argc;		/* #context args */
64   struct event_callback *next;		/* next in chain */
65 } event_callback;
66 
67 typedef struct event_list
68 { event_callback *head;			/* First event handler */
69   event_callback *tail;			/* Last event handler */
70 #ifdef O_PLMT
71   recursiveMutex  lock;			/* Access lock */
72 #endif
73 } event_list;
74 
75 typedef struct event_type
76 { pl_event_type id;
77   atom_t	name;
78   int		argc;
79   unsigned      local : 1;
80   event_list  **location;
81 } event_type;
82 
83 COMMON(int)	delayEvents(void);
84 COMMON(int)	sendDelayedEvents(int noerror);
85 COMMON(int)	PL_call_event_hook(pl_event_type ev, ...);
86 COMMON(int)	PL_call_event_hook_va(pl_event_type ev, va_list args);
87 COMMON(int)	register_event_hook(event_list **list, int last,
88 				    term_t closure, int argc);
89 COMMON(int)	register_event_function(event_list **list, int last,
90 					int (*func)(), void *closure, int argc);
91 COMMON(void)	destroy_event_list(event_list **listp);
92 COMMON(int)	predicate_update_event(Definition def,
93 				       atom_t action, Clause cl ARG_LD);
94 COMMON(int)	retractall_event(Definition def, term_t head, atom_t start
95 				 ARG_LD);
96 
97 GLOBAL const event_type PL_events[PLEV_THIS_THREAD_EXIT+2];
98 
99 static inline event_list**
event_list_location(pl_event_type ev)100 event_list_location(pl_event_type ev)
101 { if ( likely(!PL_events[ev].local) )
102   { return PL_events[ev].location;
103   } else
104   { GET_LD
105     return (event_list**)(((char*)LD) + (size_t)PL_events[ev].location);
106   }
107 }
108 
109 
110 static inline int WUNUSED
callEventHook(pl_event_type ev,...)111 callEventHook(pl_event_type ev, ...)
112 { event_list **listp = event_list_location(ev);
113 
114   if ( *listp )
115   { va_list args;
116     int rc;
117 
118     va_start(args, ev);
119     rc = PL_call_event_hook_va(ev, args);
120     va_end(args);
121 
122     return rc;
123   }
124 
125   return TRUE;
126 }
127 
128 #endif /*_PL_EVENT_H*/
129