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