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

  1 karl  1.9 //%2006////////////////////////////////////////////////////////////////////////
  2 mike  1.1 //
  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 karl  1.9 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12           // EMC Corporation; Symantec Corporation; The Open Group.
 13 mike  1.1 //
 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 karl  1.9 // 
 21 mike  1.1 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 22           // 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           // Author: Mike Brasher, Inova Europe (mike-brasher@austin.rr.com)
 33           //
 34 david.dillard 1.4 // Modified By: David Dillard, Symantec Corp. (david_dillard@symantec.com)
 35                   //
 36 mike          1.1 //%/////////////////////////////////////////////////////////////////////////////
 37                   
 38 mike          1.10 #include "Network.h"
 39                    
 40 mike          1.8  #if defined(PEGASUS_OS_SOLARIS)
 41                    # include <sys/types.h>
 42                    # include <unistd.h>
 43                    #endif
 44                    
 45 mike          1.1  #include "SpinLock.h"
 46 mike          1.2  
 47 gs.keenan     1.6  #if  !defined(PEGASUS_OS_VMS) &&  !defined(PEGASUS_OS_TYPE_WINDOWS)
 48 mike          1.3  # define PEGASUS_SPINLOCK_USE_PTHREADS
 49                    #endif
 50                    
 51 mike          1.2  #ifdef PEGASUS_SPINLOCK_USE_PTHREADS
 52                    # include <pthread.h>
 53                    #else
 54 mike          1.11 # include "Mutex.h"
 55 mike          1.2  #endif
 56 mike          1.1  
 57                    PEGASUS_NAMESPACE_BEGIN
 58                    
 59 kumpf         1.5  SpinLock spinLockPool[PEGASUS_NUM_SHARED_SPIN_LOCKS];
 60                    int spinLockPoolInitialized;
 61 mike          1.1  
 62 mike          1.2  #ifdef PEGASUS_SPINLOCK_USE_PTHREADS
 63                    pthread_mutex_t _spinLockInitMutex = PTHREAD_MUTEX_INITIALIZER;
 64                    #else
 65 mike          1.1  static Mutex _spinLockInitMutex;
 66 mike          1.2  #endif
 67 mike          1.1  
 68 kumpf         1.5  void SpinLockCreatePool()
 69 mike          1.1  {
 70 kumpf         1.5      // There's no need to check spinLockPoolInitialized before locking the
 71                        // mutex, because the caller can check the flag before calling this
 72                        // function.
 73 mike          1.1  
 74 mike          1.2  #ifdef PEGASUS_SPINLOCK_USE_PTHREADS
 75 kumpf         1.5      pthread_mutex_lock(&_spinLockInitMutex);
 76 mike          1.2  #else
 77 mike          1.11     _spinLockInitMutex.lock();
 78 mike          1.2  #endif
 79 mike          1.1  
 80 kumpf         1.5      if (spinLockPoolInitialized == 0)
 81                        {
 82                            for (size_t i = 0; i < PEGASUS_NUM_SHARED_SPIN_LOCKS; i++)
 83                                SpinLockCreate(spinLockPool[i]);
 84                    
 85                            spinLockPoolInitialized = 1;
 86                        }
 87 mike          1.1  
 88 mike          1.2  #ifdef PEGASUS_SPINLOCK_USE_PTHREADS
 89 kumpf         1.5      pthread_mutex_unlock(&_spinLockInitMutex);
 90 mike          1.2  #else
 91 kumpf         1.5      _spinLockInitMutex.unlock();
 92 mike          1.2  #endif
 93 kumpf         1.5  }
 94                    
 95                    #if defined(PEGASUS_SPINLOCK_USE_PTHREADS)
 96                    
 97                    // This function is called prior to forking.  We must obtain a lock
 98                    // on every mutex that the child will inherit.  These will remain locked
 99                    // until they are unlocked (by _unlockSpinLockPool()).  This prevents a
100                    // child process from waiting indefinitely on a mutex that was locked by
101                    // another thread in the parent process during the fork.
102                    
103 mike          1.8  extern "C" void _lockSpinLockPool()
104 kumpf         1.5  {
105                        // Initialize the spinlock pool if not already done.
106                    
107                        if (spinLockPoolInitialized == 0)
108                            SpinLockCreatePool();
109                    
110                        pthread_mutex_lock(&_spinLockInitMutex);
111                    
112                        for (size_t i = 0; i < PEGASUS_NUM_SHARED_SPIN_LOCKS; i++)
113                            SpinLockLock(spinLockPool[i]);
114                    }
115                    
116                    // This function is called after forking.  It unlocks the mutexes that
117                    // were locked by _lockSpinLockPool() before the fork.
118                    
119 mike          1.8  extern "C" void _unlockSpinLockPool()
120 kumpf         1.5  {
121                        pthread_mutex_unlock(&_spinLockInitMutex);
122                    
123                        for (size_t i = 0; i < PEGASUS_NUM_SHARED_SPIN_LOCKS; i++)
124                            SpinLockUnlock(spinLockPool[i]);
125                    }
126                    
127                    class SpinLockInitializer
128                    {
129                    public:
130                        SpinLockInitializer()
131                        {
132 kumpf         1.7  // ATTN: Temporary workaround for Bug 4559
133                    #if defined(PEGASUS_OS_HPUX) || defined(PEGASUS_OS_SOLARIS)
134 kumpf         1.5          pthread_atfork(
135                                _lockSpinLockPool,
136                                _unlockSpinLockPool,
137                                _unlockSpinLockPool);
138 kumpf         1.7  #endif
139 mike          1.1      }
140 kumpf         1.5  };
141                    
142                    static SpinLockInitializer spinLockInitializer;
143                    
144                    #endif /* PEGASUS_SPINLOCK_USE_PTHREADS */
145 mike          1.1  
146                    PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2