GNU Radio 3.3.0 C++ API
cond_init.h
Go to the documentation of this file.
00001 /* --------------------------------------------------------------  */
00002 /* (C)Copyright 2001,2007,                                         */
00003 /* International Business Machines Corporation,                    */
00004 /* Sony Computer Entertainment, Incorporated,                      */
00005 /* Toshiba Corporation,                                            */
00006 /*                                                                 */
00007 /* All Rights Reserved.                                            */
00008 /*                                                                 */
00009 /* Redistribution and use in source and binary forms, with or      */
00010 /* without modification, are permitted provided that the           */
00011 /* following conditions are met:                                   */
00012 /*                                                                 */
00013 /* - Redistributions of source code must retain the above copyright*/
00014 /*   notice, this list of conditions and the following disclaimer. */
00015 /*                                                                 */
00016 /* - Redistributions in binary form must reproduce the above       */
00017 /*   copyright notice, this list of conditions and the following   */
00018 /*   disclaimer in the documentation and/or other materials        */
00019 /*   provided with the distribution.                               */
00020 /*                                                                 */
00021 /* - Neither the name of IBM Corporation nor the names of its      */
00022 /*   contributors may be used to endorse or promote products       */
00023 /*   derived from this software without specific prior written     */
00024 /*   permission.                                                   */
00025 /*                                                                 */
00026 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND          */
00027 /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,     */
00028 /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF        */
00029 /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE        */
00030 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR            */
00031 /* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    */
00032 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT    */
00033 /* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;    */
00034 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)        */
00035 /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN       */
00036 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR    */
00037 /* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  */
00038 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              */
00039 /* --------------------------------------------------------------  */
00040 /* PROLOG END TAG zYx                                              */
00041 #ifndef _SPU_COND_INIT_H_
00042 #define _SPU_COND_INIT_H_
00043 
00044 #include "sync_utils.h"
00045 #include "cond.h"
00046 #include <spu_mfcio.h>
00047 
00048 
00049 /**
00050  * cond_init - initialize condition variable.
00051  * @cond: handle to effective address of condition variable.
00052  *
00053  *
00054  * Conditional Variable - is a synchronization device that allows 
00055  * SPE and PPE threads to suspend execution and relinquish the 
00056  * processors until some predicate on shared data is satisfied. 
00057  * The basic operations on conditions are: signal the condition
00058  * (when the predicate becomes true), and wait for the condition,
00059  * suspending the thread execution until anoter thread signals the
00060  * condition
00061  *
00062  * A condition variable must always be associated with a mutex, to
00063  * avoid the race condition where a thread prepares to wait on a 
00064  * condition variable and another thread signals the condition just
00065  * before the first thread actually waits on it.
00066  *
00067  * cond_init initializes the condition variable cond. 
00068  *
00069  * cond_signal restarts one of the threads that are waiting on the
00070  * condition variable cond. If no threads are waiting on cond, nothing
00071  * happens. If several threads are waiting on cond, exactly one
00072  * is restarted, but it is not specified which
00073  *
00074  * cond_broadcast restarts all the threads that are waiting on the 
00075  * condition variable cond. Nothing happens if no threads are waiting
00076  * on cond
00077  *
00078  * cond_wait atomically unlocks the mutex and waits for the condition
00079  * variable cond to be signaled. The mutex must be lock locked by 
00080  * the calling thread on the entrance to cond_wait. Before returning
00081  * to the calling thread, cond_wait re-acquires mutex. 
00082  *
00083  * Only one thread initializes a condition variable. Usually, the 
00084  * PPE thread initializes a condidtion variable, however, a cond_init
00085  * function is provided here for completeness
00086  *
00087  * Description: Initialize a cond variable to false.
00088  */
00089 static __inline void _cond_init(cond_ea_t cond )
00090 {
00091     char _tmp[256];                                     
00092     char *tmp = (char *) ALIGN(_tmp, 128);              
00093     volatile unsigned short *buf = (volatile unsigned short *) &tmp[0];       
00094     unsigned int size = 128, tagid;                          
00095     unsigned int offset;           
00096     addr64 ea64;
00097     unsigned int oldtmask;
00098     unsigned int tagmask;    
00099 
00100     tagid = mfc_tag_reserve(); 
00101 
00102     tagmask = 1 << (tagid & 31);
00103 
00104     ea64.ull = ALIGN128_EA(cond);
00105     offset = OFFSET128_EA_U16(cond);
00106 
00107     MFC_DMA(buf, ea64, size, tagid & 31, MFC_GET_CMD);
00108     oldtmask = spu_readch(MFC_RdTagMask);
00109     spu_writech(MFC_WrTagMask, tagmask);
00110     spu_writech(MFC_WrTagUpdate, MFC_TAG_UPDATE_ANY);
00111     spu_readch(MFC_RdTagStat);
00112 
00113     /* this is still just one word. since buf is of type
00114      * short, we fit both counts into one word. */
00115     buf[offset] = 0;
00116     buf[offset+1] = 0;
00117     MFC_DMA(buf, ea64, size, (tagid & 31), MFC_PUT_CMD);
00118     spu_writech(MFC_WrTagMask, tagmask);
00119     spu_writech(MFC_WrTagUpdate, MFC_TAG_UPDATE_ANY);
00120     spu_readch(MFC_RdTagStat);
00121     spu_writech(MFC_WrTagMask, oldtmask);
00122     mfc_tag_release (tagid);
00123 }
00124 
00125 
00126 
00127 #endif /* _SPU_COND_INIT_H_ */