Imported Upstream version 3.2.2
[debian/gnuradio] / gcell / ibm / sync / spu_source / cond_init.h
1 /* --------------------------------------------------------------  */
2 /* (C)Copyright 2001,2007,                                         */
3 /* International Business Machines Corporation,                    */
4 /* Sony Computer Entertainment, Incorporated,                      */
5 /* Toshiba Corporation,                                            */
6 /*                                                                 */
7 /* All Rights Reserved.                                            */
8 /*                                                                 */
9 /* Redistribution and use in source and binary forms, with or      */
10 /* without modification, are permitted provided that the           */
11 /* following conditions are met:                                   */
12 /*                                                                 */
13 /* - Redistributions of source code must retain the above copyright*/
14 /*   notice, this list of conditions and the following disclaimer. */
15 /*                                                                 */
16 /* - Redistributions in binary form must reproduce the above       */
17 /*   copyright notice, this list of conditions and the following   */
18 /*   disclaimer in the documentation and/or other materials        */
19 /*   provided with the distribution.                               */
20 /*                                                                 */
21 /* - Neither the name of IBM Corporation nor the names of its      */
22 /*   contributors may be used to endorse or promote products       */
23 /*   derived from this software without specific prior written     */
24 /*   permission.                                                   */
25 /*                                                                 */
26 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND          */
27 /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,     */
28 /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF        */
29 /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE        */
30 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR            */
31 /* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    */
32 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT    */
33 /* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;    */
34 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)        */
35 /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN       */
36 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR    */
37 /* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  */
38 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              */
39 /* --------------------------------------------------------------  */
40 /* PROLOG END TAG zYx                                              */
41 #ifndef _SPU_COND_INIT_H_
42 #define _SPU_COND_INIT_H_
43
44 #include "sync_utils.h"
45 #include "cond.h"
46 #include <spu_mfcio.h>
47
48
49 /**
50  * cond_init - initialize condition variable.
51  * @cond: handle to effective address of condition variable.
52  *
53  *
54  * Conditional Variable - is a synchronization device that allows 
55  * SPE and PPE threads to suspend execution and relinquish the 
56  * processors until some predicate on shared data is satisfied. 
57  * The basic operations on conditions are: signal the condition
58  * (when the predicate becomes true), and wait for the condition,
59  * suspending the thread execution until anoter thread signals the
60  * condition
61  *
62  * A condition variable must always be associated with a mutex, to
63  * avoid the race condition where a thread prepares to wait on a 
64  * condition variable and another thread signals the condition just
65  * before the first thread actually waits on it.
66  *
67  * cond_init initializes the condition variable cond. 
68  *
69  * cond_signal restarts one of the threads that are waiting on the
70  * condition variable cond. If no threads are waiting on cond, nothing
71  * happens. If several threads are waiting on cond, exactly one
72  * is restarted, but it is not specified which
73  *
74  * cond_broadcast restarts all the threads that are waiting on the 
75  * condition variable cond. Nothing happens if no threads are waiting
76  * on cond
77  *
78  * cond_wait atomically unlocks the mutex and waits for the condition
79  * variable cond to be signaled. The mutex must be lock locked by 
80  * the calling thread on the entrance to cond_wait. Before returning
81  * to the calling thread, cond_wait re-acquires mutex. 
82  *
83  * Only one thread initializes a condition variable. Usually, the 
84  * PPE thread initializes a condidtion variable, however, a cond_init
85  * function is provided here for completeness
86  *
87  * Description: Initialize a cond variable to false.
88  */
89 static __inline void _cond_init(cond_ea_t cond )
90 {
91     char _tmp[256];                                     
92     char *tmp = (char *) ALIGN(_tmp, 128);              
93     volatile unsigned short *buf = (volatile unsigned short *) &tmp[0];       
94     unsigned int size = 128, tagid;                          
95     unsigned int offset;           
96     addr64 ea64;
97     unsigned int oldtmask;
98     unsigned int tagmask;    
99
100     tagid = mfc_tag_reserve(); 
101
102     tagmask = 1 << (tagid & 31);
103
104     ea64.ull = ALIGN128_EA(cond);
105     offset = OFFSET128_EA_U16(cond);
106
107     MFC_DMA(buf, ea64, size, tagid & 31, MFC_GET_CMD);
108     oldtmask = spu_readch(MFC_RdTagMask);
109     spu_writech(MFC_WrTagMask, tagmask);
110     spu_writech(MFC_WrTagUpdate, MFC_TAG_UPDATE_ANY);
111     spu_readch(MFC_RdTagStat);
112
113     /* this is still just one word. since buf is of type
114      * short, we fit both counts into one word. */
115     buf[offset] = 0;
116     buf[offset+1] = 0;
117     MFC_DMA(buf, ea64, size, (tagid & 31), MFC_PUT_CMD);
118     spu_writech(MFC_WrTagMask, tagmask);
119     spu_writech(MFC_WrTagUpdate, MFC_TAG_UPDATE_ANY);
120     spu_readch(MFC_RdTagStat);
121     spu_writech(MFC_WrTagMask, oldtmask);
122     mfc_tag_release (tagid);
123 }
124
125
126
127 #endif /* _SPU_COND_INIT_H_ */