Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "system.h"
30 : :
31 : : #include <osl/mutex.h>
32 : : #include <osl/diagnose.h>
33 : :
34 : : #include <pthread.h>
35 : : #include <stdlib.h>
36 : :
37 : : #if defined LINUX /* bad hack */
38 : : int pthread_mutexattr_setkind_np(pthread_mutexattr_t *, int);
39 : : #define pthread_mutexattr_settype pthread_mutexattr_setkind_np
40 : : #define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP
41 : : #endif
42 : :
43 : : typedef struct _oslMutexImpl
44 : : {
45 : : pthread_mutex_t mutex;
46 : : } oslMutexImpl;
47 : :
48 : :
49 : : /*****************************************************************************/
50 : : /* osl_createMutex */
51 : : /*****************************************************************************/
52 : 4711572 : oslMutex SAL_CALL osl_createMutex()
53 : : {
54 : 4711572 : oslMutexImpl* pMutex = (oslMutexImpl*) malloc(sizeof(oslMutexImpl));
55 : : pthread_mutexattr_t aMutexAttr;
56 : 4711572 : int nRet=0;
57 : :
58 : : OSL_ASSERT(pMutex);
59 : :
60 [ - + ]: 4711572 : if ( pMutex == 0 )
61 : : {
62 : 0 : return 0;
63 : : }
64 : :
65 : 4711572 : pthread_mutexattr_init(&aMutexAttr);
66 : :
67 : 4711571 : nRet = pthread_mutexattr_settype(&aMutexAttr, PTHREAD_MUTEX_RECURSIVE);
68 [ + ]: 4711570 : if( nRet == 0 )
69 : 4711571 : nRet = pthread_mutex_init(&(pMutex->mutex), &aMutexAttr);
70 [ - + ]: 4711570 : if ( nRet != 0 )
71 : : {
72 : : OSL_TRACE("osl_createMutex : mutex init/setattr failed. Errno: %d; %s\n",
73 : : nRet, strerror(nRet));
74 : :
75 : 0 : free(pMutex);
76 : 0 : pMutex = 0;
77 : : }
78 : :
79 : 4711570 : pthread_mutexattr_destroy(&aMutexAttr);
80 : :
81 : 4711571 : return (oslMutex) pMutex;
82 : : }
83 : :
84 : : /*****************************************************************************/
85 : : /* osl_destroyMutex */
86 : : /*****************************************************************************/
87 : 4628456 : void SAL_CALL osl_destroyMutex(oslMutex Mutex)
88 : : {
89 : 4628456 : oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
90 : :
91 : : OSL_ASSERT(pMutex);
92 : :
93 [ + - ]: 4628456 : if ( pMutex != 0 )
94 : : {
95 : 4628456 : int nRet=0;
96 : :
97 : 4628456 : nRet = pthread_mutex_destroy(&(pMutex->mutex));
98 : : if ( nRet != 0 )
99 : : {
100 : : OSL_TRACE("osl_destroyMutex : mutex destroy failed. Errno: %d; %s\n",
101 : : nRet, strerror(nRet));
102 : : }
103 : :
104 : 4628458 : free(pMutex);
105 : : }
106 : :
107 : 4628458 : return;
108 : : }
109 : :
110 : : /*****************************************************************************/
111 : : /* osl_acquireMutex */
112 : : /*****************************************************************************/
113 : 182211683 : sal_Bool SAL_CALL osl_acquireMutex(oslMutex Mutex)
114 : : {
115 : 182211683 : oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
116 : :
117 : : OSL_ASSERT(pMutex);
118 : :
119 [ + - ]: 182211683 : if ( pMutex != 0 )
120 : : {
121 : 182211683 : int nRet=0;
122 : :
123 : 182211683 : nRet = pthread_mutex_lock(&(pMutex->mutex));
124 [ - + ]: 182211766 : if ( nRet != 0 )
125 : : {
126 : : OSL_TRACE("osl_acquireMutex : mutex lock failed. Errno: %d; %s\n",
127 : : nRet, strerror(nRet));
128 : 0 : return sal_False;
129 : : }
130 : 182211766 : return sal_True;
131 : : }
132 : :
133 : : /* not initialized */
134 : 182211766 : return sal_False;
135 : : }
136 : :
137 : : /*****************************************************************************/
138 : : /* osl_tryToAcquireMutex */
139 : : /*****************************************************************************/
140 : 44 : sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutex Mutex)
141 : : {
142 : 44 : oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
143 : :
144 : : OSL_ASSERT(pMutex);
145 : :
146 [ + - ]: 44 : if ( pMutex )
147 : : {
148 : 44 : int nRet = 0;
149 : 44 : nRet = pthread_mutex_trylock(&(pMutex->mutex));
150 [ - + ]: 44 : if ( nRet != 0 )
151 : 0 : return sal_False;
152 : :
153 : 44 : return sal_True;
154 : : }
155 : :
156 : : /* not initialized */
157 : 44 : return sal_False;
158 : : }
159 : :
160 : : /*****************************************************************************/
161 : : /* osl_releaseMutex */
162 : : /*****************************************************************************/
163 : 182211940 : sal_Bool SAL_CALL osl_releaseMutex(oslMutex Mutex)
164 : : {
165 : 182211940 : oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
166 : :
167 : : OSL_ASSERT(pMutex);
168 : :
169 [ + - ]: 182211940 : if ( pMutex )
170 : : {
171 : 182211940 : int nRet=0;
172 : 182211940 : nRet = pthread_mutex_unlock(&(pMutex->mutex));
173 [ + + ]: 182211947 : if ( nRet != 0 )
174 : : {
175 : : OSL_TRACE("osl_releaseMutex : mutex unlock failed. Errno: %d; %s\n",
176 : : nRet, strerror(nRet));
177 : 25 : return sal_False;
178 : : }
179 : :
180 : 182211922 : return sal_True;
181 : : }
182 : :
183 : : /* not initialized */
184 : 182211947 : return sal_False;
185 : : }
186 : :
187 : : /*****************************************************************************/
188 : : /* osl_getGlobalMutex */
189 : : /*****************************************************************************/
190 : :
191 : : static oslMutexImpl globalMutexImpl;
192 : :
193 : 1088 : static void globalMutexInitImpl(void) {
194 : : pthread_mutexattr_t attr;
195 [ + - + - ]: 2176 : if (pthread_mutexattr_init(&attr) != 0 ||
196 [ + - ]: 2176 : pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) ||
197 [ - + ]: 2176 : pthread_mutex_init(&globalMutexImpl.mutex, &attr) != 0 ||
198 : 1088 : pthread_mutexattr_destroy(&attr) != 0)
199 : : {
200 : 0 : abort();
201 : : }
202 : 1088 : }
203 : :
204 : 1425747 : oslMutex * SAL_CALL osl_getGlobalMutex()
205 : : {
206 : : /* necessary to get a "oslMutex *" */
207 : : static oslMutex globalMutex = (oslMutex) &globalMutexImpl;
208 : :
209 : : static pthread_once_t once = PTHREAD_ONCE_INIT;
210 [ - + ]: 1425747 : if (pthread_once(&once, &globalMutexInitImpl) != 0) {
211 : 0 : abort();
212 : : }
213 : :
214 : 1425747 : return &globalMutex;
215 : : }
216 : :
217 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|