1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 // to define __USE_UNIX98, via _XOPEN_SOURCE, enabling pthread_mutexattr_settype
27 #include "unixerrnostring.hxx"
29 #include <sal/log.hxx>
30 #include <osl/mutex.h>
36 typedef struct _oslMutexImpl
38 pthread_mutex_t mutex
;
41 oslMutex SAL_CALL
osl_createMutex()
43 oslMutexImpl
* pMutex
= static_cast<oslMutexImpl
*>(malloc(sizeof(oslMutexImpl
)));
44 pthread_mutexattr_t aMutexAttr
;
47 SAL_WARN_IF(!pMutex
, "sal.osl.mutex", "null pMutex");
49 if ( pMutex
== nullptr )
54 pthread_mutexattr_init(&aMutexAttr
);
56 nRet
= pthread_mutexattr_settype(&aMutexAttr
, PTHREAD_MUTEX_RECURSIVE
);
58 nRet
= pthread_mutex_init(&(pMutex
->mutex
), &aMutexAttr
);
61 SAL_WARN("sal.osl.mutex", "pthread_muxex_init failed: " << UnixErrnoString(nRet
));
67 pthread_mutexattr_destroy(&aMutexAttr
);
72 void SAL_CALL
osl_destroyMutex(oslMutex pMutex
)
74 SAL_WARN_IF(!pMutex
, "sal.osl.mutex", "null pMutex");
76 if ( pMutex
!= nullptr )
78 int nRet
= pthread_mutex_destroy(&(pMutex
->mutex
));
81 SAL_WARN("sal.osl.mutex", "pthread_mutex_destroy failed: " << UnixErrnoString(nRet
));
89 extern void __coverity_recursive_lock_acquire__(void*);
90 extern void __coverity_recursive_lock_release__(void*);
91 extern void __coverity_assert_locked__(void*);
94 sal_Bool SAL_CALL
osl_acquireMutex(oslMutex pMutex
)
96 SAL_WARN_IF(!pMutex
, "sal.osl.mutex", "null pMutex");
98 if ( pMutex
!= nullptr )
100 int nRet
= pthread_mutex_lock(&(pMutex
->mutex
));
103 SAL_WARN("sal.osl.mutex", "pthread_mutex_lock failed: " << UnixErrnoString(nRet
));
107 __coverity_recursive_lock_acquire__(pMutex
);
112 /* not initialized */
116 sal_Bool SAL_CALL
osl_tryToAcquireMutex(oslMutex pMutex
)
120 SAL_WARN_IF(!pMutex
, "sal.osl.mutex", "null pMutex");
124 int nRet
= pthread_mutex_trylock(&(pMutex
->mutex
));
128 __coverity_recursive_lock_acquire__(pMutex
);
137 sal_Bool SAL_CALL
osl_releaseMutex(oslMutex pMutex
)
140 __coverity_assert_locked__(pMutex
);
142 SAL_WARN_IF(!pMutex
, "sal.osl.mutex", "null pMutex");
146 int nRet
= pthread_mutex_unlock(&(pMutex
->mutex
));
149 SAL_WARN("sal.osl.mutex", "pthread_mutex_unlock failed: " << UnixErrnoString(nRet
));
154 __coverity_recursive_lock_release__(pMutex
);
159 /* not initialized */
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */