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.
9 * \brief Simple inter-process messaging system
11 * This module implements a common system for executing
12 * a user defined action calling a hook function.
16 * \author Bernardo Innocenti <bernie@develer.com>
21 *#* Revision 1.6 2005/01/22 04:20:26 bernie
22 *#* Write extensive documentation; Add simple locking.
24 *#* Revision 1.5 2004/11/28 23:20:25 bernie
25 *#* Remove obsolete INITLIST macro.
27 *#* Revision 1.4 2004/10/19 08:22:09 bernie
28 *#* msg_peek(): New function.
30 *#* Revision 1.3 2004/08/25 14:12:09 rasky
31 *#* Aggiornato il comment block dei log RCS
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.
36 *#* Revision 1.1 2004/06/06 15:11:08 bernie
37 *#* Import into DevLib.
44 #include <mware/list.h>
48 * Handle queues of messages associated an action.
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.
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.
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.
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.
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.
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
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.
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.
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.
97 * A simple message loop works like this:
100 * // Our message port.
101 * static MsgPort test_port;
103 * // A test message with two parameters and a result.
113 * // A process that sends two messages and waits for replies.
114 * static void sender_proc(void)
116 * MsgPort test_reply_port;
121 * msg_initPort(&reply_port,
122 * event_createSignal(proc_current(), SIGF_SINGLE);
124 * // Fill-in first message and send it out.
127 * msg1.msg.replyPort = &test_reply_port;
128 * msg_put(&test_port, &msg1);
130 * // Fill-in second message and send it out too.
133 * msg2.msg.replyPort = &test_reply_port;
134 * msg_put(&test_port, &msg1);
136 * // Wait for a reply...
137 * sig_wait(SIG_SINGLE);
139 * reply = (TestMsg *)msg_get(&test_reply_port);
140 * ASSERT(reply != NULL);
141 * ASSERT(reply->result == 5);
143 * // Get reply to second message.
144 * while (!(reply = (TestMsg *)msg_get(&test_reply_port))
146 * // Not yet, be patient and wait some more.
147 * sig_wait(SIG_SINGLE);
150 * ASSERT(reply->result == 9);
154 * // Receive messages and do something boring with them.
155 * static void receiver_proc(void)
157 * msg_initPort(&test_port,
158 * event_createSignal(proc_current(), SIGF_EXAMPLE);
160 * proc_new(sender_proc, (iptr_t)&test_port,
161 * sender_stack, sizeof(sender_stack);
165 * sigmask_t sigs = sig_wait(SIGF_EXAMPLE | more_signals);
167 * if (sigs & SIGF_EXAMPLE)
170 * while (emsg = (TestMsg *)msg_get(&test_port)
172 * // Do something with the message
173 * emsg->result = emsg->x + emsg->y;
174 * msg_reply((Msg *)msg);
181 typedef struct MsgPort
183 List queue; /*!< Messages queued at this port. */
184 Event event; /*!< Event to trigger when a message arrives. */
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 */
197 * Lock a message port.
199 * This is required before reading or manipulating
200 * any field of the MsgPort structure.
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().
206 * \todo Add a configurable policy for locking against
207 * interrupts and locking with semaphorse.
209 * \see msg_unlockPort()
211 INLINE void msg_lockPort(MsgPort *port)
217 * Unlock a message port.
219 * \see msg_lockPort()
221 INLINE void msg_unlockPort(MsgPort *port)
227 /*! Initialize a message port */
228 INLINE void msg_initPort(MsgPort *port, Event event)
230 LIST_INIT(&port->queue);
234 /*! Queue \a msg into \a port, triggering the associated event */
235 INLINE void msg_put(MsgPort *port, Msg *msg)
238 ADDTAIL(&port->queue, &msg->link);
239 msg_portUnlock(port);
241 event_do(&port->event);
245 * Get the first message from the queue of \a port.
247 * \return Pointer to the message or NULL if the port was empty.
249 INLINE Msg *msg_get(MsgPort *port)
254 msg = (Msg *)REMHEAD(&port->queue);
255 msg_portUnlock(port);
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)
266 msg = (Msg *)port->queue.head;
267 if (ISLISTEMPTY(&port->queue))
269 msg_portUnlock(port);
274 /*! Send back (reply) \a msg to its sender. */
275 INLINE void msg_reply(Msg *msg)
277 msg_put(msg->replyPort, msg);
280 #endif /* KERN_MSG_H */