Remove obsolete INITLIST macro.
[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.5  2004/11/28 23:20:25  bernie
22  *#* Remove obsolete INITLIST macro.
23  *#*
24  *#* Revision 1.4  2004/10/19 08:22:09  bernie
25  *#* msg_peek(): New function.
26  *#*
27  *#* Revision 1.3  2004/08/25 14:12:09  rasky
28  *#* Aggiornato il comment block dei log RCS
29  *#*
30  *#* Revision 1.2  2004/08/14 19:37:57  rasky
31  *#* Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
32  *#*
33  *#* Revision 1.1  2004/06/06 15:11:08  bernie
34  *#* Import into DevLib.
35  *#*
36  *#*/
37 #ifndef KERN_MSG_H
38 #define KERN_MSG_H
39
40 #include "event.h"
41 #include <mware/list.h>
42
43
44 typedef struct MsgPort
45 {
46         List  queue; /*!< Messages queued at this port */
47         Event evn;   /*!< Event to trigger when a message arrives */
48 } MsgPort;
49
50
51 typedef struct Msg
52 {
53         Node     link;      /*!< Link into message port queue */
54         MsgPort *replyPort; /*!< Port to which the msg is to be replied */
55         /* User data may follow */
56 } Msg;
57
58
59 /*! Initialize a message port */
60 INLINE void msg_initPort(MsgPort* port, Event event)
61 {
62         LIST_INIT(&port->queue);
63         port->evn = event;
64 }
65
66 /*! Queue \a msg into \a port, triggering the associated event */
67 INLINE void msg_put(MsgPort* port, Msg* msg)
68 {
69         ADDTAIL(&port->queue, &msg->link);
70         event_do(&port->evn);
71 }
72
73 /* Get the first message from the queue of \a port, or NULL if the port is empty */
74 INLINE Msg* msg_get(MsgPort* port)
75 {
76         return (Msg*)REMHEAD(&port->queue);
77 }
78
79 /* Peek the first message in the queue of \a port, or NULL if the port is empty */
80 INLINE Msg *msg_peek(MsgPort *port)
81 {
82         if (ISLISTEMPTY(&port->queue))
83                 return NULL;
84
85         return (Msg *)port->queue.head;
86 }
87
88 /*! Send back (reply) \a msg to its sender */
89 INLINE void msg_reply(Msg* msg)
90 {
91         msg_put(msg->replyPort, msg);
92 }
93
94 #endif /* KERN_MSG_H */