GME  13
PosixMutexMgr.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: PosixMutexMgr.cpp 471747 2006-11-06 14:31:56Z amassari $
00020  */
00021 
00022 // on some platforms, THREAD_MUTEX_RECURSIVE is defined only if _GNU_SOURCE is defined
00023 #ifndef _GNU_SOURCE
00024  #define _GNU_SOURCE
00025 #endif
00026 
00027 #include <pthread.h>
00028 
00029 #include <xercesc/util/MutexManagers/PosixMutexMgr.hpp>
00030 #include <xercesc/util/PlatformUtils.hpp>
00031 #include <xercesc/util/RuntimeException.hpp>
00032 #include <xercesc/util/PanicHandler.hpp>
00033 
00034 XERCES_CPP_NAMESPACE_BEGIN
00035 
00036 
00037 //      Wrap up the mutex with XMemory
00038 class PosixMutexWrap : public XMemory {
00039 public:
00040         pthread_mutex_t m;
00041 };
00042 
00043 
00044 PosixMutexMgr::PosixMutexMgr()
00045 {
00046 }
00047 
00048 
00049 PosixMutexMgr::~PosixMutexMgr()
00050 {
00051 }
00052 
00053 
00054 XMLMutexHandle
00055 PosixMutexMgr::create(MemoryManager* const manager)
00056 {
00057     PosixMutexWrap* mutex = new (manager) PosixMutexWrap;
00058     
00059     pthread_mutexattr_t attr;
00060     pthread_mutexattr_init(&attr);
00061     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
00062     
00063     if (pthread_mutex_init(&mutex->m, &attr))
00064         XMLPlatformUtils::panic(PanicHandler::Panic_MutexErr);
00065         
00066     pthread_mutexattr_destroy(&attr);
00067 
00068     return (void*)(mutex);
00069 }
00070 
00071 
00072 void
00073 PosixMutexMgr::destroy(XMLMutexHandle mtx, MemoryManager* const manager)
00074 {
00075         PosixMutexWrap* mutex = (PosixMutexWrap*)(mtx);
00076     if (mutex != NULL)
00077     {
00078         if (pthread_mutex_destroy(&mutex->m))
00079         {
00080             ThrowXMLwithMemMgr(XMLPlatformUtilsException,
00081                      XMLExcepts::Mutex_CouldNotDestroy, manager);
00082         }
00083         delete mutex;
00084     }
00085 }
00086 
00087 
00088 void
00089 PosixMutexMgr::lock(XMLMutexHandle mtx)
00090 {
00091         PosixMutexWrap* mutex = (PosixMutexWrap*)(mtx);
00092     if (mutex != NULL)
00093     {
00094         if (pthread_mutex_lock(&mutex->m))
00095             XMLPlatformUtils::panic(PanicHandler::Panic_MutexErr);
00096     }
00097 }
00098 
00099 
00100 void
00101 PosixMutexMgr::unlock(XMLMutexHandle mtx)
00102 {
00103         PosixMutexWrap* mutex = (PosixMutexWrap*)(mtx);
00104     if (mutex != NULL)
00105     {
00106         if (pthread_mutex_unlock(&mutex->m))
00107             XMLPlatformUtils::panic(PanicHandler::Panic_MutexErr);
00108     }
00109 }
00110 
00111 
00112 XERCES_CPP_NAMESPACE_END
00113