40
K
A
DAK
Application Tasks
It is important to note that a copy of the sender's message parameters is sent to the
destination task. Once the sender's parameters have been copied into the message
envelope, the caller is free to reuse the parameter storage if desired. Thus, as soon as
procedure ajsend or ajmxsnd (or any of their variat ions) returns to its caller, the
parameter variables are free for reuse.
A message which is delivered to a task in one of its mailboxes is copied directly to the
task's stack prior to starting the task. However, when a message exchange is used, the
destination task must provide a pointer to storage large enough to hold the entire message
which it will receive. Since AMX unconditionally delivers messages of MAXMSZ (>= 12)
bytes in length, the message storage must be of at least that size.
The AMX task which receives a variety of messages should declare a union rmsg in order
to reference the different messages. Note that the union should include one instance of an
array amxmsg consisting of three long values to ensure that rmsg is large enough to hold
any AMX message.
union rmsg {
long amxmsg[3];
char c;
int i;
short int iarray[6];
char carray[12];
struct msg msga;
};
Then the task can be written as follows:
void taskn(int dummy)
{union rmsg *pmsg;
pmsg = (union rmsg *)&dummy; /* Pointer to received message */
:
:
The received message can now be accessed
using union pointer pmsg.
:
}
In this simple example, the task receiving the messages has no obvious way of
determining how to interpret the message. Is the message one character (pmsg->c) or a
whole structure (pmsg->msga)? This dilemma is usually solved by including an
application specific operation code with all messages.
Note that we have declared the task's received message as an integer and then used
pointer variable pmsg to access the parameters in the message. AMX passes parameters
by value with no concern for the restrictions imposed by various high level languages.
Hence a whole array or structure can be passed by value by AMX to a task. However,
not all C compilers support such parameter passing.