xref: /aoo41x/main/sal/osl/unx/mutex.c (revision d5b37d1c)
1647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5647f063dSAndrew Rist  * distributed with this work for additional information
6647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10647f063dSAndrew Rist  *
11647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12647f063dSAndrew Rist  *
13647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14647f063dSAndrew Rist  * software distributed under the License is distributed on an
15647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17647f063dSAndrew Rist  * specific language governing permissions and limitations
18647f063dSAndrew Rist  * under the License.
19647f063dSAndrew Rist  *
20647f063dSAndrew Rist  *************************************************************/
21647f063dSAndrew Rist 
22647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "system.h"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <osl/mutex.h>
27cdf0e10cSrcweir #include <osl/diagnose.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <pthread.h>
30cdf0e10cSrcweir #include <stdlib.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #if defined LINUX /* bad hack */
33*d5b37d1cS #if !defined(__USE_XOPEN2K8)
34cdf0e10cSrcweir int pthread_mutexattr_setkind_np(pthread_mutexattr_t *, int);
35cdf0e10cSrcweir #define pthread_mutexattr_settype pthread_mutexattr_setkind_np
36cdf0e10cSrcweir #define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP
37cdf0e10cSrcweir #endif
38*d5b37d1cS #endif
39cdf0e10cSrcweir 
40cdf0e10cSrcweir /*
41cdf0e10cSrcweir 	Implementation notes:
42cdf0e10cSrcweir 	oslMutex hides a pointer to the oslMutexImpl structure, which
43cdf0e10cSrcweir 	ist needed to manage recursive locks on a mutex.
44cdf0e10cSrcweir 
45cdf0e10cSrcweir */
46cdf0e10cSrcweir 
47cdf0e10cSrcweir typedef struct _oslMutexImpl
48cdf0e10cSrcweir {
49cdf0e10cSrcweir 	pthread_mutex_t	mutex;
50cdf0e10cSrcweir } oslMutexImpl;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 
53cdf0e10cSrcweir /*****************************************************************************/
54cdf0e10cSrcweir /* osl_createMutex */
55cdf0e10cSrcweir /*****************************************************************************/
osl_createMutex()56cdf0e10cSrcweir oslMutex SAL_CALL osl_createMutex()
57cdf0e10cSrcweir {
58cdf0e10cSrcweir 	oslMutexImpl* pMutex = (oslMutexImpl*) malloc(sizeof(oslMutexImpl));
59cdf0e10cSrcweir     pthread_mutexattr_t aMutexAttr;
60cdf0e10cSrcweir     int nRet=0;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 	OSL_ASSERT(pMutex);
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	if ( pMutex == 0 )
65cdf0e10cSrcweir 	{
66cdf0e10cSrcweir 		return 0;
67cdf0e10cSrcweir 	}
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     pthread_mutexattr_init(&aMutexAttr);
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     nRet = pthread_mutexattr_settype(&aMutexAttr, PTHREAD_MUTEX_RECURSIVE);
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     nRet = pthread_mutex_init(&(pMutex->mutex), &aMutexAttr);
74cdf0e10cSrcweir 	if ( nRet != 0 )
75cdf0e10cSrcweir 	{
76cdf0e10cSrcweir 	    OSL_TRACE("osl_createMutex : mutex init failed. Errno: %d; %s\n",
77cdf0e10cSrcweir                   nRet, strerror(nRet));
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 	    free(pMutex);
80cdf0e10cSrcweir 		pMutex = 0;
81cdf0e10cSrcweir 	}
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     pthread_mutexattr_destroy(&aMutexAttr);
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 	return (oslMutex) pMutex;
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir /*****************************************************************************/
89cdf0e10cSrcweir /* osl_destroyMutex */
90cdf0e10cSrcweir /*****************************************************************************/
osl_destroyMutex(oslMutex Mutex)91cdf0e10cSrcweir void SAL_CALL osl_destroyMutex(oslMutex Mutex)
92cdf0e10cSrcweir {
93cdf0e10cSrcweir 	oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     OSL_ASSERT(pMutex);
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 	if ( pMutex != 0 )
98cdf0e10cSrcweir 	{
99cdf0e10cSrcweir         int nRet=0;
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 	    nRet = pthread_mutex_destroy(&(pMutex->mutex));
102cdf0e10cSrcweir         if ( nRet != 0 )
103cdf0e10cSrcweir         {
104cdf0e10cSrcweir             OSL_TRACE("osl_destroyMutex : mutex destroy failed. Errno: %d; %s\n",
105cdf0e10cSrcweir                       nRet, strerror(nRet));
106cdf0e10cSrcweir         }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 		free(pMutex);
109cdf0e10cSrcweir 	}
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     return;
112cdf0e10cSrcweir }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir /*****************************************************************************/
115cdf0e10cSrcweir /* osl_acquireMutex */
116cdf0e10cSrcweir /*****************************************************************************/
osl_acquireMutex(oslMutex Mutex)117cdf0e10cSrcweir sal_Bool SAL_CALL osl_acquireMutex(oslMutex Mutex)
118cdf0e10cSrcweir {
119cdf0e10cSrcweir 	oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     OSL_ASSERT(pMutex);
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 	if ( pMutex != 0 )
124cdf0e10cSrcweir 	{
125cdf0e10cSrcweir         int nRet=0;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         nRet = pthread_mutex_lock(&(pMutex->mutex));
128cdf0e10cSrcweir         if ( nRet != 0 )
129cdf0e10cSrcweir         {
130cdf0e10cSrcweir             OSL_TRACE("osl_acquireMutex : mutex lock failed. Errno: %d; %s\n",
131cdf0e10cSrcweir                       nRet, strerror(nRet));
132cdf0e10cSrcweir 			return sal_False;
133cdf0e10cSrcweir         }
134cdf0e10cSrcweir 		return sal_True;
135cdf0e10cSrcweir 	}
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     /* not initialized */
138cdf0e10cSrcweir     return sal_False;
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir /*****************************************************************************/
142cdf0e10cSrcweir /* osl_tryToAcquireMutex */
143cdf0e10cSrcweir /*****************************************************************************/
osl_tryToAcquireMutex(oslMutex Mutex)144cdf0e10cSrcweir sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutex Mutex)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir 	oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     OSL_ASSERT(pMutex);
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 	if ( pMutex )
151cdf0e10cSrcweir 	{
152cdf0e10cSrcweir 		int nRet = 0;
153cdf0e10cSrcweir         nRet = pthread_mutex_trylock(&(pMutex->mutex));
154cdf0e10cSrcweir         if ( nRet != 0  )
155cdf0e10cSrcweir             return sal_False;
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 		return sal_True;
158cdf0e10cSrcweir 	}
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     /* not initialized */
161cdf0e10cSrcweir     return sal_False;
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir /*****************************************************************************/
165cdf0e10cSrcweir /* osl_releaseMutex */
166cdf0e10cSrcweir /*****************************************************************************/
osl_releaseMutex(oslMutex Mutex)167cdf0e10cSrcweir sal_Bool SAL_CALL osl_releaseMutex(oslMutex Mutex)
168cdf0e10cSrcweir {
169cdf0e10cSrcweir 	oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     OSL_ASSERT(pMutex);
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 	if ( pMutex )
174cdf0e10cSrcweir 	{
175cdf0e10cSrcweir         int nRet=0;
176cdf0e10cSrcweir         nRet = pthread_mutex_unlock(&(pMutex->mutex));
177cdf0e10cSrcweir         if ( nRet != 0 )
178cdf0e10cSrcweir         {
179cdf0e10cSrcweir             OSL_TRACE("osl_releaseMutex : mutex unlock failed. Errno: %d; %s\n",
180cdf0e10cSrcweir                       nRet, strerror(nRet));
181cdf0e10cSrcweir 			return sal_False;
182cdf0e10cSrcweir         }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir         return sal_True;
185cdf0e10cSrcweir 	}
186cdf0e10cSrcweir 
187cdf0e10cSrcweir     /* not initialized */
188cdf0e10cSrcweir     return sal_False;
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir /*****************************************************************************/
192cdf0e10cSrcweir /* osl_getGlobalMutex */
193cdf0e10cSrcweir /*****************************************************************************/
194cdf0e10cSrcweir 
195cdf0e10cSrcweir static oslMutexImpl globalMutexImpl;
196cdf0e10cSrcweir 
globalMutexInitImpl(void)197cdf0e10cSrcweir static void globalMutexInitImpl(void) {
198cdf0e10cSrcweir     pthread_mutexattr_t attr;
199cdf0e10cSrcweir     if (pthread_mutexattr_init(&attr) != 0 ||
200cdf0e10cSrcweir         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) ||
201cdf0e10cSrcweir         pthread_mutex_init(&globalMutexImpl.mutex, &attr) != 0 ||
202cdf0e10cSrcweir         pthread_mutexattr_destroy(&attr) != 0)
203cdf0e10cSrcweir     {
204cdf0e10cSrcweir         abort();
205cdf0e10cSrcweir     }
206cdf0e10cSrcweir }
207cdf0e10cSrcweir 
osl_getGlobalMutex()208cdf0e10cSrcweir oslMutex * SAL_CALL osl_getGlobalMutex()
209cdf0e10cSrcweir {
210cdf0e10cSrcweir 	/* necessary to get a "oslMutex *" */
211cdf0e10cSrcweir 	static oslMutex globalMutex = (oslMutex) &globalMutexImpl;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     static pthread_once_t once = PTHREAD_ONCE_INIT;
214cdf0e10cSrcweir     if (pthread_once(&once, &globalMutexInitImpl) != 0) {
215cdf0e10cSrcweir         abort();
216cdf0e10cSrcweir     }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 	return &globalMutex;
219cdf0e10cSrcweir }
220