Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
[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.2  2004/08/14 19:37:57  rasky
22  * Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
23  *
24  * Revision 1.1  2004/06/06 15:11:08  bernie
25  * Import into DevLib.
26  *
27  */
28 #ifndef KERN_MSG_H
29 #define KERN_MSG_H
30
31 #include "event.h"
32 #include <mware/list.h>
33
34
35 typedef struct MsgPort
36 {
37         List  queue; /*!< Messages queued at this port */
38         Event evn;   /*!< Event to trigger when a message arrives */
39 } MsgPort;
40
41
42 typedef struct Msg
43 {
44         Node     link;      /*!< Link into message port queue */
45         MsgPort *replyPort; /*!< Port to which the msg is to be replied */
46         /* User data may follow */
47 } Msg;
48
49
50 /*! Initialize a message port */
51 INLINE void msg_initPort(MsgPort* port, Event event)
52 {
53         INITLIST(&port->queue);
54         port->evn = event;
55 }
56
57 /*! Queue \a msg into \a port, triggering the associated event */
58 INLINE void msg_put(MsgPort* port, Msg* msg)
59 {
60         ADDTAIL(&port->queue, &msg->link);
61         event_do(&port->evn);
62 }
63
64 /* Get the first message from the queue of \a port, or NULL if the port is empty */
65 INLINE Msg* msg_get(MsgPort* port)
66 {
67         return (Msg*)REMHEAD(&port->queue);
68 }
69
70 /*! Send back (reply) \a msg to its sender */
71 INLINE void msg_reply(Msg* msg)
72 {
73         msg_put(msg->replyPort, msg);
74 }
75
76 #endif /* KERN_MSG_H */