Panda3D

mutexPosixImpl.I

00001 // Filename: mutexPosixImpl.I
00002 // Created by:  drose (10Feb06)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: MutexPosixImpl::Constructor
00018 //       Access: Public
00019 //  Description:
00020 ////////////////////////////////////////////////////////////////////
00021 INLINE MutexPosixImpl::
00022 MutexPosixImpl() {
00023   TAU_PROFILE("MutexPosixImpl::MutexPosixImpl", " ", TAU_USER);
00024   pthread_mutexattr_t attr;
00025   pthread_mutexattr_init(&attr);
00026   // The symbol PTHREAD_MUTEX_DEFAULT isn't always available?
00027   // pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
00028   int result = pthread_mutex_init(&_lock, &attr);
00029   pthread_mutexattr_destroy(&attr);
00030   assert(result == 0);
00031 }
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //     Function: MutexPosixImpl::Destructor
00035 //       Access: Public
00036 //  Description:
00037 ////////////////////////////////////////////////////////////////////
00038 INLINE MutexPosixImpl::
00039 ~MutexPosixImpl() {
00040   TAU_PROFILE("MutexPosixImpl::~MutexPosixImpl", " ", TAU_USER);
00041   int result = pthread_mutex_destroy(&_lock);
00042   assert(result == 0);
00043 }
00044 
00045 ////////////////////////////////////////////////////////////////////
00046 //     Function: MutexPosixImpl::acquire
00047 //       Access: Public
00048 //  Description: 
00049 ////////////////////////////////////////////////////////////////////
00050 INLINE void MutexPosixImpl::
00051 acquire() {
00052   TAU_PROFILE("void MutexPosixImpl::acquire", " ", TAU_USER);
00053   int result = pthread_mutex_lock(&_lock);
00054   assert(result == 0);
00055 }
00056 
00057 ////////////////////////////////////////////////////////////////////
00058 //     Function: MutexPosixImpl::try_acquire
00059 //       Access: Public
00060 //  Description: 
00061 ////////////////////////////////////////////////////////////////////
00062 INLINE bool MutexPosixImpl::
00063 try_acquire() {
00064   TAU_PROFILE("bool MutexPosixImpl::try_acquire", " ", TAU_USER);
00065   int result = pthread_mutex_trylock(&_lock);
00066   assert(result == 0 || result == EBUSY);
00067   return (result == 0);
00068 }
00069 
00070 ////////////////////////////////////////////////////////////////////
00071 //     Function: MutexPosixImpl::release
00072 //       Access: Public
00073 //  Description: 
00074 ////////////////////////////////////////////////////////////////////
00075 INLINE void MutexPosixImpl::
00076 release() {
00077   TAU_PROFILE("void MutexPosixImpl::release", " ", TAU_USER);
00078   int result = pthread_mutex_unlock(&_lock);
00079   assert(result == 0);
00080 }
00081 
00082 ////////////////////////////////////////////////////////////////////
00083 //     Function: MutexPosixImpl::get_posix_lock
00084 //       Access: Public
00085 //  Description: Returns the underlying Posix lock handle.
00086 ////////////////////////////////////////////////////////////////////
00087 INLINE pthread_mutex_t *MutexPosixImpl::
00088 get_posix_lock() {
00089   return &_lock;
00090 }
00091 
00092 ////////////////////////////////////////////////////////////////////
00093 //     Function: ReMutexPosixImpl::Constructor
00094 //       Access: Public
00095 //  Description:
00096 ////////////////////////////////////////////////////////////////////
00097 INLINE ReMutexPosixImpl::
00098 ReMutexPosixImpl() {
00099   TAU_PROFILE("ReMutexPosixImpl::ReMutexPosixImpl", " ", TAU_USER);
00100   pthread_mutexattr_t attr;
00101   pthread_mutexattr_init(&attr);
00102   pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
00103   int result = pthread_mutex_init(&_lock, &attr);
00104   pthread_mutexattr_destroy(&attr);
00105   assert(result == 0);
00106 }
00107 
00108 ////////////////////////////////////////////////////////////////////
00109 //     Function: ReMutexPosixImpl::Destructor
00110 //       Access: Public
00111 //  Description:
00112 ////////////////////////////////////////////////////////////////////
00113 INLINE ReMutexPosixImpl::
00114 ~ReMutexPosixImpl() {
00115   TAU_PROFILE("ReMutexPosixImpl::~ReMutexPosixImpl", " ", TAU_USER);
00116   int result = pthread_mutex_destroy(&_lock);
00117   assert(result == 0);
00118 }
00119 
00120 ////////////////////////////////////////////////////////////////////
00121 //     Function: ReMutexPosixImpl::acquire
00122 //       Access: Public
00123 //  Description: 
00124 ////////////////////////////////////////////////////////////////////
00125 INLINE void ReMutexPosixImpl::
00126 acquire() {
00127   TAU_PROFILE("void ReMutexPosixImpl::acquire", " ", TAU_USER);
00128   int result = pthread_mutex_lock(&_lock);
00129   assert(result == 0);
00130 }
00131 
00132 ////////////////////////////////////////////////////////////////////
00133 //     Function: ReMutexPosixImpl::try_acquire
00134 //       Access: Public
00135 //  Description: 
00136 ////////////////////////////////////////////////////////////////////
00137 INLINE bool ReMutexPosixImpl::
00138 try_acquire() {
00139   TAU_PROFILE("bool ReMutexPosixImpl::try_acquire", " ", TAU_USER);
00140   int result = pthread_mutex_trylock(&_lock);
00141   assert(result == 0 || result == EBUSY);
00142   return (result == 0);
00143 }
00144 
00145 ////////////////////////////////////////////////////////////////////
00146 //     Function: ReMutexPosixImpl::release
00147 //       Access: Public
00148 //  Description: 
00149 ////////////////////////////////////////////////////////////////////
00150 INLINE void ReMutexPosixImpl::
00151 release() {
00152   TAU_PROFILE("void ReMutexPosixImpl::release", " ", TAU_USER);
00153   int result = pthread_mutex_unlock(&_lock);
00154   assert(result == 0);
00155 }
00156 
00157 ////////////////////////////////////////////////////////////////////
00158 //     Function: ReMutexPosixImpl::get_posix_lock
00159 //       Access: Public
00160 //  Description: Returns the underlying Posix lock handle.
00161 ////////////////////////////////////////////////////////////////////
00162 INLINE pthread_mutex_t *ReMutexPosixImpl::
00163 get_posix_lock() {
00164   return &_lock;
00165 }
 All Classes Functions Variables Enumerations