reSIProcate/rutil  9694
RecursiveMutex.cxx
Go to the documentation of this file.
00001 #include <cassert>
00002 #include <cerrno>
00003 
00004 #include "rutil/RecursiveMutex.hxx"
00005 
00006 #if defined(WIN32) 
00007 #  include <windows.h>
00008 #  include <winbase.h>
00009 #else
00010 #  include <pthread.h>
00011 #endif
00012 
00013 #if defined(__INTEL_COMPILER)
00014 // !rk! hack, I'm not sure off hand why the Intel compiler can't find this
00015 extern int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind)
00016    __THROW;
00017 #endif
00018 
00019 using resip::RecursiveMutex;
00020 
00021 // !abr! I can't find evidence that the Intel C++ Compiler has any issues
00022 // with the use of recursive mutexes. The need for this exception should
00023 // be re-verified and documented here.
00024 
00025 // .abr. .amr. OS X 10.2 is 1020. Prior to this, OS X did not support
00026 // recursive mutexes. The iPhone Macro will be defined for all iPhone SDK based
00027 // compiles which properly supports recursive mutexes
00028 
00029 #if (defined( __APPLE__ ) && (defined(MAC_OS_X_VERSION_MIN_REQUIRED) && (MAC_OS_X_VERSION_MIN_REQUIRED < 1020) && !defined(TARGET_OS_IPHONE))) || defined (__INTEL_COMPILER)
00030 // !cj! need to write intel mutex stuff 
00031 
00032 #warning "RecursiveMutex is not available on this platform yet."
00033 
00034 namespace resip
00035 {
00036 
00037 RecursiveMutex::RecursiveMutex()
00038 {
00039    assert(0); 
00040 }
00041 
00042 
00043 RecursiveMutex::~RecursiveMutex ()
00044 {
00045    assert(0);
00046 }
00047 
00048 
00049 void
00050 RecursiveMutex::lock()
00051 {
00052    assert(0); 
00053 }
00054 
00055 void
00056 RecursiveMutex::unlock()
00057 {
00058    assert(0); 
00059 }
00060 
00061 }
00062 
00063 #else
00064 
00065 namespace resip
00066 {
00067 
00068 RecursiveMutex::RecursiveMutex()
00069 {
00070 #ifndef WIN32
00071    int rc = pthread_mutexattr_init(&mMutexAttr);
00072  #if defined(__linux__)
00073    pthread_mutexattr_settype(&mMutexAttr, PTHREAD_MUTEX_RECURSIVE_NP);
00074  #else
00075    pthread_mutexattr_settype(&mMutexAttr, PTHREAD_MUTEX_RECURSIVE);
00076  #endif
00077 
00078    rc = pthread_mutex_init(&mId, &mMutexAttr);
00079    assert( rc == 0 );
00080 #else
00081         InitializeCriticalSection(&mId);
00082 #endif
00083 }
00084 
00085 
00086 RecursiveMutex::~RecursiveMutex ()
00087 {
00088 #ifndef WIN32
00089     int  rc = pthread_mutex_destroy(&mId);
00090     assert( rc != EBUSY );  // currently locked 
00091     assert( rc == 0 );
00092     rc = pthread_mutexattr_destroy(&mMutexAttr);
00093 #else
00094         DeleteCriticalSection(&mId);
00095 #endif
00096 }
00097 
00098 
00099 void
00100 RecursiveMutex::lock()
00101 {
00102 #ifndef WIN32
00103     int  rc = pthread_mutex_lock(&mId);
00104     (void)rc;
00105     assert( rc != EINVAL );
00106     assert( rc != EDEADLK );
00107     assert( rc == 0 );
00108 #else
00109         EnterCriticalSection(&mId);
00110 #endif
00111 }
00112 
00113 void
00114 RecursiveMutex::unlock()
00115 {
00116 #ifndef WIN32
00117     int  rc = pthread_mutex_unlock(&mId);
00118     (void)rc;
00119     assert( rc != EINVAL );
00120     assert( rc != EPERM );
00121     assert( rc == 0 );
00122 #else
00123         LeaveCriticalSection(&mId);
00124 #endif
00125 }
00126 
00127 #ifndef WIN32
00128 pthread_mutex_t*
00129 RecursiveMutex::getId() const
00130 {
00131     return ( &mId );
00132 }
00133 #endif
00134 
00135 }
00136 
00137 #endif
00138 
00139 /* ====================================================================
00140  * The Vovida Software License, Version 1.0 
00141  * 
00142  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00143  * 
00144  * Redistribution and use in source and binary forms, with or without
00145  * modification, are permitted provided that the following conditions
00146  * are met:
00147  * 
00148  * 1. Redistributions of source code must retain the above copyright
00149  *    notice, this list of conditions and the following disclaimer.
00150  * 
00151  * 2. Redistributions in binary form must reproduce the above copyright
00152  *    notice, this list of conditions and the following disclaimer in
00153  *    the documentation and/or other materials provided with the
00154  *    distribution.
00155  * 
00156  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00157  *    and "Vovida Open Communication Application Library (VOCAL)" must
00158  *    not be used to endorse or promote products derived from this
00159  *    software without prior written permission. For written
00160  *    permission, please contact vocal@vovida.org.
00161  *
00162  * 4. Products derived from this software may not be called "VOCAL", nor
00163  *    may "VOCAL" appear in their name, without prior written
00164  *    permission of Vovida Networks, Inc.
00165  * 
00166  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00167  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00168  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00169  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00170  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00171  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00172  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00173  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00174  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00175  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00176  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00177  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00178  * DAMAGE.
00179  * 
00180  * ====================================================================
00181  * 
00182  * This software consists of voluntary contributions made by Vovida
00183  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00184  * Inc.  For more information on Vovida Networks, Inc., please see
00185  * <http://www.vovida.org/>.
00186  *
00187  */