Write extensive documentation; Add simple locking.
[bertos.git] / kern / msg.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 1999,2001 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See devlib/README for information.
7  * -->
8  *
9  * \brief Simple inter-process messaging system
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.6  2005/01/22 04:20:26  bernie
22  *#* Write extensive documentation; Add simple locking.
23  *#*
24  *#* Revision 1.5  2004/11/28 23:20:25  bernie
25  *#* Remove obsolete INITLIST macro.
26  *#*
27  *#* Revision 1.4  2004/10/19 08:22:09  bernie
28  *#* msg_peek(): New function.
29  *#*
30  *#* Revision 1.3  2004/08/25 14:12:09  rasky
31  *#* Aggiornato il comment block dei log RCS
32  *#*
33  *#* Revision 1.2  2004/08/14 19:37:57  rasky
34  *#* Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
35  *#*
36  *#* Revision 1.1  2004/06/06 15:11:08  bernie
37  *#* Import into DevLib.
38  *#*
39  *#*/
40 #ifndef KERN_MSG_H
41 #define KERN_MSG_H
42
43 #include "event.h"
44 #include <mware/list.h>
45
46
47 /*!
48  * Handle queues of messages associated an action.
49  *
50  * A message port is an abstraction used to exchange information
51  * asynchronously between processes or other entities such as
52  * interrupts and call-back functions.
53  *
54  * This form of IPC is higher-level than bare signals and
55  * semaphores, because it sets a policy for exchanging
56  * structured data with well-defined synchronization and
57  * ownership semantics.
58  *
59  * Before using it, a message port must be initialized by
60  * calling msg_initPort(), which associates the port with
61  * an Event object, which can be setup to signal a process
62  * or invoke a call-back hook.
63  *
64  * A process or interrupt routine can deliver messages to any
65  * message port by calling msg_put().  By sending a message,
66  * the sender temporarly or permanently transfers ownership
67  * of its associated data to the receiver.
68  *
69  * Queuing a message to a port automatically triggers the
70  * associated Event to notify the receiver.  When the
71  * receiver wakes up, it usually invokes msg_get() to pick
72  * the next message from the port.
73  *
74  * Message ports can hold any number of pending messages,
75  * and receivers usually process them in FIFO order.
76  * Other scheduling policies are possible, but not implemented
77  * in this API.
78  *
79  * After the receiver has done processing a message, it replies
80  * it back to the sender with msg_reply(), which transfer
81  * ownership back to the original sender.  Replies are delivered
82  * to a reply port, which is nothing more than another MsgPort
83  * structure designated by the sender.
84  *
85  * Returning messages to senders is not mandatory, but it provides
86  * a convenient way to provide some kind of result and simplify
87  * the resource allocation scheme at the same time.
88  *
89  * When using signals to receive messages in a process, you
90  * call sig_wait() in an event-loop to wake up when messages
91  * are delivered to any of your ports.  When your process
92  * wakes up with the port signal active, multiple messages
93  * may already have queued up at the message port, and the
94  * process must process them all before returning to sleep.
95  * Signals don't keep a nesting count.
96  *
97  * A simple message loop works like this:
98  *
99  * \code
100  *      // Our message port.
101  *      static MsgPort test_port;
102  *
103  *      // A test message with two parameters and a result.
104  *      typedef struct
105  *      {
106  *              Msg msg;
107  *
108  *              int x, y;
109  *              int result;
110  *      } TestMsg;
111  *
112  *
113  *      // A process that sends two messages and waits for replies.
114  *      static void sender_proc(void)
115  *      {
116  *              MsgPort test_reply_port;
117  *              TestMsg msg1;
118  *              TestMsg msg2;
119  *              Msg *reply;
120  *
121  *              msg_initPort(&reply_port,
122  *                      event_createSignal(proc_current(), SIGF_SINGLE);
123  *
124  *              // Fill-in first message and send it out.
125  *              msg1.x = 3;
126  *              msg1.y = 2;
127  *              msg1.msg.replyPort = &test_reply_port;
128  *              msg_put(&test_port, &msg1);
129  *
130  *              // Fill-in second message and send it out too.
131  *              msg2.x = 5;
132  *              msg2.y = 4;
133  *              msg2.msg.replyPort = &test_reply_port;
134  *              msg_put(&test_port, &msg1);
135  *
136  *              // Wait for a reply...
137  *              sig_wait(SIG_SINGLE);
138  *
139  *              reply = (TestMsg *)msg_get(&test_reply_port);
140  *              ASSERT(reply != NULL);
141  *              ASSERT(reply->result == 5);
142  *
143  *              // Get reply to second message.
144  *              while (!(reply = (TestMsg *)msg_get(&test_reply_port))
145  *              {
146  *                      // Not yet, be patient and wait some more.
147  *                      sig_wait(SIG_SINGLE);
148  *              }
149  *
150  *              ASSERT(reply->result == 9);
151  *      }
152  *
153  *
154  *      // Receive messages and do something boring with them.
155  *      static void receiver_proc(void)
156  *      {
157  *              msg_initPort(&test_port,
158  *                      event_createSignal(proc_current(), SIGF_EXAMPLE);
159  *
160  *              proc_new(sender_proc, (iptr_t)&test_port,
161  *                      sender_stack, sizeof(sender_stack);
162  *
163  *              for (;;)
164  *              {
165  *                      sigmask_t sigs = sig_wait(SIGF_EXAMPLE | more_signals);
166  *
167  *                      if (sigs & SIGF_EXAMPLE)
168  *                      {
169  *                              TestMsg *emsg;
170  *                              while (emsg = (TestMsg *)msg_get(&test_port)
171  *                              {
172  *                                      // Do something with the message
173  *                                      emsg->result = emsg->x + emsg->y;
174  *                                      msg_reply((Msg *)msg);
175  *                              }
176  *                      }
177  *              }
178  *      }
179  * \end code
180  */
181 typedef struct MsgPort
182 {
183         List  queue;   /*!< Messages queued at this port. */
184         Event event;   /*!< Event to trigger when a message arrives. */
185 } MsgPort;
186
187
188 typedef struct Msg
189 {
190         Node     link;      /*!< Link into message port queue. */
191         MsgPort *replyPort; /*!< Port to which the msg is to be replied. */
192         /* User data may follow */
193 } Msg;
194
195
196 /*!
197  * Lock a message port.
198  *
199  * This is required before reading or manipulating
200  * any field of the MsgPort structure.
201  *
202  * \note Ports may be locked multiple times and each
203  *       call to msg_lockPort() must be paired with
204  *       a corresponding call to msg_unlockPort().
205  *
206  * \todo Add a configurable policy for locking against
207  *       interrupts and locking with semaphorse.
208  *
209  * \see msg_unlockPort()
210  */
211 INLINE void msg_lockPort(MsgPort *port)
212 {
213         proc_forbid();
214 }
215
216 /*!
217  * Unlock a message port.
218  *
219  * \see msg_lockPort()
220  */
221 INLINE void msg_unlockPort(MsgPort *port)
222 {
223         proc_permit();
224 }
225
226
227 /*! Initialize a message port */
228 INLINE void msg_initPort(MsgPort *port, Event event)
229 {
230         LIST_INIT(&port->queue);
231         port->event = event;
232 }
233
234 /*! Queue \a msg into \a port, triggering the associated event */
235 INLINE void msg_put(MsgPort *port, Msg *msg)
236 {
237         msg_portLock(port);
238         ADDTAIL(&port->queue, &msg->link);
239         msg_portUnlock(port);
240
241         event_do(&port->event);
242 }
243
244 /*!
245  * Get the first message from the queue of \a port.
246  *
247  * \return Pointer to the message or NULL if the port was empty.
248  */
249 INLINE Msg *msg_get(MsgPort *port)
250 {
251         Msg *msg;
252
253         msg_portLock(port);
254         msg = (Msg *)REMHEAD(&port->queue);
255         msg_portUnlock(port);
256
257         return msg;
258 }
259
260 /* Peek the first message in the queue of \a port, or NULL if the port is empty */
261 INLINE Msg *msg_peek(MsgPort *port)
262 {
263         Msg *msg;
264
265         msg_portLock(port);
266         msg = (Msg *)port->queue.head;
267         if (ISLISTEMPTY(&port->queue))
268                 msg = NULL;
269         msg_portUnlock(port);
270
271         return msg;
272 }
273
274 /*! Send back (reply) \a msg to its sender. */
275 INLINE void msg_reply(Msg *msg)
276 {
277         msg_put(msg->replyPort, msg);
278 }
279
280 #endif /* KERN_MSG_H */