Import kern/ subdirectory.
[bertos.git] / kern / event.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 1999,2001,2003 Bernardo Innocenti <bernie@develer.com>
6  * All Rights Reserved.
7  * -->
8  *
9  * \brief Events handling
10  *
11  * This module implements a common system for executing
12  * a user defined action calling a hook function.
13  *
14  * \version $Id$
15  *
16  * \author Bernardo Innocenti <bernie@develer.com>
17  */
18
19 /*
20  * $Log$
21  * Revision 1.1  2004/05/23 17:27:00  bernie
22  * Import kern/ subdirectory.
23  *
24  */
25 #ifndef KERN_EVENT_H
26 #define KERN_EVENT_H
27
28 #include "config.h"
29
30 #ifdef CONFIG_KERNEL
31         #include "config_kern.h"
32         #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
33                 #include "signal.h"
34         #endif
35 #endif
36
37
38 /* Forward decl */
39 struct Process;
40
41
42 /*! Event types */
43 enum EventAction
44 {
45         EVENT_IGNORE,   /*!< No-op event */
46 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
47         EVENT_SIGNAL,   /*!< Send a signal to a process */
48 #endif
49         EVENT_SOFTINT   /*!< Trigger a software interrupt (i.e.: call user hook) */
50 };
51
52 //! User defined callback type
53 typedef void (*Hook)(void *);
54
55 typedef struct Event
56 {
57         enum EventAction action;
58         union
59         {
60 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
61                 struct
62                 {
63                         struct Process *sig_proc;  /* Process to be signalled */
64                         sig_t           sig_bit;   /* Signal to send */
65                 } Sig;
66 #endif
67                 struct
68                 {
69                         Hook  func;         /* Pointer to softint hook */
70                         void *user_data;    /* Data to be passed back to user hook */
71                 } Int;
72         } Ev;
73 } Event;
74
75
76 /*! Initialize an event with a softint */
77 #define INITEVENT_INT(e,f,u) \
78         ((e)->action = EVENT_SOFTINT,(e)->Ev.Int.func = (f), (e)->Ev.Int.user_data = (u))
79
80
81 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
82
83 /*! Initialize an event with a signal */
84 #define INITEVENT_SIG(e,p,s) \
85         ((e)->action = EVENT_SIGNAL,(e)->Ev.Sig.sig_proc = (p), (e)->Ev.Sig.sig_bit = (s))
86
87 /*! Trigger an event */
88 #define DOEVENT(e) \
89 do { \
90         if ((e)->action == EVENT_SIGNAL) \
91                 sig_signal((e)->Ev.Sig.sig_proc, (e)->Sig.sig_bit); \
92         else if ((e)->action == EVENT_SOFTINT) \
93                 (e)->Ev.Int.func((e)->Ev.Int.user_data); \
94 } while (0)
95
96 /*! Trigger an event (to be used inside interrupts) */
97 #define DOEVENT_INTR(e) \
98 do { \
99         if ((e)->action == EVENT_SIGNAL) \
100                 _sig_signal((e)->Ev.Sig.sig_proc, (e)->Ev.Sig.sig_bit); \
101         else if ((e)->action == EVENT_SOFTINT) \
102                 (e)->Ev.Int.func((e)->Ev.Int.user_data); \
103 } while (0)
104
105 #else /* !CONFIG_KERN_SIGNALS */
106
107 /*! Trigger an event */
108 #define DOEVENT(e) \
109 do { \
110         if ((e)->action == EVENT_SOFTINT) \
111                 (e)->Ev.Int.func((e)->Ev.Int.user_data); \
112 } while (0)
113
114 /*! Trigger an event (to be used inside interrupts) */
115 #define DOEVENT_INTR(e) \
116 do { \
117         if ((e)->action == EVENT_SOFTINT) \
118                 (e)->Ev.Int.func((e)->Ev.Int.user_data); \
119 } while (0)
120
121 #endif
122
123 #endif /* KERN_EVENT_H */