2013-09-05 13:40:54 +02:00
|
|
|
/*
|
2013-09-13 01:06:54 +02:00
|
|
|
* \brief Backend for end points of synchronous interprocess communication
|
2013-09-05 13:40:54 +02:00
|
|
|
* \author Martin Stein
|
|
|
|
* \date 2012-11-30
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2012-2013 Genode Labs GmbH
|
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _KERNEL__IPC_NODE_H_
|
|
|
|
#define _KERNEL__IPC_NODE_H_
|
|
|
|
|
|
|
|
/* core includes */
|
2014-12-01 15:09:51 +01:00
|
|
|
#include <kernel/fifo.h>
|
2015-04-16 11:25:23 +02:00
|
|
|
#include <kernel/interface.h>
|
2013-09-05 13:40:54 +02:00
|
|
|
|
|
|
|
namespace Kernel
|
|
|
|
{
|
|
|
|
/**
|
2013-09-13 01:06:54 +02:00
|
|
|
* Backend for end points of synchronous interprocess communication
|
2013-09-05 13:40:54 +02:00
|
|
|
*/
|
|
|
|
class Ipc_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Kernel::Ipc_node
|
|
|
|
{
|
2013-11-28 00:44:54 +01:00
|
|
|
protected:
|
2013-09-05 13:40:54 +02:00
|
|
|
|
|
|
|
enum State
|
|
|
|
{
|
2013-09-19 15:14:04 +02:00
|
|
|
INACTIVE = 1,
|
|
|
|
AWAIT_REPLY = 2,
|
|
|
|
AWAIT_REQUEST = 3,
|
|
|
|
PREPARE_REPLY = 4,
|
2013-09-05 13:40:54 +02:00
|
|
|
PREPARE_AND_AWAIT_REPLY = 5,
|
|
|
|
};
|
|
|
|
|
2013-11-28 00:44:54 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
class Message_buf;
|
|
|
|
|
2014-12-01 15:09:51 +01:00
|
|
|
typedef Kernel::Fifo<Message_buf> Message_fifo;
|
2013-11-28 00:44:54 +01:00
|
|
|
|
2013-09-05 13:40:54 +02:00
|
|
|
/**
|
|
|
|
* Describes the buffer for incoming or outgoing messages
|
|
|
|
*/
|
2013-09-13 01:06:54 +02:00
|
|
|
class Message_buf : public Message_fifo::Element
|
2013-09-05 13:40:54 +02:00
|
|
|
{
|
2013-09-13 01:06:54 +02:00
|
|
|
public:
|
|
|
|
|
|
|
|
void * base;
|
|
|
|
size_t size;
|
2013-09-16 17:01:52 +02:00
|
|
|
Ipc_node * src;
|
2013-09-05 13:40:54 +02:00
|
|
|
};
|
|
|
|
|
2013-09-13 01:06:54 +02:00
|
|
|
Message_fifo _request_queue;
|
|
|
|
Message_buf _inbuf;
|
|
|
|
Message_buf _outbuf;
|
2013-09-16 17:01:52 +02:00
|
|
|
Ipc_node * _outbuf_dst;
|
2014-12-01 15:10:33 +01:00
|
|
|
bool _outbuf_dst_help;
|
2013-09-13 01:06:54 +02:00
|
|
|
State _state;
|
2013-09-05 13:40:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Buffer next request from request queue in 'r' to handle it
|
|
|
|
*/
|
2015-04-16 11:25:23 +02:00
|
|
|
void _receive_request(Message_buf * const r);
|
2013-09-05 13:40:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive a given reply if one is expected
|
|
|
|
*
|
|
|
|
* \param base base of the reply payload
|
|
|
|
* \param size size of the reply payload
|
|
|
|
*/
|
2015-04-16 11:25:23 +02:00
|
|
|
void _receive_reply(void * const base, size_t const size);
|
2013-09-05 13:40:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert 'r' into request queue, buffer it if we were waiting for it
|
|
|
|
*/
|
2015-04-16 11:25:23 +02:00
|
|
|
void _announce_request(Message_buf * const r);
|
2013-09-05 13:40:54 +02:00
|
|
|
|
|
|
|
/**
|
2013-09-16 17:01:52 +02:00
|
|
|
* Cancel all requests in request queue
|
2013-09-05 13:40:54 +02:00
|
|
|
*/
|
2015-04-16 11:25:23 +02:00
|
|
|
void _cancel_request_queue();
|
2013-09-05 13:40:54 +02:00
|
|
|
|
|
|
|
/**
|
2013-09-16 17:01:52 +02:00
|
|
|
* Cancel request in outgoing buffer
|
2013-09-05 13:40:54 +02:00
|
|
|
*/
|
2015-04-16 11:25:23 +02:00
|
|
|
void _cancel_outbuf_request();
|
2013-09-05 13:40:54 +02:00
|
|
|
|
2013-09-16 17:01:52 +02:00
|
|
|
/**
|
|
|
|
* Cancel request in incoming buffer
|
|
|
|
*/
|
2015-04-16 11:25:23 +02:00
|
|
|
void _cancel_inbuf_request();
|
2013-09-05 13:40:54 +02:00
|
|
|
|
|
|
|
/**
|
2013-09-16 17:01:52 +02:00
|
|
|
* A request 'r' in inbuf or request queue was cancelled by sender
|
2013-09-05 13:40:54 +02:00
|
|
|
*/
|
2015-04-16 11:25:23 +02:00
|
|
|
void _announced_request_cancelled(Message_buf * const r);
|
2013-09-05 13:40:54 +02:00
|
|
|
|
|
|
|
/**
|
2013-09-16 17:01:52 +02:00
|
|
|
* The request in the outbuf was cancelled by receiver
|
|
|
|
*/
|
2015-04-16 11:25:23 +02:00
|
|
|
void _outbuf_request_cancelled();
|
2013-09-16 17:01:52 +02:00
|
|
|
|
2014-12-01 15:10:33 +01:00
|
|
|
/**
|
|
|
|
* Return wether we are the source of a helping relationship
|
|
|
|
*/
|
2015-04-16 11:25:23 +02:00
|
|
|
bool _helps_outbuf_dst();
|
2014-12-01 15:10:33 +01:00
|
|
|
|
2013-09-16 17:01:52 +02:00
|
|
|
/**
|
2014-04-04 13:41:04 +02:00
|
|
|
* IPC node returned from waiting due to reply receipt
|
2013-09-16 17:01:52 +02:00
|
|
|
*/
|
2014-04-04 13:41:04 +02:00
|
|
|
virtual void _send_request_succeeded() = 0;
|
2013-09-16 17:01:52 +02:00
|
|
|
|
|
|
|
/**
|
2014-04-04 13:41:04 +02:00
|
|
|
* IPC node returned from waiting due to reply cancellation
|
2013-09-16 17:01:52 +02:00
|
|
|
*/
|
2014-04-04 13:41:04 +02:00
|
|
|
virtual void _send_request_failed() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IPC node returned from waiting due to request receipt
|
|
|
|
*/
|
|
|
|
virtual void _await_request_succeeded() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IPC node returned from waiting due to request cancellation
|
|
|
|
*/
|
|
|
|
virtual void _await_request_failed() = 0;
|
2013-09-16 17:01:52 +02:00
|
|
|
|
2013-11-28 00:44:54 +01:00
|
|
|
protected:
|
|
|
|
|
|
|
|
/***************
|
|
|
|
** Accessors **
|
|
|
|
***************/
|
|
|
|
|
|
|
|
Ipc_node * outbuf_dst() { return _outbuf_dst; }
|
|
|
|
|
|
|
|
State state() { return _state; }
|
|
|
|
|
2013-09-16 17:01:52 +02:00
|
|
|
public:
|
|
|
|
|
2015-04-16 11:25:23 +02:00
|
|
|
Ipc_node();
|
|
|
|
~Ipc_node();
|
2013-09-05 13:40:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a request and wait for the according reply
|
|
|
|
*
|
2014-03-27 14:23:49 +01:00
|
|
|
* \param dst targeted IPC node
|
|
|
|
* \param buf_base base of receive buffer and request message
|
|
|
|
* \param buf_size size of receive buffer
|
|
|
|
* \param msg_size size of request message
|
2014-12-01 15:10:33 +01:00
|
|
|
* \param help wether the request implies a helping relationship
|
2013-09-05 13:40:54 +02:00
|
|
|
*/
|
2014-03-27 14:23:49 +01:00
|
|
|
void send_request(Ipc_node * const dst, void * const buf_base,
|
2014-12-01 15:10:33 +01:00
|
|
|
size_t const buf_size, size_t const msg_size,
|
2015-04-16 11:25:23 +02:00
|
|
|
bool help);
|
2014-12-01 15:10:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return root destination of the helping-relation tree we are in
|
|
|
|
*/
|
2015-04-16 11:25:23 +02:00
|
|
|
Ipc_node * helping_sink();
|
2014-12-01 15:10:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Call function 'f' of type 'void (Ipc_node *)' for each helper
|
|
|
|
*/
|
|
|
|
template <typename F> void for_each_helper(F f)
|
|
|
|
{
|
|
|
|
/* if we have a helper in the receive buffer, call 'f' for it */
|
|
|
|
if (_state == PREPARE_REPLY || _state == PREPARE_AND_AWAIT_REPLY) {
|
|
|
|
if (_inbuf.src->_outbuf_dst_help) { f(_inbuf.src); } }
|
|
|
|
|
|
|
|
/* call 'f' for each helper in our request queue */
|
|
|
|
_request_queue.for_each([f] (Message_buf * const b) {
|
|
|
|
if (b->src->_outbuf_dst_help) { f(b->src); } });
|
2013-09-05 13:40:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait until a request has arrived and load it for handling
|
|
|
|
*
|
2014-03-27 14:23:49 +01:00
|
|
|
* \param buf_base base of receive buffer
|
|
|
|
* \param buf_size size of receive buffer
|
2014-03-27 12:20:55 +01:00
|
|
|
*
|
|
|
|
* \return wether a request could be received already
|
2013-09-05 13:40:54 +02:00
|
|
|
*/
|
2014-03-27 14:23:49 +01:00
|
|
|
bool await_request(void * const buf_base,
|
2015-04-16 11:25:23 +02:00
|
|
|
size_t const buf_size);
|
2013-09-05 13:40:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reply to last request if there's any
|
|
|
|
*
|
2014-03-27 14:23:49 +01:00
|
|
|
* \param msg_base base of reply message
|
|
|
|
* \param msg_size size of reply message
|
2013-09-05 13:40:54 +02:00
|
|
|
*/
|
2014-03-27 14:23:49 +01:00
|
|
|
void send_reply(void * const msg_base,
|
2015-04-16 11:25:23 +02:00
|
|
|
size_t const msg_size);
|
2013-09-16 17:01:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If IPC node waits, cancel '_outbuf' to stop waiting
|
2013-09-05 13:40:54 +02:00
|
|
|
*/
|
2015-04-16 11:25:23 +02:00
|
|
|
void cancel_waiting();
|
2013-09-05 13:40:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _KERNEL__IPC_NODE_H_ */
|