(file) Return to Condition.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 mike  1.2 //%2006////////////////////////////////////////////////////////////////////////
  2           //
  3           // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  4           // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  5           // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  6           // IBM Corp.; EMC Corporation, The Open Group.
  7           // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8           // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9           // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10           // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11           // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12           // EMC Corporation; Symantec Corporation; The Open Group.
 13           //
 14           // Permission is hereby granted, free of charge, to any person obtaining a copy
 15           // of this software and associated documentation files (the "Software"), to
 16           // deal in the Software without restriction, including without limitation the
 17           // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 18           // sell copies of the Software, and to permit persons to whom the Software is
 19           // furnished to do so, subject to the following conditions:
 20           // 
 21           // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 22 mike  1.2 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 23           // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 24           // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 25           // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 26           // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 27           // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 28           // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 29           //
 30           //==============================================================================
 31           //
 32           //%/////////////////////////////////////////////////////////////////////////////
 33           
 34           #include "Condition.h"
 35           #include "PegasusAssert.h"
 36           
 37           PEGASUS_NAMESPACE_BEGIN
 38           
 39           //==============================================================================
 40           //
 41           // PEGASUS_HAVE_PTHREADS
 42           //
 43 mike  1.2 //==============================================================================
 44           
 45           #if defined(PEGASUS_HAVE_PTHREADS)
 46           
 47           Condition::Condition()
 48           {
 49               pthread_cond_init(&_rep.cond, NULL);
 50           }
 51           
 52           Condition::~Condition()
 53           {
 54               pthread_cond_destroy(&_rep.cond);
 55           }
 56           
 57           void Condition::signal()
 58           {
 59               pthread_cond_signal(&_rep.cond);
 60           }
 61           
 62           void Condition::wait(Mutex& lock)
 63           {
 64 mike  1.3.16.1 #if defined(PEGASUS_OS_VXWORKS)
 65                    assert(lock._rep.recursive == 0);
 66                #endif
 67                
 68 mike  1.2          pthread_cond_wait(&_rep.cond, &lock._rep.mutex);
 69                }
 70                
 71                #endif /* PEGASUS_HAVE_PTHREADS */
 72                
 73                //==============================================================================
 74                //
 75                // PEGASUS_HAVE_WINDOWS_THREADS
 76                //
 77                //==============================================================================
 78                
 79                #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
 80                
 81                ConditionWaiter* _get_waiter()
 82                {
 83 kumpf 1.3          // ATTN: since Windows Thread Local Storage does not have a cleanup
 84                    // routine mechanism, the ConditionWaiter object (and its event) will
 85 mike  1.2          // be leaked when the thread exists.
 86                
 87                    static DWORD _waiter_tls = TlsAlloc();
 88                
 89                    // Obtain (or create) the waiter for this thread.
 90                
 91                    ConditionWaiter* waiter = (ConditionWaiter*)TlsGetValue(_waiter_tls);
 92                
 93                    if (waiter == 0)
 94                    {
 95                        waiter = new ConditionWaiter;
 96                        waiter->event = CreateEvent(0, TRUE, FALSE, 0);
 97                        PEGASUS_DEBUG_ASSERT(waiter->event != NULL);
 98                        TlsSetValue(_waiter_tls, waiter);
 99                    }
100                
101                    return waiter;
102                }
103                
104                Condition::Condition()
105                {
106 mike  1.2      }
107                
108                Condition::~Condition()
109                {
110                    // Remove all of the waiters without deleting them (they are owned by
111                    // the thread-local-storage slot defined above and should never be
112                    // deleted.
113                
114                    while (_rep.waiters.size())
115                        _rep.waiters.remove_front();
116                }
117                
118                void Condition::signal()
119                {
120                    // Remove the waiter at the front of the queue.
121                
122                    ConditionWaiter* waiter = _rep.waiters.remove_front();
123                
124                    if (waiter)
125                        SetEvent(waiter->event);
126                }
127 mike  1.2      
128                void Condition::wait(Mutex& mutex)
129                {
130                    // Insert this thread's waiter at end of waiters queue.
131                
132                    ConditionWaiter* waiter = _get_waiter();
133                    _rep.waiters.insert_back(waiter);
134                
135                    // Unlock the mutex.
136                
137                    size_t count = mutex._rep.count;
138                
139                    for (size_t i = 0; i < count; i++)
140                        mutex.unlock();
141                
142                    // Wait for a signal.
143                
144                    DWORD rc = WaitForSingleObject(waiter->event, INFINITE);
145                    PEGASUS_DEBUG_ASSERT(rc == WAIT_OBJECT_0);
146                
147                    // Relock the mutex.
148 mike  1.2      
149                    for (size_t i = 0; i < count; i++)
150                        mutex.lock();
151                
152                    // Reset the event.
153                
154                    ResetEvent(waiter->event);
155                }
156                
157                #endif /* PEGASUS_HAVE_WINDOWS_THREADS */
158                
159                PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2