GLAST/LAT > DAQ and FSW > FSW > Doxygen Index > PBS / V2-10-11

Constituent: pbs     Tag: sun-gcc


Interface   Data Structures   File List   Data Fields   Globals  

FORK.c File Reference

The FORK routines. More...

#include <stdio.h>
#include <string.h>
#include "PBI/Ofs.h"
#include "PBS/PL.ih"
#include "PBS/LI.ih"
#include "PBS/TASK.h"
#include "PBS/TOC.h"
#include "TASK_pvtdefs.h"
#include "PBS/FORK.h"

Include dependency graph for FORK.c:


Data Structures

union  _FORK_que
 Structure defining a FORK_que. More...
union  _FORK_que_head
 Structure defining a FORK_que_head. The FORK routines are set up to manage to types of ques, a simply linked list or a Priority List of ques. This is simply a union of the controlling list heads. More...
struct  _FORK_fcb
 Defines Fork Control Block containing the global information about the FORK que. More...

Typedefs

typedef _FORK_que_head FORK_que_head
 Typedef for a FORK_que_head.
typedef enum _FORK_msg_type FORK_msg_type
 Typedef for enum FORK_msg_type.

Enumerations

enum  _FORK_msg_type {
  FORK_K_MSG_TYPE_USR = 0,
  FORK_K_MSG_TYPE_SYS = 1
}
 Distinguishes between the two types of messages, user or system. More...

Functions

int FORK_fcb_sizeof (int nques)
 Returns the size, in bytes, of a FORK Control Block.
int FORK_create (FORK_fcb *fcb, const struct _TASK_attr *attributes, FORK_cb_rtn def_handler, FORK_cb_prm ctx, FORK_cb_prm tmo_ctx, const TOC *tmo_toc, int nques, const FORK_que_cfg *que_cfg, FORK_type msg_type, FORK_msg_sys *msg_blks, int msg_cnt)
 Creates a FORK message que.
FORK_queFORK_que_get (FORK_fcb *fcb, int que_id)
 Returns a handle to the specified que.
int FORK_destroy (FORK_fcb *fcb)
 Destroys a FORK que.
FORK_que_list FORK_disable (FORK_fcb *fcb, FORK_que_list disable_list)
 Disables the servicing of the specified ques.
FORK_que_list FORK_enable (FORK_fcb *fcb, FORK_que_list enable_list)
 Enables the servicing of the specified ques.
FORK_que_list FORK_enabled (const FORK_fcb *fcb)
 Returns the enable/disabled state of the specified que.
int FORK_qdisable (FORK_que *que)
 Disables the servicing of the specified que.
int FORK_qenable (FORK_que *que)
 Enables the servicing of the specified que.
int FORK_qenabled (const FORK_que *que)
 Returns the enable/disabled state of all ques.
int FORK_qhead_modify (FORK_que *que, FORK_cb_modify modify, void *context)
 Sneaky way to modify the head (oldest) element on the specified FORK que.
int FORK_qtail_modify (FORK_que *que, FORK_cb_modify modify, void *context)
 Sneaky way to modify the tail (newest) element on the specified FORK que.
int FORK_qsys (FORK_que *que, FORK_cb_rtn handler, void *contents)
 Ques a message using the internal FORK message blocks.
int FORK_qsysW (FORK_que *que, FORK_cb_rtn handler, void *contents)
 Ques a message using the internal FORK message blocks.
int FORK_qsysW_toc (FORK_que *que, FORK_cb_rtn handler, void *contents, const TOC *tmo_toc)
 Ques a message using the internal FORK message blocks.
int FORK_qusr (FORK_que *que, FORK_cb_rtn handler, FORK_msg_hdr *msg_hdr)
 Ques a user composed message.
int FORK_resume (FORK_fcb *fcb)
 Resumes a previously suspended FORK task.
int FORK_suspend (FORK_fcb *fcb)
 Suspends the FORK task.
int FORK_join (FORK_fcb *fcb, int *exit_status)
 Waits until the FORK'd task is deleted.

Detailed Description

The FORK routines.

Author:
JJRussell - russell@slac.stanford.edu
   CVS $Id: FORK.c,v 1.12 2005/10/01 01:00:13 russell Exp $
   

SYNOPIS
The FORK routines provide a clean mechanism for implementing what looks like a function call across threads/tasks. An interlocked message que is created between the task that calls FORK_create() and the task created by the FORK_create(). Messages can be placed on this que by any task that has access to the que through the Fork Control Block. In this sense, it is a multi-writer, single-reader half-duplex communication link.
These routines serve much the same purpose as the VxWorks msgQ routines with some important advantages

1. The FORK routines allow user controller over memory allocation.

The msgQ routines allocate memory when a request to send a message is made. If no memory is available, the user may either have the call return with failure or block. Ignoring the 'return with failure option' a problem occurs when blocking is not an option, as in an interrupt service routine. The FORK routines allow the user to pre-allocate the message buffer before committing to servicing an interrupt. This ensures that the necessary resources are in hand.

2. The burden of creating the receiving task is taken on by the FORK_create() routine.

Since the metaphor of the FORK routines is to transparently fork the execution path, this seems more natural than manually creating an task/thread to handle the messages. In most cases, this is all the create task is going to do, ie pend on a message que and service the incominig messages.

3. The messsage to be queued is not copied twice as it is in the VxWorks routines (once when sending and once when receiving).

This is a direct consequence of allowing the user to do his own management of the message buffers. Many times memory allocation can be avoid altogether. All the FORK routines demand is two free 32 bit locations (one for a link management and the other for a dispatch routine). Both locations are used only when on the FORK que. Many times the user can find two free location in the piece of information being sent, thus avoiding allocation (and, more importantly, handling the case of failure to allocate) altogether.

4. The FORK routines offer the option of managing a number (up to 32) priority order ques.

This feature allows higher priority messages to be serviced first, effectively creating an out-of-band mechanism.

A good use for these would be a command receiver/command handler. Assuming the receiver and handler are in different threads, the receiver receives incoming commands, decides which FORK que they should be assigned to and the function that such be executed. The received packages this information along with the command parameters and ques it. The handler thread wakes out and executes the command.

USAGE
The user first creates the FORK que by calling FORK_create(). FORK_create() allows control over all the parameters of the FORK que, including the attributes of the FORK servicing task. The user also has the option having the FORK routines create an internally managed pool of message buffers. Choosing this option makes the FORK routines behave very similarly to the msqQ routines with a buffer size of 4 bytes.
A queued message always consists of two quantities, a routine to handle the message and the message contents itself. If the internal message pool, referred to here as the system pool, is used, the contents of the message are limited to a single 32 bit number. If the user chooses to que his own messages, the contents can be any size he wishes, his only obligation is to preface the message with a FORK_msg_hdr structure. Messages of varying sizes and types, ie both user and system messages, can be queued to the same FORK que. The deallocation of user messages is the responsibility of the user, while system messages are freed immediately after the dispatch routine finishes its processing.

This latter design choice has only a slight impact on performance, since, in theory, the dispatch routine could release the message memory earlier. However, given the single reader nature of the FORK utilities, only one such buffer can be outstanding at an given time. The truly concerned user can avoid this limitation by using only message buffers that he manages.

EXAMPLE
    int test_fork (void)
    {
    FORK_fcb   *fcb;
    FORK_que   *que;
    int        size;
    int exit_status;

    / * Allocate 10 message blocks for the FORK ques internal pool * /
    FORK_msg_sys msg_blks[10];
    int          msg_blk_cnt = sizeof (msg_blks) / sizeof (*msg_blks);

    size = FORK_fcb_sizeof (1);             / * Size of Fork Control Block  * /
    fcb  = (FORK_fcb *) MBA_alloc (size);   / * Allocate the memory for it  * /

    / * Create the fork que                                                 * /
    status = FORK_create (fcb,              / * Fork Control Block to init  * /
                          NULL,             / * Use default task attributes * /
                          normalPrint,      / * Default handling routine    * /
                          forkParameter,    / * Parameter passed to handler * /
                          forkTmoParameter, / * Parameter passed if timeout * /
                          TOC_FOREVER,      / * How long to pend before tmo * /
                          1,                / * One internal FORK que       * /
                          NULL,             / * Individual que cfg's        * /
                          FORK_K_TYPE_PENDING_WTO,,
                                            / * FIFO style blocking when    * /
                                            / * allocating system messges   * /
                          msg_blks,         / * A source of message blocks  * /
                          msg_blk_cnt)      / * Number of messge blocks     * /

                          
    / *
     |  Retrieve the que to post messages to. In this example the FORK
     |  routines were initialized to support only a single que, so the
     |  que to be located in que = 0
 /
    que    = FORK_get_que (fcb, 0);

    / * Post a series of messages to this que * /
    status = FORK_qsysW   (que, NULL,      "Hello World\n");
    status = FORK_qsysW   (que, boldPrint, "Goodbye World\n");
    status = FORK_qsysW   (que, winddown,  "Killing FORK'd task\n");
    status = FORK_join    (que, &exit_status);

    / *
     |   All done, kill the que. Note that this call blocks until the
     |   the FORK'd task has completed its work.
 / 
    status = FORK_destroy (fcb);
    free (fcb);

    return 0;
    }
    
    
    int normalPrint (void *parameter, FORK_msg_sys *msg)
    {
       fputs (msg->contents);
       return FORK_C_CONTINUE;
    }

    int boldPrint (void *parameter, FORK_msg_sys *msg)
    {
      printf ("%s", BOLD_ESC_SEQUENCE);
      fputs (msg->contents);
      printf ("%s", NORMAL_ESC_SEQUENCE);
      return FORK_C_CONTINUE;
    }

    int winddown (void *parameter, FORK_msg_sys *msg)
    {
      fputs (msg->contents);
      fflush (stdout);

      / * Indicate that this is it * /
      return 1;
    }
The usual precautions should be heeded. For example, in this example, not allowing the creating task to terminate when messages are still on the que. Since the memory is from stack, that would be bad.

Enumeration Type Documentation

enum _FORK_msg_type
 

Distinguishes between the two types of messages, user or system.

Enumerator:
FORK_K_MSG_TYPE_USR  The message type is a user provided
FORK_K_MSG_TYPE_SYS  The message type is a system provided


Function Documentation

int FORK_create FORK_fcb fcb,
const struct _TASK_attr attributes,
FORK_cb_rtn  def_handler,
FORK_cb_prm  ctx,
FORK_cb_prm  tmo_ctx,
const TOC tmo_toc,
int  nques,
const FORK_que_cfg que_cfg,
FORK_type  msg_type,
FORK_msg_sys msg_blks,
int  msg_cnt
 

Creates a FORK message que.

Parameters:
fcb The FORK Control Block to initialize
attributes The attributes to use when creating the FORK servicing task.
def_handler The address of a default FORK handler. This routine will be called back in one of two circumstances.
  1. The user does not provide an explicit routine queing the message.
  2. A timeout occurs

Parameters:
ctx Parameter passed transparently to the user's callback handler, except in the case of a timeout.
tmo_ctx Parameter passed transparently to the user's callback routine in case of a timeout. The user may use this parameter to distinguish a timeout condition from a normal message. (The message block will also be NULL in this case.)
tmo_toc Timeout control specification. This is used when a message is being pended for. If this value is TOC_FOREVER, the pend is indefinite.
Warning:
Do not pass NULL for this parameter. The standard intepretation of NULL for a timeout control block is as NO_WAIT. This is almost certainly not what one wants in the FORK routines.
Parameters:
nques If specified as 0 or 1, a single non-prioritized que is used. If specified as >1, multiple prioritized ques are used. que_cfg configures each of the ques.
que_cfg An array of que configuration blocks. If nques is specified as 0 or 1, this parameter is ignored. If the callback routine is specified as NULL for any individual que configuration, the default callback handler is used.
msg_type The blocking type to use when attempting allocate a message from the system queue. There are three blocking styles.
  1. Non-blocking, this is the equivalent of a poll and is not very useful
  2. Blocking without a timeout. This is the equivalent of a indefinite wait for a message.
  3. Blocking with a timeout.
    Note:
    This parameter is applicable if and only if one is using a system pool to allocate messages from. Use of a system pool is determined by a non-zero message count, msg_cnt
    Parameters:
    msg_blks A source of internal message blocks. This may be specified as NULL and is ignored if msg_cnt is specified as 0.
    Note:
    This parameter is applicable if and only if one is using a system pool to allocate messages from. Use of a system pool is determined by a non-zero message count, msg_cnt
    Parameters:
    msg_cnt The number of msg_blks. This may be specified as 0. In this case no system message pool is created and the values of the parameters msg_type and msg_blks are ignored.
    Returns:
    Status
    This routine creates the FORK message que and its associated task. The def_handler will be used as the callback handler when a message is queued without an explicit handler, see FORK_qsysW(), FORK_qsysWTO(), and FORK_quser(). It is also used when a timeout occurs.

int FORK_destroy FORK_fcb fcb  ) 
 

Destroys a FORK que.

Parameters:
fcb The Fork Control Block of the FORK que to destroy
Returns:
Status
Releases all the internal resources used by the FORK que. It does not release either the memory associated with any potential internal pool of message blocks that the FORK routines may be managing on behalf of the user, nor does it release the memory associated with the Fork Control Block itself. Both these are user providef blocks of memory and, therefore, user managed blocks of memories.

The usual method of winding down the FORK task is for the parent task to send a QUIT message to the FORK'd task. The parent task then calls FORK_join, which block until the FORK'd task exits. The FORK'd task performs any necessary clean-up then returns a non-zero status code from the callback routine. This causes the FORK'd task to delete itself, allowing the FORK_join() call to complete.

Warning:
As with all delete operations, care must be taken to ensure it is safe to destroy the FORK que. The above suggests one method of doing this.

int FORK_disable FORK_fcb fcb,
FORK_que_list  disable_list
 

Disables the servicing of the specified ques.

Parameters:
fcb The FORK Control Block
disable_list The list of ques to disable
Returns:
The list of previously of enabled ques

int FORK_enable FORK_fcb fcb,
FORK_que_list  enable_list
 

Enables the servicing of the specified ques.

Parameters:
fcb The FORK Control Block
enable_list The list of ques to enable
Returns:
The list of previously of enabled ques

FORK_que_list FORK_enabled const FORK_fcb fcb  ) 
 

Returns the enable/disabled state of the specified que.

Parameters:
fcb The FORK control block
Returns:
A list of the enabled ques, MSB = que 0.
Warning:
As with all queries of this type, the answer is only good until it changes. The user is cautioned on its usage. It is primarily provided as a debugging tool.

FORK_fcb_sizeof int  nques  ) 
 

Returns the size, in bytes, of a FORK Control Block.

Parameters:
nques The number of internal ques to be supported
Returns:
The sizeof of a fork control block
Returns the size of a FORK CONTROL BLOCK. This allows the user to allocate and control the FORK CONTROL BLOCK without making its internal structure known.

The FORK routines are capable of supporting multiple prioritized internal ques. This is done by specifying nques to be greater than 1.

int FORK_join FORK_fcb fcb,
int *  exit_status
 

Waits until the FORK'd task is deleted.

Parameters:
fcb The Fork Control Block
exit_status Returned as the exit status of the FORK'd task.
Returns:
Status of the join operation.
This allows the parent task to synchronize with the deletion of the FORK'd task.

The usual method of winding down the FORK task is for the parent task to send a QUIT message to the FORK'd task. The parent task then calls FORK_join, which block until the FORK'd task exits. The FORK'd task performs any necessary clean-up then returns a non-zero status code from the callback routine. (This status code is returned to the user as exit_status.) This causes the FORK'd task to delete itself, allowing the FORK_join() call to complete. The user is then free to call FORK_destroy() to return the resources garnered by the FORK fcb.

Note that the exit_status may be returned as any non-zero 32-bit value. This includes returning a pointer to a structure which may contain additional information. If one does this, one must be careful to ensure that the memory is viable after task deletion. For instance, don't return pointers to things on the stack.

int FORK_qdisable FORK_que que  ) 
 

Disables the servicing of the specified que.

Return values:
0,if the specified que was not originally enabled !=0, if the specified que was previously enabled
Parameters:
que The FORK que to disable
Warning:
Note that the return value is the original state of the all enables before the qdisable was performed.

int FORK_qenable FORK_que que  ) 
 

Enables the servicing of the specified que.

Return values:
0,if the specified que was not originally enabled !=0, if the specified que was previously enabled
Parameters:
que The FORK que to enable

int FORK_qenabled const FORK_que que  ) 
 

Returns the enable/disabled state of all ques.

Return values:
= 0 if the queue was previously disabled != 0 if the queue was previously enabled
Parameters:
que The que to query
Warning:
As with all queries of this type, the answer is only good until it changes. The user is cautioned on its usage. It is primarily provided as a debugging tool.

int FORK_qhead_modify FORK_que que,
FORK_cb_modify  modify,
void *  context
 

Sneaky way to modify the head (oldest) element on the specified FORK que.

Parameters:
que The target fork que.
modify A callback handler used to modify the que element. The return value of this callback routine is the return value of FORK_qhead_modify.
Warning:
If the que is empty, NULL is passed as the msg_hdr header in the callback routine.
Parameters:
context A user provided context parameter.
Returns:
Whatever the user callback routine does.

int FORK_qsys FORK_que que,
FORK_cb_rtn  handler,
void *  contents
 

Ques a message using the internal FORK message blocks.

Parameters:
que The target fork que.
handler A callback handler. This may be specified as NULL, in which case, the default handler established by FORK_create(), will handle the message.
contents A single 32-bit value representing the message contents.
Returns:
Status
Ques a message to the FORK message que using the internal system message pool. System messages are limited to queing a single 32-bit number. Of course one can always use this to que a pointer to another data structure. The user should consider FORK_qusr(), to que a more complicated message without absorbing another level of indirection.

Note that because the message is internally allocated it may be possible that this list is exhausted. This version of the routine returns immediately with failure if the list is exhausted. See FORK_qsysW() or FORK_qsysW_toc for wait forever and wait with a timeout versions

int FORK_qsysW FORK_que que,
FORK_cb_rtn  handler,
void *  contents
 

Ques a message using the internal FORK message blocks.

Parameters:
que The target fork que.
handler A callback handler. This may be specified as NULL, in which case, the default handler established by FORK_create(), will handle the message.
contents A single 32-bit value representing the message contents.
Returns:
Status
Ques a message to the FORK message que using the internal system message pool. System messages are limited to queing a single 32-bit number. Of course one can always use this to que a pointer to another data structure. The user should consider FORK_qusr(), to que a more complicated message without absorbing another level of indirection.

Note that because the message is internally allocated it may be possible that this list is exhausted. This version of the routine waits indefinitely until a message becomes available. See FORK_qsys() or FORK_qsysW_toc for no wait and wait with timeout versions.

int FORK_qsysW_toc FORK_que que,
FORK_cb_rtn  handler,
void *  contents,
const TOC tmo_toc
 

Ques a message using the internal FORK message blocks.

Parameters:
que The target fork que.
handler A callback handler. This may be specified as NULL, in which case, the default handler established by FORK_create(), will handle the message.
contents A single 32-bit value representing the message contents.
tmo_toc A timeout control structure. This is used when no system message blocks available. The values TOC_NOWAIT and TOC_FOREVER can be used here. The information in this structure is copied, so the user may free the memory after the call.
Returns:
Status
Ques a message to the FORK message que using the internal system message pool. System messages are limited to queing a single 32-bit number. Of course one can always use this to que a pointer to another data structure. The user should consider FORK_qusr(), to que a more complicated message without absorbing another level of indirection.

Note that because the message is internally allocated it may be possible that this list is exhausted. A timeout parameter is provided to handle this situation. See FORK_qsys() or FORK_qsysW for no wait and wait forever versions.

int FORK_qtail_modify FORK_que que,
FORK_cb_modify  modify,
void *  context
 

Sneaky way to modify the tail (newest) element on the specified FORK que.

Parameters:
que The target fork que.
modify A callback handler used to modify the que element. The return value of this callback routine is the return value of FORK_qhead_modify.
Warning:
If the que is empty, NULL is passed as the msg_hdr header in the callback routine.
Parameters:
context A user provided context parameter.
Returns:
Whatever the user callback routine does.

FORK_que * FORK_que_get FORK_fcb fcb,
int  que_id
 

Returns a handle to the specified que.

Parameters:
fcb The FORK Control Block
que_id The ID of the que
Returns:
A handle to the specified que or NULL if no such que exists.
The FORK que routines take a FORK que as a parameter. This is used routine is used to get that handle. When a single que is managed by the FORK routines, only a que_id of 0 is allowed. When multiple ques are managed by the FORK routines, then an a que_id up to nques - 1 is allowed, with que_id = 0 being the highest priority que.

int FORK_qusr FORK_que que,
FORK_cb_rtn  handler,
FORK_msg_hdr msg_hdr
 

Ques a user composed message.

Parameters:
que The target fork que.
handler A callback handler. This may be specified as NULL, in which case, the default handler established by FORK_create(), will handle the message.
msg_hdr Pointer to the message to que. It is mandatory that all queued messages are prefaced by a standard message header block
Returns:
Status
Ques a user defined message. This routine behaves in much the same manner as FORK_qsys(), except the message being queued is controlled almost entirely by the user. (The except part, is that the message must be prefaced by a standard message header.)

For the extra work involved in using this routine, the user gets to control his own memory allocation and the exact contents of the message. This means that there is almost no case, other than an internal corruption, under which this routine can fail.

int FORK_resume FORK_fcb fcb  ) 
 

Resumes a previously suspended FORK task.

Parameters:
fcb The FORK Control Block
Returns:
Status
Warning:
This routine and its companion, FORK_suspend(), are primarily intended to be used for debugging and test code purposes.

int FORK_suspend FORK_fcb fcb  ) 
 

Suspends the FORK task.

Parameters:
fcb The FORK Control Block
Returns:
Status
Warning:
This routine and its companion, FORK_resume(), are primarily intended to be used for debugging and test code purposes.


Generated on Wed Dec 20 06:48:51 2006 by  doxygen 1.4.4