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