source: icXML/icXML-devel/src/xercesc/util/MutexManagers/PosixMutexMgr.cpp @ 2722

Last change on this file since 2722 was 2722, checked in by cameron, 6 years ago

Original Xerces files with import mods for icxercesc

File size: 2.7 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/*
19 * $Id: PosixMutexMgr.cpp 471747 2006-11-06 14:31:56Z amassari $
20 */
21
22// on some platforms, THREAD_MUTEX_RECURSIVE is defined only if _GNU_SOURCE is defined
23#ifndef _GNU_SOURCE
24 #define _GNU_SOURCE
25#endif
26
27#include <pthread.h>
28
29#include <xercesc/util/MutexManagers/PosixMutexMgr.hpp>
30#include <icxercesc/util/PlatformUtils.hpp>
31#include <xercesc/util/RuntimeException.hpp>
32#include <xercesc/util/PanicHandler.hpp>
33
34XERCES_CPP_NAMESPACE_BEGIN
35
36
37//      Wrap up the mutex with XMemory
38class PosixMutexWrap : public XMemory {
39public:
40        pthread_mutex_t m;
41};
42
43
44PosixMutexMgr::PosixMutexMgr()
45{
46}
47
48
49PosixMutexMgr::~PosixMutexMgr()
50{
51}
52
53
54XMLMutexHandle
55PosixMutexMgr::create(MemoryManager* const manager)
56{
57    PosixMutexWrap* mutex = new (manager) PosixMutexWrap;
58   
59    pthread_mutexattr_t attr;
60    pthread_mutexattr_init(&attr);
61    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
62   
63    if (pthread_mutex_init(&mutex->m, &attr))
64        XMLPlatformUtils::panic(PanicHandler::Panic_MutexErr);
65       
66    pthread_mutexattr_destroy(&attr);
67
68    return (void*)(mutex);
69}
70
71
72void
73PosixMutexMgr::destroy(XMLMutexHandle mtx, MemoryManager* const manager)
74{
75        PosixMutexWrap* mutex = (PosixMutexWrap*)(mtx);
76    if (mutex != NULL)
77    {
78        if (pthread_mutex_destroy(&mutex->m))
79        {
80            ThrowXMLwithMemMgr(XMLPlatformUtilsException,
81                     XMLExcepts::Mutex_CouldNotDestroy, manager);
82        }
83        delete mutex;
84    }
85}
86
87
88void
89PosixMutexMgr::lock(XMLMutexHandle mtx)
90{
91        PosixMutexWrap* mutex = (PosixMutexWrap*)(mtx);
92    if (mutex != NULL)
93    {
94        if (pthread_mutex_lock(&mutex->m))
95            XMLPlatformUtils::panic(PanicHandler::Panic_MutexErr);
96    }
97}
98
99
100void
101PosixMutexMgr::unlock(XMLMutexHandle mtx)
102{
103        PosixMutexWrap* mutex = (PosixMutexWrap*)(mtx);
104    if (mutex != NULL)
105    {
106        if (pthread_mutex_unlock(&mutex->m))
107            XMLPlatformUtils::panic(PanicHandler::Panic_MutexErr);
108    }
109}
110
111
112XERCES_CPP_NAMESPACE_END
113
Note: See TracBrowser for help on using the repository browser.