]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/kernel/v2_0/include/mutex.hxx
Initial revision
[karo-tx-redboot.git] / packages / kernel / v2_0 / include / mutex.hxx
1 #ifndef CYGONCE_KERNEL_MUTEX_HXX
2 #define CYGONCE_KERNEL_MUTEX_HXX
3
4 //==========================================================================
5 //
6 //      mutex.hxx
7 //
8 //      Mutex class declarations
9 //
10 //==========================================================================
11 //####ECOSGPLCOPYRIGHTBEGIN####
12 // -------------------------------------------
13 // This file is part of eCos, the Embedded Configurable Operating System.
14 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
15 //
16 // eCos is free software; you can redistribute it and/or modify it under
17 // the terms of the GNU General Public License as published by the Free
18 // Software Foundation; either version 2 or (at your option) any later version.
19 //
20 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
21 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23 // for more details.
24 //
25 // You should have received a copy of the GNU General Public License along
26 // with eCos; if not, write to the Free Software Foundation, Inc.,
27 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 //
29 // As a special exception, if other files instantiate templates or use macros
30 // or inline functions from this file, or you compile this file and link it
31 // with other works to produce a work based on this file, this file does not
32 // by itself cause the resulting work to be covered by the GNU General Public
33 // License. However the source code for this file must still be made available
34 // in accordance with section (3) of the GNU General Public License.
35 //
36 // This exception does not invalidate any other reasons why a work based on
37 // this file might be covered by the GNU General Public License.
38 //
39 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
40 // at http://sources.redhat.com/ecos/ecos-license/
41 // -------------------------------------------
42 //####ECOSGPLCOPYRIGHTEND####
43 //==========================================================================
44 //#####DESCRIPTIONBEGIN####
45 //
46 // Author(s):   nickg
47 // Contributors:        nickg
48 // Date:        1997-09-09
49 // Purpose:     Define Mutex class interfaces
50 // Description: The classes defined here provide the APIs for mutexes
51 //              and condition variables.
52 // Usage:       #include <cyg/kernel/mutex.hxx>
53 //              
54 //
55 //####DESCRIPTIONEND####
56 //
57 //==========================================================================
58
59 #include <cyg/kernel/ktypes.h>
60 #include <cyg/infra/cyg_ass.h>         // assertion macros
61
62 #include <cyg/kernel/thread.hxx>
63
64 // -------------------------------------------------------------------------
65 // Mutex.
66
67 class Cyg_Mutex
68 {
69     friend class Cyg_Condition_Variable;
70     
71     cyg_atomic          locked;         // true if locked. This may seem
72                                         // redundant due to "owner" below,
73                                         // but is intentionally present for
74                                         // future SMP support.
75
76     Cyg_Thread          *owner;         // Current locking thread
77
78     Cyg_ThreadQueue     queue;          // Queue of waiting threads
79
80 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC
81
82 public:    
83     enum cyg_protcol
84     {
85         NONE = 0,                       // no inversion protocol
86         INHERIT,                        // priority inheritance protocol
87         CEILING                         // priority ceiling protocol
88     };
89
90 private:    
91     cyg_protcol protocol;               // this mutex's protocol
92     
93 #endif    
94     
95 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING
96
97 private:    
98     cyg_priority        ceiling;        // mutex priority ceiling
99     
100 #endif
101     
102 public:
103
104     CYGDBG_DEFINE_CHECK_THIS
105     
106     Cyg_Mutex();                        // Create in unlocked state
107
108 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC
109     
110     Cyg_Mutex( cyg_protcol protocol );  // Create with defined protocol
111
112 #endif
113     
114     ~Cyg_Mutex();                       // Destructor
115         
116     cyg_bool    lock();                 // lock and/or wait
117
118     cyg_bool    trylock();              // try to lock and return success
119
120     void        unlock();               // unlock
121
122     void        release();              // release all waiting threads
123
124     // Get the current owning thread
125     inline Cyg_Thread *get_owner() { return owner; }
126     
127 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING
128
129     // set ceiling priority for priority ceiling protocol
130     void        set_ceiling( cyg_priority priority );
131
132     cyg_priority get_ceiling(void) { return ceiling; };
133     
134 #endif
135
136 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC
137
138     // set inversion protocol
139     void        set_protocol( cyg_protcol new_protocol );
140 #endif
141     
142 };
143
144 // -------------------------------------------------------------------------
145 // Condition variable.
146
147 class Cyg_Condition_Variable
148 {
149     Cyg_Mutex           *mutex;         // Associated mutex
150
151     Cyg_ThreadQueue     queue;          // Queue of waiting threads
152
153     // Private internal implementation function for wait operations
154     cyg_bool wait_inner( Cyg_Mutex *mutex );
155
156 #ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
157
158     // Private internal implementation function for timed wait operations
159     cyg_bool wait_inner( Cyg_Mutex *mutex, cyg_tick_count timeout );
160
161 #endif
162     
163 public:
164
165     CYGDBG_DEFINE_CHECK_THIS
166
167     Cyg_Condition_Variable();           // simple constructor
168     
169     Cyg_Condition_Variable(
170         Cyg_Mutex &mutex                // linked mutex
171         );
172
173     ~Cyg_Condition_Variable();          // Destructor
174         
175
176     void     signal();                  // Set cond true, wake one thread
177
178     void     broadcast();               // Set cond true, wake all threads
179
180     // Wait for condition to be true
181     inline cyg_bool wait() { return wait_inner( mutex ); }
182     
183 #ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
184
185     // Wait until a signal or timeout expiry
186     inline cyg_bool wait( cyg_tick_count timeout )
187     { return wait_inner( mutex, timeout ); }
188
189 #endif
190
191 #ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_WAIT_MUTEX
192
193     // Wait for condition to be true using the supplied mutex    
194     inline cyg_bool wait( Cyg_Mutex &mx ) { return wait_inner( &mx ); }
195     
196
197 #ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
198
199     // Wait until a signal or timeout expiry, using the supplied mutex
200     inline cyg_bool wait( Cyg_Mutex &mx, cyg_tick_count timeout )
201     { return wait_inner( &mx, timeout ); }        
202     
203 #endif    
204 #endif    
205
206     // Return a pointer to this variables thread queue. Used mainly
207     // for testing whether a thread is on the queue for a particular
208     // cv.
209     inline Cyg_ThreadQueue *get_queue() { return &queue; };
210     
211 };
212
213
214 // -------------------------------------------------------------------------
215
216 #endif // ifndef CYGONCE_KERNEL_MUTEX_HXX
217 // EOF mutex.hxx