Mark3 Realtime Kernel
message.h File Reference

Inter-thread communication via message-passing Embedded systems guru Jack Ganssle once said that without a robust form of interprocess communications (IPC), an RTOS is just a toy. Mark3 implements a form of IPC to provide safe and flexible messaging between threads. More...

#include "kerneltypes.h"
#include "mark3cfg.h"
#include "ll.h"
#include "ksemaphore.h"
#include "timerlist.h"

Go to the source code of this file.

Classes

class  Mark3::Message
 the Message class. This object provides threadsafe message-based IPC services based on exchange of objects containing a data pointer and minimal application-defined metadata. Messages are to be allocated/produced by the sender, and deallocated/consumed by the receiver. More...
 
class  Mark3::MessagePool
 The MessagePool Class The MessagePool class implements a simple allocator for message objects exchanged between threads. The sender allocates (pop's) messages, then sends them to the receiver. Upon receipt, it is the receiver's responsibility to deallocate (push) the message back to the pool. More...
 
class  Mark3::MessageQueue
 The MessageQueue class. Implements a mechanism used to send/receive data between threads. Allows threads to block, waiting for messages to be sent from other contexts. More...
 

Namespaces

 Mark3
 

Detailed Description

Inter-thread communication via message-passing Embedded systems guru Jack Ganssle once said that without a robust form of interprocess communications (IPC), an RTOS is just a toy. Mark3 implements a form of IPC to provide safe and flexible messaging between threads.

using kernel-managed IPC offers significant benefits over other forms of data sharing (i.e. Global variables) in that it avoids synchronization issues and race conditions common to the practice. using IPC also enforces a more disciplined coding style that keeps threads decoupled from one another and minimizes global data, preventing careless and hard-to-debug errors.

using Messages, Queues, and the Global Message Pool

// Declare a message queue shared between two threads
MessageQueue my_queue;
int main()
{
...
// Initialize the message queue
my_queue.init();
...
}
void Thread1()
{
// Example TX thread - sends a message every 10ms
while(1)
{
// Grab a message from the global message pool
Message *tx_message = GlobalMessagePool::Pop();
// Set the message data/parameters
tx_message->SetCode( 1234 );
tx_message->SetData( nullptr );
// Send the message on the queue.
my_queue.Send( tx_message );
Thread::Sleep(10);
}
}
void Thread2()
{
while()
{
// Blocking receive - wait until we have messages to process
Message *rx_message = my_queue.Recv();
// Do something with the message data...
// Return back into the pool when done
GlobalMessagePool::Push(rx_message);
}
}

Definition in file message.h.