GLAST/LAT > DAQ and FSW > FSW > Doxygen Index > IPBS / V0-0-1

Constituent: pbs     Tag: i845e


Interface   Data Structures   File List   Data Fields   Globals  

WUT.c File Reference

WakeUp Timer facility. More...

#include "IPBS/WUT.h"
#include "IPBS/WUT_tmr.h"
#include "IPBS/TOV.h"
#include "IPBS/FPA.h"
#include "IPBS/MBA.h"
#include "IPBS/Q.h"
#include "IPBS/Q.ih"
#include "IPBS/PTS.h"
#include "WCT_pvtdefs.h"
#include "WUT_debug.c"

Include dependency graph for WUT.c:


Data Structures

struct  _WUT_keepalive_ctx
 Callback structure for the keepalive callbacks. More...
struct  _WUT_keepalive
 Captures the keepalive timer entry and context parameter. More...
struct  _WCB_common
 WakeUp Control Block. More...

Defines

#define WUT_DEBUG
 Used to include debugging code.
#define STATIC   static
 Kludge for Doxygen,.
#define MEMBER_OFFSET(_structure, _member)   ((int) &(((_structure *) 0) -> _member))
 Calculates the offset, in bytes, of the specified _member in _structure.
#define WUT_M_KEY   1
 WUT key used to allow some normally prohibited actions.

Typedefs

typedef _WUT_keepalive_ctx WUT_keepalive_ctx
 Typedef for WUT_keepalive_ctx.
typedef _WUT_keepalive WUT_keepalive
 Typedef for struct _WUT_keepalive.
typedef _WCB_common WCB_common
 Typedef for struct _WCB.
typedef _WCB WCB
 Typedef for struct _WCB.

Functions

STATIC WUT_tmradd_key (const WUT_tmr *wut)
 Adds an special key to the timer entry handle. Operations which are normally prohibited are allowed in special circumstances.
STATIC int has_key (const WUT_tmr *wut)
 Tests whether the WUT timer entry has the special key attached.
STATIC WUT_tmrremove_key (const WUT_tmr *wut)
 Removes the special key from a WUT timer entry.
STATIC WUT_tmrget_wut (const Q_node *node)
 Returns a pointer to top of the WUT timer structure.
STATIC WUT_state add_wut (WUT_tmr *wut, register TOV tov)
 Adds the specified WUT timer entry into the active que.
STATIC const Q_nodefind (const Q_head *que, register TOV tov)
 Scans the que to find the last timer entry with a timeout value smaller than tov.
STATIC int cmp_lt_tov (TOV tov_a, TOV tov_b)
 Platform implementation dependent routine to compare to timeout values.
STATIC int lock_que (WCB *wcb)
 Platform implementation dependent routine to lock the WUT timer que.
STATIC void unlock_que (WCB *wcb, int key)
 Platform implementation dependent routine to unlock the WUT timer que.
STATIC void arm_handler (WCB *wcb, TOV tov)
 Seeds the timer interrupt to handle expire a tov.
STATIC void arm_handler_nxt_wut (WCB *wcb, const WUT_tmr *wut)
 Seeds the timer interrupt to handle expire at the timeout associated with the specified WUT timer entry.
STATIC int wut_sys_connect (WCB *wcb)
 Platform dependent part of the WUT system connection process.
STATIC int wut_sys_shutdown (WCB *wcb)
 Platform specific shutdown procedure.
WUT_cb_status WUT_keepalive_rtn (WUT_keepalive_ctx *keepalive, WUT_tmr *wut)
 Simple WUT timer callback routine used to drive the VxWorks system clock.
WUT_cb_status WUT_keepalive_update (WUT_keepalive_ctx *keepalive, WUT_tmr *wut)
 Simple WUT timer callback routine used to update the WCT clock.
STATIC void init_wut (void *unused, WUT_tmr *wut, int pckt_size, int pcb_offset)
 Initializes each timer entry as it is added to the Free Packet Pool.
WUT_state WUT_cancel (WUT_tmr *wut)
 Cancels a WUT timer entry.
WUT_tmrWUT_create ()
 Allocate a WUT timer entry.
WUT_state WUT_destroy (WUT_tmr *wut)
 Destroys, ie frees, an unused WUT timer entry.
WUT_state WUT_expire (WUT_tmr *wut)
 Marks a wut timer as expire.
WUT_state WUT_restart (WUT_tmr *wut, register TOV tov)
 Restarts a WUT timer entry.
WUT_state WUT_start (WUT_tmr *wut, register TOV tov, WUT_cb_routine cb, void *prm)
 Establishes and starts a WUT timer entry.
WUT_state WUT_state_get (const WUT_tmr *wut)
 Gets the current state of the WUT timer entry.
TOV WUT_tov_get (const WUT_tmr *wut)
 Gets the current TOV of the WUT timer entry.
int WUT_overrun (const WUT_tmr *wut)
 Returns how 'on-time' the timer ISR was serviced.
unsigned int WUT_frequency ()
 Gets frequency, in Hertz of the WUT clock.
int WUT_sys_adjust (long long int dnsecx)
 Adjusts all the current entries on the WUT timer que in response to a change in the base time.
int WUT_sys_init (int count)
 One time WUT services initialization routine.
int WUT_sys_connect (unsigned int keepalive, unsigned int update)
 One time WUT connection routine to start WUT system.
int WUT_sys_reset (unsigned int keepalive, unsigned int update)
 Resets the keepalive and update times.
int WUT_sys_shutdown ()
 Shuts down the WUT routine.

Detailed Description

WakeUp Timer facility.

Author:
JJRussell - russell@slac.stanford.edu
   CVS $Id: WUT.c,v 1.1.1.1 2006/02/10 21:45:34 saxton Exp $

Implementation file for the os/platform/compiler independent WUT routines. These routines provide the same style of functionality as the VxWorks 'wdb' routines, but with two important differences.

Define Documentation

#define MEMBER_OFFSET _structure,
_member   )     ((int) &(((_structure *) 0) -> _member))
 

Calculates the offset, in bytes, of the specified _member in _structure.

Parameters:
_structure The name or typedef of the structure
_member The member name
Returns:
The offset, in bytes, of the specified _member in _structure

#define STATIC   static
 

Kludge for Doxygen,.

Doxygen does not like to float documentation for static functions across file boundaries. By defining a symbol for static, Doxygen no longer understands that the function is, in fact, static. Oh, the tangled WEB we weave.

#define WUT_M_KEY   1
 

WUT key used to allow some normally prohibited actions.

If this key is attached to the WUT timer handle, certain normally prohibited actions are allowed. In particular, one is allowed to DESTROY and REQUE WUT timer entries from the user callback routine. Note that this information cannot be carried with the state information, since all threads of execution would then have this priviledged status. One gets 'copy' semantics by attaching the key to the handle.


Typedef Documentation

WCB
 

Typedef for struct _WCB.

This structure contains all the control variables for the WakeUp Timer facility. It consists of a common or platform independent piece WCB_common and, optionally, a platform dependent piece.

WCB_common
 

Typedef for struct _WCB.

This structure contains all the control variables for the WakeUp Timer facility.

WUT_keepalive_ctx
 

Typedef for WUT_keepalive_ctx.

Currently there are two styles of keepalive callbacks. The usual callback depends on an external source to update the WCT clock. The external source is likely the GPS 1 PPS signal or the on-board auxilliary realtime clocks.
The other style is the a keepalive that updates the WCT clock itself. This is a little incestuous, but is useful in low performance, debugging work.


Function Documentation

STATIC WUT_tmr * add_key const WUT_tmr wut  )  [inline]
 

Adds an special key to the timer entry handle. Operations which are normally prohibited are allowed in special circumstances.

Parameters:
wut The WUT timer entry to modify.
Returns:
The modified WUT timer entry.
This is an internal routine only.

STATIC WUT_state add_wut WUT_tmr wut,
register TOV  tov
 

Adds the specified WUT timer entry into the active que.

Parameters:
wut The WUT timer entry to add
tov The timeout value.
Return values:
WUT_K_QUEUED 
This is an internal only routine. This routine scans the timer queue and places this entry before any other entries with a later timeout value. The active que is always ordered by timeout value, with the most quickly to expire at the top and the least at the bottom.

STATIC void arm_handler WCB wcb,
TOV  tov
[inline]
 

Seeds the timer interrupt to handle expire a tov.

Parameters:
wcb The WUT control block
tov The expiration timeout value

STATIC void arm_handler_nxt_wut WCB wcb,
const WUT_tmr wut
[inline]
 

Seeds the timer interrupt to handle expire at the timeout associated with the specified WUT timer entry.

Parameters:
wcb The WUT control block
wut The WUT timer entry.

STATIC int cmp_lt_tov TOV  tov_a,
TOV  tov_b
[inline]
 

Platform implementation dependent routine to compare to timeout values.

Parameters:
tov_a The first of the two timeout values to compare
tov_b The second of the two timeout values to compare
Returns:
non-zero if tov_a < tov_b, else 0

STATIC const Q_node * find const Q_head que,
register TOV  tov
[inline]
 

Scans the que to find the last timer entry with a timeout value smaller than tov.

Parameters:
que The active timer que to scan
tov The target expiration time
Returns:
The last timer entry with a timeout value smaller than tov.
This is an internal only routine.

STATIC WUT_tmr * get_wut const Q_node node  )  [inline]
 

Returns a pointer to top of the WUT timer structure.

Parameters:
node The node (que links) of the WUT timer entry
This is an internal routine only. WUT timer entries are queued on both a free list and an active list. Because the que links are not at the top of the structure, but at some fixed offset from the top, a uniform method of paging to the top of WUT timer structure is provided.

STATIC int has_key const WUT_tmr wut  )  [inline]
 

Tests whether the WUT timer entry has the special key attached.

Parameters:
wut The WUT timer entry to test.
Returns:
A non zero value if the special key is attached.
This is an internal routine only.

STATIC void init_wut void *  unused,
WUT_tmr wut,
int  pckt_size,
int  pcb_offset
 

Initializes each timer entry as it is added to the Free Packet Pool.

Parameters:
unused Unused parameter
wut The WUT timer entry to initialize
pckt_size The size of a packet
pcb_offset The offset to the FPA control structure
Initializes each timer entry as it is added to the WUT timer's free list.

STATIC int lock_que WCB wcb  )  [inline]
 

Platform implementation dependent routine to lock the WUT timer que.

Parameters:
wcb Pointer to the WUT control block
Returns:
A key to be used to unlock the que.

STATIC WUT_tmr * remove_key const WUT_tmr wut  )  [inline]
 

Removes the special key from a WUT timer entry.

Parameters:
wut The WUT timer entry to remove the key from.
This is an internal routine only.

STATIC void unlock_que WCB wcb,
int  key
[inline]
 

Platform implementation dependent routine to unlock the WUT timer que.

Parameters:
wcb Pointer to the WUT control block
key The key used to unlock the que (returned from lock_que()

WUT_state WUT_cancel WUT_tmr wut  ) 
 

Cancels a WUT timer entry.

Parameters:
wut The WUT timer entry to cancel
Returns:
The new state. If successful this is WUT_K_CANCELLED
Cancels a WUT timer entry. Due to race conditions, this is a very tricky operation. Only timer entries in the WUT_K_QUEUED state may be cancelled. If the timer entry is not in this state, the cancel operation fails and the current state is returned to the user.

WUT_tmr* WUT_create void   ) 
 

Allocate a WUT timer entry.

Returns:
If successful, a handle for WUT timer entry, else NULL
Allocates, without blocking a WUT timer entry. If successful, the state of the timer entry is updated to WUT_K_ALLOCATED.

WUT_state WUT_destroy WUT_tmr wut  ) 
 

Destroys, ie frees, an unused WUT timer entry.

Parameters:
wut The WUT timer entry to destroy
Returns:
If successful, WUT_K_FREE, else its current state.
A WUT timer entry can only be destroyed, ie returned to the free list, if it is in the state
  • WUT_K_ALLOCATED
  • WUT_K_CANCELLED
  • WUT_K_EXPIRED
  • or if it being destroyed as part of its ISR callback routine.

The user should ensure the timer entry was destroyed by checking that the returned state is WUT_K_FREE. Note that this value may also be returned if the timer entry was already in the the freed state.

WUT_state WUT_expire WUT_tmr wut  ) 
 

Marks a wut timer as expire.

Parameters:
wut The WUT timer entry to expire
Returns:
IF successful, WUT_K_EXPIRED, else the current state
This routine must only be called from the user's callback routine. The user must call this, or one of WUT_cancel, WUT_start or WUT_restart in his callback before calling the routine being used to synchronize with another thread of execution. If successful, the WUT timer entry will be changed from the ACTIVE state to the EXPIRED state. Once in the EXPIRED state, ownership of the timer entry is effectively transferred from the WUT facility back to the user.
Warning:
This mechanism should be used instead of passing back WUT_K_STATE_CHANGED_YES or WUT_K_STATE_CHANGED_NO from the user's callback routine. This mechanism will continued to be honored and will work correctly on platforms where the WUT callback routine executes in a higher priority thread (for us VxWorks and not any of the host platforms. So to be portable and correct, use this call in lieu of the return code. The return code in portable code should now always be WUT_K_STATE_CHANGED_YES.

This call is necessary on platforms on POSIX platforms that do not support task priorities. On these platforms, the WUT timer cannot be protected by having the callback routine run in a higher priority thread. This means that WUT driver servicing the timer que cannot touch the WUT timer after the user's callback has been called. If this were allowed, there is a possibility that as soon as the user calls his synchronization routine, the thread waiting on that synchronization mechanism can begin execution. In that case, the user would make the reasonable assumption that the WUT timer handle is no longer property of the WUT facility. This call effectively declares to the WUT timer entry is no longer property of the WUT facility.

A previous implementation marked the timer entry EXPIRED after the return, thus allowing a hole between when the user called his synchronization method and when the driver marked it as inactive when the WUT timer was still property of the WUT facility. The user ownership assumption was thus violated.
Technically the previous implementation was protected since the state of the timer was still active, thus prohibiting operations on the timer. However, while technically correct, this implementation gave the user no good method (other than spinning) to know when the timer entry went to the expired state.

unsigned int WUT_frequency void   ) 
 

Gets frequency, in Hertz of the WUT clock.

Returns:
The frequency in Hertz of the WUT clock
In principle this allows the caller to translate physical time units, like nanoseconds, into the internal time units used by the WUT facility. However, this is only a crude conversion. For more accurate conversions the TOV facility should be used. One should consider this function as for informational purposes only.

WUT_cb_status WUT_keepalive_rtn WUT_keepalive_ctx keepalive,
WUT_tmr wut
 

Simple WUT timer callback routine used to drive the VxWorks system clock.

Parameters:
keepalive The keepalive control structure.
wut The WUT timer entry
Return values:
WUT_K_STATE_CHANGE_YES 
VXWORKS Implementation Note
On the PowerPC platform, this routine is used to keep the decrement counter within its limited range, ensuring that no timer entry attempts to seed it with a value beyond it's limits. It is also used to update the VxWork's system clock by calling tickAnnounce().
Return Value
The return value reflects the fact the timer entries state has been changed by the callback routine (its been restarted).

WUT_cb_status WUT_keepalive_update WUT_keepalive_ctx keepalive,
WUT_tmr wut
 

Simple WUT timer callback routine used to update the WCT clock.

Parameters:
keepalive The keepalive control structure.
wut The WUT timer entry
Return values:
WUT_K_STATE_CHANGE_YES 
This routine is used in systems without an external clock, such as the sysAuxClock or the GPS.
The WUT timer entry updates the WCT clock and the timer entry is then requeued with the specified delay relative to it's previous expiration time. This creates a fairly accurate clock.
The return value reflects the fact the timer entries state has been changed by the callback routine (its been restarted).
VXWORKS IMPLEMENTATION NOTE
The VxWork's routine tickAnnounce(), is called to update the VxWork's system clock and WCT clock.

int WUT_overrun const WUT_tmr wut  ) 
 

Returns how 'on-time' the timer ISR was serviced.

Returns:
The current internal value of the WUT time
The caller may use this value to determine how 'on time' his callback routine was called. Remember, due to scheduling conflicts, the WUT facility may service the timer entry after its scheduled expiration time. It is up to the user to determine how late is 'too late'.

WUT_state WUT_restart WUT_tmr wut,
register TOV  tov
 

Restarts a WUT timer entry.

Parameters:
wut The WUT timer entry to restart.
tov The timeout value.
Returns:
If successful, WUT_K_QUEUED
Restarts an timer entry. Only inactive timer entries, ie those in the state WUT_K_ALLOCATED, WUT_K_CANCELLED and WUT_K_EXPIRED may be restarted or WUT_restart is being called from the timer entries callback handler. If the state of the timer entry to be restarted is not one of these, the operation fails and WUT_restart() returns the state of the timer entry.

WUT_state WUT_start WUT_tmr wut,
register TOV  tov,
WUT_cb_routine  cb,
void *  prm
 

Establishes and starts a WUT timer entry.

Parameters:
wut The WUT timer entry to restart.
tov The timeout value
cb The address of the WUT timer entry's callback service routine.
prm A user parameter passed transparently through to the timer entry's callback service routine
Returns:
If successful, WUT_K_QUEUED
Places the WUT timer entry on the active queue. The timer entry to be started must be in one of the inactive states, ie WUT_K_ALLOCATED, WUT_K_CANCELLED or WUT_K_EXPIRED or it must own the special key, allowing it is to be started from the callback routine. If it is not in one of these conditions are true, the current state of the timer entry is returned.
The name is a bit of a misnomer, suggesting that this routine can only be called when the timer is initially place on the active que. Although it must be called to initially place the entry on the active que, this routine can be called to at any time (provided the state permits). When called in this fashion, it behaves very similarly to WUT_restart(). The difference is that this routine allows one to reset not only a new expiration time, but also the callback routine and callback parameter.
Used cleverly, this routine could be used to create a state machine which executes at prescribed times, and whose next expiration callback is handled by a different routine.

WUT_state WUT_state_get const WUT_tmr wut  ) 
 

Gets the current state of the WUT timer entry.

Parameters:
wut The WUT timer entry to get the state for.
Returns:
The current state of the WUT timer entry.
Simple query routine to return the current state of a timer entry.
Warning:
If the timer is in an active state or is shared amongst different threads, the state may not be static, ie it may change immediately after it is fetched. The user is responsible for using the results of this query wisely.

int WUT_sys_adjust long long int  dnsecx  ) 
 

Adjusts all the current entries on the WUT timer que in response to a change in the base time.

Returns:
The number of entries that were adjusted
Parameters:
dnsecx The number of nanoseconds to shift the timer que entries by.
This is a high level system routine used to fix-up the timer entries in response to a (hopefully) one-time resetting of the underlying system clock. The call to this routine will occur when the external time is known. Since this time is potentially well after the time WUT has started, it is necessary to adjust the entries on the WUT timer que to this new base time.
Warning:
There is a gotcha in this and that is that all timer que entries up to this point are assumed to be relative timers. It really does not make any sense to have placed any absolute timers on the que, since, until the external time is known, one has no basis for establishing when an absolute timer will expire.

It is assumed that interrupts are locked during the course of this execution.

int WUT_sys_connect unsigned int  keepalive,
unsigned int  update
 

One time WUT connection routine to start WUT system.

Parameters:
keepalive The timeout period, in nsecs, to run the keepalive timer.
update The update period. If non-zero, the WCT clock will be updated at this rate. If 0, then WCT clock will be update externally.
Returns:
Status
Another one time initialization call. This creates a watchdog timer and starts it.

STATIC int wut_sys_connect WCB wcb  ) 
 

Platform dependent part of the WUT system connection process.

Parameters:
wcb The WUT control block
Returns:
Status

int WUT_sys_init int  count  ) 
 

One time WUT services initialization routine.

Parameters:
count The number of available timer entries
Returns:
Status
Initializes a pool of WakeUp Timer entries. This is a one time system initialization call and establishes a fixed pool of timer entries. Enough timer entries should be configured to handle the maximum possible number of simoultaneosly outstanding requests. Timer entries are fairly cheap (~50 bytes) so one should allocate generously.
Having said that, the prudent programmer will allocate the timer entries he needs at his start-up time and hold on to them. While this is somewhat wasteful of system resources, it beats dealing with trying to allocate a timer entry during the meat of the code, only to come up empty handed. If this stategy is uniformily adopted it should be relatively easy to establish a working number.

int WUT_sys_reset unsigned int  keepalive,
unsigned int  update
 

Resets the keepalive and update times.

Returns:
Status
Parameters:
keepalive The timeout period, in nsecs, to run the keepalive timer. One values is recognized as special
  1. 0, Leave as is
  2. anything else, The new keepalive period
update The new update period (in nanoseconds). Three values are recognized
  1. -1, Remove the updater, the implication is that the caller is assuming responsibility for this function
  2. 0, Leave as is
  3. anything else, the new update time, in nanoseconds
This is used to establish new values for the keepalive and update timers. This is generally used after the initial initialization of the WUT facility, when one learns the absolute wall clock time.

int WUT_sys_shutdown void   ) 
 

Shuts down the WUT routine.

Returns:
Status, 1 indicates WUT was not started, so cannot be shutdown.
This is definitely a dicey function. It makes no check that the WUT que is empty. That is left to the poor user to guarantee.
Warning:
Given the degree of difficulty reclaiming all the resources, this function should probably be regarded as a debugging aid only. It can be used when reloading PBS>
If one does call it from a real piece of code, then the user is obligated to cancel any and all WUT timers. In addition, any timers allocated from the WUT pool should be returned. This last step is not strictly necessary. WUT knows where all its timer entries are so it will free them whether the user does or doesn't. Freeing the timers is just for clean-living.

STATIC int wut_sys_shutdown WCB wcb  ) 
 

Platform specific shutdown procedure.

The decrement counter is set to the maximum expiration, effectively providing enough time to restore the original VxWorks exception handler.

TOV WUT_tov_get const WUT_tmr wut  ) 
 

Gets the current TOV of the WUT timer entry.

Parameters:
wut The WUT timer entry to get the TOV for.
Returns:
The current TOV of the WUT timer entry.
Simply query routine to return the current state of a timer entry.
Warning:
If the timer is in an active state or is shared amongst different threads, the TOV may not be static, ie it may change immediately after it is fetched. The user is responsible for using the results of this query wisely.


Generated on Fri Feb 10 20:25:43 2006 by  doxygen 1.4.4