Add dual-license information.
[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  * This file is part of DevLib - See devlib/README for information.
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.2  2004/06/03 11:27:09  bernie
22  * Add dual-license information.
23  *
24  * Revision 1.1  2004/05/23 17:27:00  bernie
25  * Import kern/ subdirectory.
26  *
27  */
28 #ifndef KERN_EVENT_H
29 #define KERN_EVENT_H
30
31 #include "config.h"
32
33 #ifdef CONFIG_KERNEL
34         #include "config_kern.h"
35         #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
36                 #include "signal.h"
37         #endif
38 #endif
39
40
41 /* Forward decl */
42 struct Process;
43
44
45 /*! Event types */
46 enum EventAction
47 {
48         EVENT_IGNORE,   /*!< No-op event */
49 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
50         EVENT_SIGNAL,   /*!< Send a signal to a process */
51 #endif
52         EVENT_SOFTINT   /*!< Trigger a software interrupt (i.e.: call user hook) */
53 };
54
55 //! User defined callback type
56 typedef void (*Hook)(void *);
57
58 typedef struct Event
59 {
60         enum EventAction action;
61         union
62         {
63 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
64                 struct
65                 {
66                         struct Process *sig_proc;  /* Process to be signalled */
67                         sig_t           sig_bit;   /* Signal to send */
68                 } Sig;
69 #endif
70                 struct
71                 {
72                         Hook  func;         /* Pointer to softint hook */
73                         void *user_data;    /* Data to be passed back to user hook */
74                 } Int;
75         } Ev;
76 } Event;
77
78
79 /*! Initialize an event with a softint */
80 #define INITEVENT_INT(e,f,u) \
81         ((e)->action = EVENT_SOFTINT,(e)->Ev.Int.func = (f), (e)->Ev.Int.user_data = (u))
82
83
84 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
85
86 /*! Initialize an event with a signal */
87 #define INITEVENT_SIG(e,p,s) \
88         ((e)->action = EVENT_SIGNAL,(e)->Ev.Sig.sig_proc = (p), (e)->Ev.Sig.sig_bit = (s))
89
90 /*! Trigger an event */
91 #define DOEVENT(e) \
92 do { \
93         if ((e)->action == EVENT_SIGNAL) \
94                 sig_signal((e)->Ev.Sig.sig_proc, (e)->Sig.sig_bit); \
95         else if ((e)->action == EVENT_SOFTINT) \
96                 (e)->Ev.Int.func((e)->Ev.Int.user_data); \
97 } while (0)
98
99 /*! Trigger an event (to be used inside interrupts) */
100 #define DOEVENT_INTR(e) \
101 do { \
102         if ((e)->action == EVENT_SIGNAL) \
103                 _sig_signal((e)->Ev.Sig.sig_proc, (e)->Ev.Sig.sig_bit); \
104         else if ((e)->action == EVENT_SOFTINT) \
105                 (e)->Ev.Int.func((e)->Ev.Int.user_data); \
106 } while (0)
107
108 #else /* !CONFIG_KERN_SIGNALS */
109
110 /*! Trigger an event */
111 #define DOEVENT(e) \
112 do { \
113         if ((e)->action == EVENT_SOFTINT) \
114                 (e)->Ev.Int.func((e)->Ev.Int.user_data); \
115 } while (0)
116
117 /*! Trigger an event (to be used inside interrupts) */
118 #define DOEVENT_INTR(e) \
119 do { \
120         if ((e)->action == EVENT_SOFTINT) \
121                 (e)->Ev.Int.func((e)->Ev.Int.user_data); \
122 } while (0)
123
124 #endif
125
126 #endif /* KERN_EVENT_H */