\hypertarget{RWF_8h}{
\section{RWF.h File Reference}
\label{RWF_8h}\index{RWF.h@{RWF.h}}
}
Resource Wait Faility, generic resource allocation and freeing.  


{\tt \#include $<$IPBS/RW\_\-comdefs.h$>$}\par
{\tt \#include $<$IPBS/TOC\_\-comdefs.h$>$}\par
\subsection*{Typedefs}
\begin{CompactItemize}
\item 
\hypertarget{RWF_8h_a20ea6dfceb5e8004203cead8f809a59}{
typedef struct \hyperlink{struct__RWF__ctl}{\_\-RWF\_\-ctl} \hyperlink{RWF_8h_a20ea6dfceb5e8004203cead8f809a59}{RWF\_\-ctl}}
\label{RWF_8h_a20ea6dfceb5e8004203cead8f809a59}

\begin{CompactList}\small\item\em Typedef for struct \hyperlink{struct__RWF__ctl}{\_\-RWF\_\-ctl}, the RWF control handle. \item\end{CompactList}\end{CompactItemize}
\subsection*{Functions}
\begin{CompactItemize}
\item 
int \hyperlink{RWF_8h_f4ddc3cf81abbfc9dcee25e1f16f5884}{RWF\_\-sizeof} (void)
\begin{CompactList}\small\item\em Returns the size, in bytes, of an RWF control handle. \item\end{CompactList}\item 
int \hyperlink{RWF_8h_71367b13bbc16cac29686a44cd56cd1a}{RWF\_\-init} (\hyperlink{struct__RWF__ctl}{RWF\_\-ctl} $\ast$rwf, \hyperlink{RW__type_8h_faa0e5e8eff72730433bbbbe27430c60}{RW\_\-type} type, void $\ast$ctx, \hyperlink{RW__comdefs_8h_9395764c4c2477c596e70423f325c081}{RW\_\-get\_\-cb} get, \hyperlink{RW__comdefs_8h_98799b5134c6dee5e81ff4e4d659c169}{RW\_\-free\_\-cb} free)
\begin{CompactList}\small\item\em Initializes the RWF control structure. \item\end{CompactList}\item 
int \hyperlink{RWF_8h_c80ff1f5cc16427daba2e36c4b8c6567}{RWF\_\-free} (\hyperlink{struct__RWF__ctl}{RWF\_\-ctl} $\ast$rwf, void $\ast$resource, void $\ast$amount)
\begin{CompactList}\small\item\em Frees a resource, potentially waking up a task pending on an \hyperlink{RWF_8c_828012e86a23deb9e2f45854451bc899}{RWF\_\-getW()} or \hyperlink{RWF_8c_76b0e5eadb12bca5b54780f4d10a2462}{RWF\_\-getW\_\-toc()}. \item\end{CompactList}\item 
void $\ast$ \hyperlink{RWF_8h_98aef7ce2d1518bceb017210877d89c9}{RWF\_\-get} (\hyperlink{struct__RWF__ctl}{RWF\_\-ctl} $\ast$rwf, void $\ast$amount)
\begin{CompactList}\small\item\em Attempts to allocate a resource using the provided free routine. Whether the resource is available or not, this routine returns immediately with the return value of the get routine. See {\em \hyperlink{RWF_8c_828012e86a23deb9e2f45854451bc899}{RWF\_\-getW()}\/} and {\em \hyperlink{RWF_8c_76b0e5eadb12bca5b54780f4d10a2462}{RWF\_\-getW\_\-toc()}\/} for versions that implement waiting. \item\end{CompactList}\item 
void $\ast$ \hyperlink{RWF_8h_42c8722e6dfb269ae4d9803b6315f6d3}{RWF\_\-getW} (\hyperlink{struct__RWF__ctl}{RWF\_\-ctl} $\ast$rwf, void $\ast$amount)
\begin{CompactList}\small\item\em Attempts to allocate a resource using the provided get routine. This routine blocks until the get routine returns a non-NULL value. If nowaiting is desired, see {\em \hyperlink{RW_8c_5d831fe5474b69458e9b9eda5d74dd41}{RW\_\-get()}\/}. If a waiting with a timeout is desired, see {\em \hyperlink{RW_8c_ff6f720e3350909e5295296d3496270f}{RW\_\-getW\_\-toc()}\/}. \item\end{CompactList}\item 
void $\ast$ \hyperlink{RWF_8h_ea411e6e114986aa102215ceb62b34e2}{RWF\_\-getW\_\-toc} (\hyperlink{struct__RWF__ctl}{RWF\_\-ctl} $\ast$rwf, void $\ast$amount, const \hyperlink{struct__TOC}{TOC} $\ast$toc)
\begin{CompactList}\small\item\em Attempts to allocate a resource using the provided get routine. This routine blocks until the get routine returns a non-NULL value or until the timeout period expires. If nowaiting is desired, see {\em \hyperlink{RW_8c_5d831fe5474b69458e9b9eda5d74dd41}{RW\_\-get()}\/}. If a waiting with no timeout is desired, see {\em \hyperlink{RW_8c_ff6f720e3350909e5295296d3496270f}{RW\_\-getW\_\-toc()}\/}. \item\end{CompactList}\item 
int \hyperlink{RWF_8h_a5cf1a8251e8d7d4748f5b6a391a4daa}{RWF\_\-destroy} (\hyperlink{struct__RWF__ctl}{RWF\_\-ctl} $\ast$rwf)
\begin{CompactList}\small\item\em Destroys an RWF control handle, returning any resources garnered by the \hyperlink{RWF_8c_25a627c5e8c70e324f3ffbaac87f675f}{RWF\_\-init()} routine. \item\end{CompactList}\end{CompactItemize}


\subsection{Detailed Description}
Resource Wait Faility, generic resource allocation and freeing. 

\begin{Desc}
\item[Author:]JJRussell - \href{mailto:russell@slac.stanford.edu}{\tt russell@slac.stanford.edu}\end{Desc}


\footnotesize\begin{verbatim}

   CVS $Id: RWF.h,v 1.2 2011/03/25 21:15:02 saxton Exp $
\end{verbatim}
\normalsize


\begin{Desc}
\item[SYNOPSIS]These routines allow a user to get and free resources in a interlocked and blind fashion. A control structure is initialized with two call back routine; one 'gets' or allocates the resource and the other frees the resource. These get and free routines need not be thread-safe, RWF does the locking. The caller is then uses \hyperlink{RWF_8c_749d0e472f9926d04797dc68d7e597b2}{RWF\_\-get()} (or one of its waiting variants, \hyperlink{RWF_8c_828012e86a23deb9e2f45854451bc899}{RWF\_\-getW()} or \hyperlink{RWF_8c_76b0e5eadb12bca5b54780f4d10a2462}{RWF\_\-getW\_\-toc()}) to allocate the resource and \hyperlink{RWF_8c_c80ff1f5cc16427daba2e36c4b8c6567}{RWF\_\-free()} to free the resource. The caller, thus knows next to nothing about how the resource has been acquired nor need concern himself with how the resource is freed. This feature makes these routines ideal for passing resources between task threads. Typically a higher priority task allocates the resource and eventually passes this resource to a lower priority task (commonly called lending/borrowing the memory). The lower priority task need only call \hyperlink{RWF_8c_c80ff1f5cc16427daba2e36c4b8c6567}{RWF\_\-free()} to return the resource.\end{Desc}
\begin{Desc}
\item[USAGE EXAMPLE]\end{Desc}


\begin{Code}\begin{verbatim}   Q_head   q;
   int status;
   struct q_blk { Q_node *node; int buf[2]; } blks[2], *blk;
   RWF_ctl *rwf = MBA_alloc (RWF_sizeof ())

   / * Initialize the resource Q and make some block available * /
   Q_init   (&q);
   Q_insert (&q, &blks[0].node);
   Q_insert (&q, &blks[1].node);

   / * Initialize the RWF control block * /
   RWF_init (rwf, RW_K_TYPE_BLOCKING_FIFO_WTO, &q, Q_remove, Q_insert);
   .

   / * Allocate a block * /
   blk    = RWF_getW (rwf, NOT_NEEDED);
   .
   .
   / * Free the block * /
   status = RWF_free (rwf, blk, NOT_NEEDED);
\end{verbatim}
\end{Code}

 

\subsection{Function Documentation}
\hypertarget{RWF_8h_a5cf1a8251e8d7d4748f5b6a391a4daa}{
\index{RWF.h@{RWF.h}!RWF\_\-destroy@{RWF\_\-destroy}}
\index{RWF\_\-destroy@{RWF\_\-destroy}!RWF.h@{RWF.h}}
\subsubsection[{RWF\_\-destroy}]{\setlength{\rightskip}{0pt plus 5cm}int RWF\_\-destroy ({\bf RWF\_\-ctl} $\ast$ {\em rwf})}}
\label{RWF_8h_a5cf1a8251e8d7d4748f5b6a391a4daa}


Destroys an RWF control handle, returning any resources garnered by the \hyperlink{RWF_8c_25a627c5e8c70e324f3ffbaac87f675f}{RWF\_\-init()} routine. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em rwf}]The RWF control handle to destroy. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]Status \end{Desc}


References \_\-RWF\_\-ctl::rw, and RW\_\-destroy().\hypertarget{RWF_8h_c80ff1f5cc16427daba2e36c4b8c6567}{
\index{RWF.h@{RWF.h}!RWF\_\-free@{RWF\_\-free}}
\index{RWF\_\-free@{RWF\_\-free}!RWF.h@{RWF.h}}
\subsubsection[{RWF\_\-free}]{\setlength{\rightskip}{0pt plus 5cm}int RWF\_\-free ({\bf RWF\_\-ctl} $\ast$ {\em rwf}, \/  void $\ast$ {\em resource}, \/  void $\ast$ {\em amount})}}
\label{RWF_8h_c80ff1f5cc16427daba2e36c4b8c6567}


Frees a resource, potentially waking up a task pending on an \hyperlink{RWF_8c_828012e86a23deb9e2f45854451bc899}{RWF\_\-getW()} or \hyperlink{RWF_8c_76b0e5eadb12bca5b54780f4d10a2462}{RWF\_\-getW\_\-toc()}. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em rwf}]The RWF handle \item[{\em resource}]The resource being freed. \item[{\em amount}]Typically the amount being freed. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]The return value of the free routine.\end{Desc}
While the name of the {\em amount\/} parameter is suggestive, it is, in fact, just a void $\ast$, so the free routine may make any use of it that it pleases. 

References \_\-RWF\_\-ctl::ctx, \_\-RWF\_\-ctl::free, \_\-RWF\_\-ctl::rw, and RW\_\-free().\hypertarget{RWF_8h_98aef7ce2d1518bceb017210877d89c9}{
\index{RWF.h@{RWF.h}!RWF\_\-get@{RWF\_\-get}}
\index{RWF\_\-get@{RWF\_\-get}!RWF.h@{RWF.h}}
\subsubsection[{RWF\_\-get}]{\setlength{\rightskip}{0pt plus 5cm}void$\ast$ RWF\_\-get ({\bf RWF\_\-ctl} $\ast$ {\em rwf}, \/  void $\ast$ {\em amount})}}
\label{RWF_8h_98aef7ce2d1518bceb017210877d89c9}


Attempts to allocate a resource using the provided free routine. Whether the resource is available or not, this routine returns immediately with the return value of the get routine. See {\em \hyperlink{RWF_8c_828012e86a23deb9e2f45854451bc899}{RWF\_\-getW()}\/} and {\em \hyperlink{RWF_8c_76b0e5eadb12bca5b54780f4d10a2462}{RWF\_\-getW\_\-toc()}\/} for versions that implement waiting. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em rwf}]The RWF handle \item[{\em amount}]The amount of the resource being requested. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]A pointer to the acquired resource.\end{Desc}
This routine locks access to the resource pool while the get is being performed.

While the name of the {\em amount\/} parameter is suggestive, it is, in fact, just a void $\ast$, so the get routine may make any use of it that it pleases. 

References \_\-RWF\_\-ctl::ctx, \_\-RWF\_\-ctl::get, \_\-RWF\_\-ctl::rw, and RW\_\-get().\hypertarget{RWF_8h_42c8722e6dfb269ae4d9803b6315f6d3}{
\index{RWF.h@{RWF.h}!RWF\_\-getW@{RWF\_\-getW}}
\index{RWF\_\-getW@{RWF\_\-getW}!RWF.h@{RWF.h}}
\subsubsection[{RWF\_\-getW}]{\setlength{\rightskip}{0pt plus 5cm}void$\ast$ RWF\_\-getW ({\bf RWF\_\-ctl} $\ast$ {\em rwf}, \/  void $\ast$ {\em amount})}}
\label{RWF_8h_42c8722e6dfb269ae4d9803b6315f6d3}


Attempts to allocate a resource using the provided get routine. This routine blocks until the get routine returns a non-NULL value. If nowaiting is desired, see {\em \hyperlink{RW_8c_5d831fe5474b69458e9b9eda5d74dd41}{RW\_\-get()}\/}. If a waiting with a timeout is desired, see {\em \hyperlink{RW_8c_ff6f720e3350909e5295296d3496270f}{RW\_\-getW\_\-toc()}\/}. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em rwf}]The RWF handle \item[{\em amount}]The amount of the resource being requested. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]A pointer to the acquired resource.\end{Desc}
This routine locks access to the resource pool while the get is being performed and waits until the resource is available. An equivalent call to \hyperlink{RWF_8c_c80ff1f5cc16427daba2e36c4b8c6567}{RWF\_\-free()} will unblock this routine, and another attempt will be made to allocate the resource. If successful, as indicated by the get routine returning a non-NULL value, the resource will be returned to the caller. If unsuccessful, the routine will block again.

While the name of the {\em amount\/} parameter is suggestive, it is, in fact, just a void $\ast$, so the get routine may make any use of it that it pleases. 

References \_\-RWF\_\-ctl::ctx, \_\-RWF\_\-ctl::get, \_\-RWF\_\-ctl::rw, and RW\_\-getW().\hypertarget{RWF_8h_ea411e6e114986aa102215ceb62b34e2}{
\index{RWF.h@{RWF.h}!RWF\_\-getW\_\-toc@{RWF\_\-getW\_\-toc}}
\index{RWF\_\-getW\_\-toc@{RWF\_\-getW\_\-toc}!RWF.h@{RWF.h}}
\subsubsection[{RWF\_\-getW\_\-toc}]{\setlength{\rightskip}{0pt plus 5cm}void$\ast$ RWF\_\-getW\_\-toc ({\bf RWF\_\-ctl} $\ast$ {\em rwf}, \/  void $\ast$ {\em amount}, \/  const {\bf TOC} $\ast$ {\em toc})}}
\label{RWF_8h_ea411e6e114986aa102215ceb62b34e2}


Attempts to allocate a resource using the provided get routine. This routine blocks until the get routine returns a non-NULL value or until the timeout period expires. If nowaiting is desired, see {\em \hyperlink{RW_8c_5d831fe5474b69458e9b9eda5d74dd41}{RW\_\-get()}\/}. If a waiting with no timeout is desired, see {\em \hyperlink{RW_8c_ff6f720e3350909e5295296d3496270f}{RW\_\-getW\_\-toc()}\/}. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em rwf}]The RWF handle \item[{\em amount}]The amount of the resource being requested. \item[{\em toc}]The timeout control structure. TOC\_\-NOWAIT and TOC\_\-FOREVER may be specified. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]A pointer to the acquired resource.\end{Desc}
This routine locks access to the resource pool while the get is being performed and waits until the resource is available. An equivalent call to \hyperlink{RWF_8c_c80ff1f5cc16427daba2e36c4b8c6567}{RWF\_\-free()} will unblock this routine, and another attempt will be made to allocate the resource. If successful, as indicated by the get routine returning a non-NULL value, the resource will be returned to the caller. If unsuccessful, the routine will block again until the timeout period expires or the resource becomes available.

While the name of the {\em amount\/} parameter is suggestive, it is, in fact, just a void $\ast$, so the get routine may make any use of it that it pleases. 

References \_\-RWF\_\-ctl::ctx, \_\-RWF\_\-ctl::get, \_\-RWF\_\-ctl::rw, and RW\_\-getW\_\-toc().\hypertarget{RWF_8h_71367b13bbc16cac29686a44cd56cd1a}{
\index{RWF.h@{RWF.h}!RWF\_\-init@{RWF\_\-init}}
\index{RWF\_\-init@{RWF\_\-init}!RWF.h@{RWF.h}}
\subsubsection[{RWF\_\-init}]{\setlength{\rightskip}{0pt plus 5cm}int RWF\_\-init ({\bf RWF\_\-ctl} $\ast$ {\em rwf}, \/  {\bf RW\_\-type} {\em type}, \/  void $\ast$ {\em ctx}, \/  {\bf RW\_\-get\_\-cb} {\em get}, \/  {\bf RW\_\-free\_\-cb} {\em free})}}
\label{RWF_8h_71367b13bbc16cac29686a44cd56cd1a}


Initializes the RWF control structure. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em rwf}]The RWF structure to initialize \item[{\em type}]The type of RW to create, see RW\_\-type for details. \item[{\em ctx}]A context parameter passed as the first argument to both the {\em get\/} and {\em free\/} routines. \item[{\em get}]A user provide 'get' routine. The function signature is\end{description}
\end{Desc}


\begin{Code}\begin{verbatim}                void *resource = (*get)(void *ctx, void *amount);
\end{verbatim}
\end{Code}



A successful allocation returns resource as non-NULL. Typically {\em ctx\/} is a context parameter and {\em amount\/} is the amount of the resource being requested.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em free}]User provide callback routine to implement the actual freeing of the resource. The function signature is\end{description}
\end{Desc}


\begin{Code}\begin{verbatim}                   (*free)(void *ctx, void *resource, void *amount);
\end{verbatim}
\end{Code}



where typically the {\em ctx\/} provides context to the free routine, the {\em resource\/} is a pointer to the resource being freed, and {\em amount\/} is the amount of the resource being freed.

Note that while the {\em amount\/} parameter name is suggestive that this represents the amount of a resource gathered, it is, in fact, just a void $\ast$, so the user is free to pass any 32-bit value that he wishes. Again, typically the freer will be handed the resource parameter and the amount parameter and be expected to just pass them on to \hyperlink{RW_8c_20e7595bd7b8a3753e5cfaaca33600bd}{RW\_\-free()}. 

References \_\-RWF\_\-ctl::ctx, \_\-RWF\_\-ctl::free, \_\-RWF\_\-ctl::get, \_\-RWF\_\-ctl::rw, and RW\_\-init().\hypertarget{RWF_8h_f4ddc3cf81abbfc9dcee25e1f16f5884}{
\index{RWF.h@{RWF.h}!RWF\_\-sizeof@{RWF\_\-sizeof}}
\index{RWF\_\-sizeof@{RWF\_\-sizeof}!RWF.h@{RWF.h}}
\subsubsection[{RWF\_\-sizeof}]{\setlength{\rightskip}{0pt plus 5cm}int RWF\_\-sizeof (void)}}
\label{RWF_8h_f4ddc3cf81abbfc9dcee25e1f16f5884}


Returns the size, in bytes, of an RWF control handle. 

\begin{Desc}
\item[Returns:]The size, in bytes, of an RWF control handle \end{Desc}
