Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #if defined LINUX
21 : // to define __USE_UNIX98, via _XOPEN_SOURCE, enabling pthread_mutexattr_settype
22 : #ifndef _GNU_SOURCE
23 : #define _GNU_SOURCE 1
24 : #endif
25 : #endif
26 : #include "system.hxx"
27 :
28 : #include <sal/log.hxx>
29 : #include <osl/mutex.h>
30 :
31 : #include <pthread.h>
32 : #include <stdlib.h>
33 :
34 :
35 : typedef struct _oslMutexImpl
36 : {
37 : pthread_mutex_t mutex;
38 : } oslMutexImpl;
39 :
40 5656393 : oslMutex SAL_CALL osl_createMutex()
41 : {
42 5656393 : oslMutexImpl* pMutex = static_cast<oslMutexImpl*>(malloc(sizeof(oslMutexImpl)));
43 : pthread_mutexattr_t aMutexAttr;
44 5656393 : int nRet=0;
45 :
46 : SAL_WARN_IF(!pMutex, "sal.osl.mutex", "null pMutex");
47 :
48 5656393 : if ( pMutex == 0 )
49 : {
50 0 : return 0;
51 : }
52 :
53 5656393 : pthread_mutexattr_init(&aMutexAttr);
54 :
55 5656393 : nRet = pthread_mutexattr_settype(&aMutexAttr, PTHREAD_MUTEX_RECURSIVE);
56 5656393 : if( nRet == 0 )
57 5656393 : nRet = pthread_mutex_init(&(pMutex->mutex), &aMutexAttr);
58 5656392 : if ( nRet != 0 )
59 : {
60 : SAL_WARN("sal.osl.mutex", "pthread_muxex_init failed: " << strerror(nRet));
61 :
62 0 : free(pMutex);
63 0 : pMutex = 0;
64 : }
65 :
66 5656392 : pthread_mutexattr_destroy(&aMutexAttr);
67 :
68 5656392 : return pMutex;
69 : }
70 :
71 5503953 : void SAL_CALL osl_destroyMutex(oslMutexImpl *pMutex)
72 : {
73 : SAL_WARN_IF(!pMutex, "sal.osl.mutex", "null pMutex");
74 :
75 5503953 : if ( pMutex != 0 )
76 : {
77 5503953 : int nRet=0;
78 :
79 5503953 : nRet = pthread_mutex_destroy(&(pMutex->mutex));
80 5503954 : if ( nRet != 0 )
81 : {
82 : SAL_WARN("sal.osl.mutex", "pthread_mutex_destroy failed: " << strerror(nRet));
83 : }
84 :
85 5503954 : free(pMutex);
86 : }
87 :
88 5503954 : return;
89 : }
90 :
91 13828444517 : sal_Bool SAL_CALL osl_acquireMutex(oslMutexImpl *pMutex)
92 : {
93 : SAL_WARN_IF(!pMutex, "sal.osl.mutex", "null pMutex");
94 :
95 13828444517 : if ( pMutex != 0 )
96 : {
97 13828444517 : int nRet=0;
98 :
99 13828444517 : nRet = pthread_mutex_lock(&(pMutex->mutex));
100 13828444213 : if ( nRet != 0 )
101 : {
102 : SAL_WARN("sal.osl.mutex", "pthread_mutex_lock failed: " << strerror(nRet));
103 0 : return sal_False;
104 : }
105 13828444213 : return sal_True;
106 : }
107 :
108 : /* not initialized */
109 0 : return sal_False;
110 : }
111 :
112 49 : sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutexImpl *pMutex)
113 : {
114 49 : bool result = false;
115 :
116 : SAL_WARN_IF(!pMutex, "sal.osl.mutex", "null pMutex");
117 :
118 49 : if ( pMutex )
119 : {
120 49 : int nRet = 0;
121 49 : nRet = pthread_mutex_trylock(&(pMutex->mutex));
122 49 : if ( nRet == 0 )
123 49 : result = true;
124 : }
125 :
126 49 : return result;
127 : }
128 :
129 13828442785 : sal_Bool SAL_CALL osl_releaseMutex(oslMutexImpl *pMutex)
130 : {
131 : SAL_WARN_IF(!pMutex, "sal.osl.mutex", "null pMutex");
132 :
133 13828442785 : if ( pMutex )
134 : {
135 13828442785 : int nRet=0;
136 13828442785 : nRet = pthread_mutex_unlock(&(pMutex->mutex));
137 13828448935 : if ( nRet != 0 )
138 : {
139 : SAL_WARN("sal.osl.mutex", "pthread_mutex_unlock failed: " << strerror(nRet));
140 5 : return sal_False;
141 : }
142 :
143 13828448930 : return sal_True;
144 : }
145 :
146 : /* not initialized */
147 0 : return sal_False;
148 : }
149 :
150 : static oslMutexImpl globalMutexImpl;
151 :
152 1045 : static void globalMutexInitImpl() {
153 : pthread_mutexattr_t attr;
154 3135 : if (pthread_mutexattr_init(&attr) != 0 ||
155 2090 : pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) ||
156 3135 : pthread_mutex_init(&globalMutexImpl.mutex, &attr) != 0 ||
157 1045 : pthread_mutexattr_destroy(&attr) != 0)
158 : {
159 0 : abort();
160 : }
161 1045 : }
162 :
163 223503738 : oslMutex * SAL_CALL osl_getGlobalMutex()
164 : {
165 : /* necessary to get a "oslMutex *" */
166 : static oslMutex globalMutex = &globalMutexImpl;
167 :
168 : static pthread_once_t once = PTHREAD_ONCE_INIT;
169 223503738 : if (pthread_once(&once, &globalMutexInitImpl) != 0) {
170 0 : abort();
171 : }
172 :
173 223503736 : return &globalMutex;
174 : }
175 :
176 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|