| 1 | // **********************************************************************
 | 
|---|
| 2 | //
 | 
|---|
| 3 | // Copyright (c) 2000
 | 
|---|
| 4 | // Object Oriented Concepts, Inc.
 | 
|---|
| 5 | // Billerica, MA, USA
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // All Rights Reserved
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // **********************************************************************
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #include <JTC/Types.h>
 | 
|---|
| 12 | #include <JTC/Exception.h>
 | 
|---|
| 13 | #include <JTC/Syscall.h>
 | 
|---|
| 14 | #include <JTC/Mutex.h>
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <stdlib.h>
 | 
|---|
| 17 | #include <assert.h>
 | 
|---|
| 18 | #include <errno.h>
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #ifdef HAVE_STD_IOSTREAM
 | 
|---|
| 21 | using namespace std;
 | 
|---|
| 22 | #endif
 | 
|---|
| 23 | 
 | 
|---|
| 24 | // ----------------------------------------------------------------------
 | 
|---|
| 25 | // JTCRecursiveMutex private member implementation
 | 
|---|
| 26 | // ----------------------------------------------------------------------
 | 
|---|
| 27 | 
 | 
|---|
| 28 | void
 | 
|---|
| 29 | JTCRecursiveMutex::lockI(int count)
 | 
|---|
| 30 | {
 | 
|---|
| 31 |     //
 | 
|---|
| 32 |     // This flag is set to true once the mutex has been obtained.
 | 
|---|
| 33 |     //
 | 
|---|
| 34 |     bool ob = false;
 | 
|---|
| 35 |     while(!ob)
 | 
|---|
| 36 |     {
 | 
|---|
| 37 |         //
 | 
|---|
| 38 |         // Lock the internal mutex.
 | 
|---|
| 39 |         //
 | 
|---|
| 40 |         internal_.lock();
 | 
|---|
| 41 | 
 | 
|---|
| 42 |         //
 | 
|---|
| 43 |         // count_ represents the number of times the mutex has been
 | 
|---|
| 44 |         // aquired.  If count_ is zero then the mutex is not yet
 | 
|---|
| 45 |         // aquired.  Note that two mutexes cannot be in here at once,
 | 
|---|
| 46 |         // since this CS is protected by the mutex internal_.
 | 
|---|
| 47 |         //
 | 
|---|
| 48 |         if(!count_)
 | 
|---|
| 49 |         {
 | 
|---|
| 50 |             //
 | 
|---|
| 51 |             // We're attempting to aquire the mutex count more times.
 | 
|---|
| 52 |             //
 | 
|---|
| 53 |             count_ = count;
 | 
|---|
| 54 |             owner_ = JTCThreadId::self();
 | 
|---|
| 55 |             ob = true;
 | 
|---|
| 56 | 
 | 
|---|
| 57 |             //
 | 
|---|
| 58 |             // Acquire the mutexes critical section.
 | 
|---|
| 59 |             //
 | 
|---|
| 60 |             try
 | 
|---|
| 61 |             {
 | 
|---|
| 62 | #if defined(HAVE_POSIX_THREADS) || defined(HAVE_DCE_THREADS)
 | 
|---|
| 63 |                 JTC_SYSCALL_1(pthread_mutex_lock, &crit_, != 0)
 | 
|---|
| 64 | #endif
 | 
|---|
| 65 | #if defined(HAVE_WIN32_THREADS)
 | 
|---|
| 66 |                 EnterCriticalSection(&crit_);
 | 
|---|
| 67 | #endif
 | 
|---|
| 68 |             }
 | 
|---|
| 69 |             catch(...)
 | 
|---|
| 70 |             {
 | 
|---|
| 71 |                 try
 | 
|---|
| 72 |                 {
 | 
|---|
| 73 |                     internal_.unlock();
 | 
|---|
| 74 |                 }
 | 
|---|
| 75 |                 catch(...)
 | 
|---|
| 76 |                 {
 | 
|---|
| 77 |                 }
 | 
|---|
| 78 |                 throw;
 | 
|---|
| 79 |             }
 | 
|---|
| 80 |         }
 | 
|---|
| 81 |         //
 | 
|---|
| 82 |         // Otherwise count_ is non-zero.  This means that the mutex is
 | 
|---|
| 83 |         // currently aquired.  There are two cases here:
 | 
|---|
| 84 |         //
 | 
|---|
| 85 |         // - The mutex is owned by the caller. Simply increment the
 | 
|---|
| 86 |         //   count in this case.
 | 
|---|
| 87 |         //
 | 
|---|
| 88 |         // - The mutex is owned by a thread other than the caller. In
 | 
|---|
| 89 |         //   this case we attempt to aquire the mutexes critical
 | 
|---|
| 90 |         //   section. This CS is unlocked once mutex is released count_
 | 
|---|
| 91 |         //   times.
 | 
|---|
| 92 |         //
 | 
|---|
| 93 |         else if(owner_ == JTCThreadId::self())
 | 
|---|
| 94 |         {
 | 
|---|
| 95 |             count_ += count;
 | 
|---|
| 96 |             ob = true;
 | 
|---|
| 97 |         }
 | 
|---|
| 98 |         internal_.unlock();
 | 
|---|
| 99 | 
 | 
|---|
| 100 |         //
 | 
|---|
| 101 |         // If we haven't yet obtained the mutex lock the critical
 | 
|---|
| 102 |         // section mutex.  Immediately unlock the CS, then attempt to
 | 
|---|
| 103 |         // re-obtain.
 | 
|---|
| 104 |         //
 | 
|---|
| 105 |         if(!ob)
 | 
|---|
| 106 |         {
 | 
|---|
| 107 | #if defined(HAVE_POSIX_THREADS) || defined(HAVE_DCE_THREADS)
 | 
|---|
| 108 |             JTC_SYSCALL_1(pthread_mutex_lock, &crit_, != 0)
 | 
|---|
| 109 |             pthread_mutex_unlock(&crit_);
 | 
|---|
| 110 | #endif
 | 
|---|
| 111 | #if defined(HAVE_WIN32_THREADS)
 | 
|---|
| 112 |             EnterCriticalSection(&crit_);
 | 
|---|
| 113 |             LeaveCriticalSection(&crit_);
 | 
|---|
| 114 | #endif
 | 
|---|
| 115 |         }
 | 
|---|
| 116 |     }
 | 
|---|
| 117 | }
 | 
|---|
| 118 | 
 | 
|---|
| 119 | void
 | 
|---|
| 120 | JTCRecursiveMutex::unlockI()
 | 
|---|
| 121 | {
 | 
|---|
| 122 |     //
 | 
|---|
| 123 |     // Aquire the internal mutex.
 | 
|---|
| 124 |     //
 | 
|---|
| 125 |     internal_.lock();
 | 
|---|
| 126 | 
 | 
|---|
| 127 |     //
 | 
|---|
| 128 |     // If count_ decrements to zero then unlock the mutexes CS. The
 | 
|---|
| 129 |     // mutex is no longer owned by a thread.
 | 
|---|
| 130 |     //
 | 
|---|
| 131 |     if(--count_ == 0)
 | 
|---|
| 132 |     {
 | 
|---|
| 133 |         owner_ = JTCThreadId();
 | 
|---|
| 134 | 
 | 
|---|
| 135 | #if defined(HAVE_POSIX_THREADS) || defined(HAVE_DCE_THREADS)
 | 
|---|
| 136 |         pthread_mutex_unlock(&crit_);
 | 
|---|
| 137 | #endif
 | 
|---|
| 138 | #if defined(HAVE_WIN32_THREADS)
 | 
|---|
| 139 |         LeaveCriticalSection(&crit_);
 | 
|---|
| 140 | #endif
 | 
|---|
| 141 |     }
 | 
|---|
| 142 | 
 | 
|---|
| 143 |     internal_.unlock();
 | 
|---|
| 144 | }
 | 
|---|
| 145 | 
 | 
|---|
| 146 | //
 | 
|---|
| 147 | // Lock the mutex count times.
 | 
|---|
| 148 | //
 | 
|---|
| 149 | void
 | 
|---|
| 150 | JTCRecursiveMutex::lock(int count) const
 | 
|---|
| 151 | {
 | 
|---|
| 152 |     //
 | 
|---|
| 153 |     // Work around lack of mutable.
 | 
|---|
| 154 |     //
 | 
|---|
| 155 |     ((JTCRecursiveMutex*)this) -> lockI(count);
 | 
|---|
| 156 | }
 | 
|---|
| 157 | 
 | 
|---|
| 158 | // ----------------------------------------------------------------------
 | 
|---|
| 159 | // JTCRecursiveMutex constructor and destructor
 | 
|---|
| 160 | // ----------------------------------------------------------------------
 | 
|---|
| 161 | 
 | 
|---|
| 162 | JTCRecursiveMutex::JTCRecursiveMutex()
 | 
|---|
| 163 |         : count_(0), owner_(JTCThreadId())
 | 
|---|
| 164 | {
 | 
|---|
| 165 | #if defined(HAVE_POSIX_THREADS)
 | 
|---|
| 166 |     JTC_SYSCALL_2(pthread_mutex_init, &crit_, 0, != 0)
 | 
|---|
| 167 | #endif
 | 
|---|
| 168 | #if defined(HAVE_DCE_THREADS)
 | 
|---|
| 169 |     JTC_SYSCALL_2(pthread_mutex_init, &crit_, pthread_mutexattr_default, != 0)
 | 
|---|
| 170 | #endif
 | 
|---|
| 171 | #if defined(HAVE_WIN32_THREADS)
 | 
|---|
| 172 |     InitializeCriticalSection(&crit_);
 | 
|---|
| 173 | #endif
 | 
|---|
| 174 | }
 | 
|---|
| 175 | 
 | 
|---|
| 176 | JTCRecursiveMutex::~JTCRecursiveMutex()
 | 
|---|
| 177 | {
 | 
|---|
| 178 | #if defined(HAVE_POSIX_THREADS) || defined(HAVE_DCE_THREADS)
 | 
|---|
| 179 |     pthread_mutex_destroy(&crit_);
 | 
|---|
| 180 | #endif
 | 
|---|
| 181 | #if defined(HAVE_WIN32_THREADS)
 | 
|---|
| 182 |     DeleteCriticalSection(&crit_);
 | 
|---|
| 183 | #endif
 | 
|---|
| 184 | }
 | 
|---|
| 185 | 
 | 
|---|
| 186 | // ----------------------------------------------------------------------
 | 
|---|
| 187 | // JTCRecursiveMutex public member implementation
 | 
|---|
| 188 | // ----------------------------------------------------------------------
 | 
|---|
| 189 | 
 | 
|---|
| 190 | //
 | 
|---|
| 191 | // Lock the mutex.
 | 
|---|
| 192 | //
 | 
|---|
| 193 | void
 | 
|---|
| 194 | JTCRecursiveMutex::lock() const
 | 
|---|
| 195 | {
 | 
|---|
| 196 |     //
 | 
|---|
| 197 |     // Work around lack of mutable.
 | 
|---|
| 198 |     //
 | 
|---|
| 199 |     ((JTCRecursiveMutex*)this) -> lockI(1);
 | 
|---|
| 200 | }
 | 
|---|
| 201 | 
 | 
|---|
| 202 | //
 | 
|---|
| 203 | // Unlock the mutex.
 | 
|---|
| 204 | //
 | 
|---|
| 205 | void
 | 
|---|
| 206 | JTCRecursiveMutex::unlock() const
 | 
|---|
| 207 | {
 | 
|---|
| 208 |     //
 | 
|---|
| 209 |     // Work around lack of mutable.
 | 
|---|
| 210 |     //
 | 
|---|
| 211 |     ((JTCRecursiveMutex*)this) -> unlockI();
 | 
|---|
| 212 | }
 | 
|---|
| 213 | 
 | 
|---|
| 214 | //
 | 
|---|
| 215 | // Return the ID of the owning thread. If the mutex isn't locked then
 | 
|---|
| 216 | // return nullThreadId.
 | 
|---|
| 217 | //
 | 
|---|
| 218 | JTCThreadId
 | 
|---|
| 219 | JTCRecursiveMutex::_JTC_getId() const
 | 
|---|
| 220 | {
 | 
|---|
| 221 |     JTCRecursiveMutex* This = (JTCRecursiveMutex*)this;
 | 
|---|
| 222 |     //
 | 
|---|
| 223 |     // Aquire the internal mutex.
 | 
|---|
| 224 |     //
 | 
|---|
| 225 |     This -> internal_.lock();
 | 
|---|
| 226 |     JTCThreadId id;
 | 
|---|
| 227 |     if(count_ > 0)
 | 
|---|
| 228 |         id = owner_;
 | 
|---|
| 229 |     This -> internal_.unlock();
 | 
|---|
| 230 |     
 | 
|---|
| 231 |     return id;
 | 
|---|
| 232 | }
 | 
|---|
| 233 | 
 | 
|---|
| 234 | 
 | 
|---|