1 #ifndef CYGONCE_KERNEL_MUTEX_HXX
2 #define CYGONCE_KERNEL_MUTEX_HXX
4 //==========================================================================
8 // Mutex class declarations
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.
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.
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
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.
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.
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.
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####
47 // Contributors: nickg
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>
55 //####DESCRIPTIONEND####
57 //==========================================================================
59 #include <cyg/kernel/ktypes.h>
60 #include <cyg/infra/cyg_ass.h> // assertion macros
62 #include <cyg/kernel/thread.hxx>
64 // -------------------------------------------------------------------------
69 friend class Cyg_Condition_Variable;
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.
76 Cyg_Thread *owner; // Current locking thread
78 Cyg_ThreadQueue queue; // Queue of waiting threads
80 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC
85 NONE = 0, // no inversion protocol
86 INHERIT, // priority inheritance protocol
87 CEILING // priority ceiling protocol
91 cyg_protcol protocol; // this mutex's protocol
95 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING
98 cyg_priority ceiling; // mutex priority ceiling
104 CYGDBG_DEFINE_CHECK_THIS
106 Cyg_Mutex(); // Create in unlocked state
108 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC
110 Cyg_Mutex( cyg_protcol protocol ); // Create with defined protocol
114 ~Cyg_Mutex(); // Destructor
116 cyg_bool lock(); // lock and/or wait
118 cyg_bool trylock(); // try to lock and return success
120 void unlock(); // unlock
122 void release(); // release all waiting threads
124 // Get the current owning thread
125 inline Cyg_Thread *get_owner() { return owner; }
127 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING
129 // set ceiling priority for priority ceiling protocol
130 void set_ceiling( cyg_priority priority );
132 cyg_priority get_ceiling(void) { return ceiling; };
136 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC
138 // set inversion protocol
139 void set_protocol( cyg_protcol new_protocol );
144 // -------------------------------------------------------------------------
145 // Condition variable.
147 class Cyg_Condition_Variable
149 Cyg_Mutex *mutex; // Associated mutex
151 Cyg_ThreadQueue queue; // Queue of waiting threads
153 // Private internal implementation function for wait operations
154 cyg_bool wait_inner( Cyg_Mutex *mutex );
156 #ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
158 // Private internal implementation function for timed wait operations
159 cyg_bool wait_inner( Cyg_Mutex *mutex, cyg_tick_count timeout );
165 CYGDBG_DEFINE_CHECK_THIS
167 Cyg_Condition_Variable(); // simple constructor
169 Cyg_Condition_Variable(
170 Cyg_Mutex &mutex // linked mutex
173 ~Cyg_Condition_Variable(); // Destructor
176 void signal(); // Set cond true, wake one thread
178 void broadcast(); // Set cond true, wake all threads
180 // Wait for condition to be true
181 inline cyg_bool wait() { return wait_inner( mutex ); }
183 #ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
185 // Wait until a signal or timeout expiry
186 inline cyg_bool wait( cyg_tick_count timeout )
187 { return wait_inner( mutex, timeout ); }
191 #ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_WAIT_MUTEX
193 // Wait for condition to be true using the supplied mutex
194 inline cyg_bool wait( Cyg_Mutex &mx ) { return wait_inner( &mx ); }
197 #ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
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 ); }
206 // Return a pointer to this variables thread queue. Used mainly
207 // for testing whether a thread is on the queue for a particular
209 inline Cyg_ThreadQueue *get_queue() { return &queue; };
214 // -------------------------------------------------------------------------
216 #endif // ifndef CYGONCE_KERNEL_MUTEX_HXX