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
26 #include "unixerrnostring.hxx"
28 #include <sal/log.hxx>
29 #include <osl/mutex.h>
35 typedef struct _oslMutexImpl
37 pthread_mutex_t mutex
;
40 oslMutex SAL_CALL
osl_createMutex()
42 oslMutexImpl
* pMutex
= static_cast<oslMutexImpl
*>(malloc(sizeof(oslMutexImpl
)));
43 pthread_mutexattr_t aMutexAttr
;
46 SAL_WARN_IF(!pMutex
, "sal.osl.mutex", "null pMutex");
48 if ( pMutex
== nullptr )
53 pthread_mutexattr_init(&aMutexAttr
);
55 nRet
= pthread_mutexattr_settype(&aMutexAttr
, PTHREAD_MUTEX_RECURSIVE
);
57 nRet
= pthread_mutex_init(&(pMutex
->mutex
), &aMutexAttr
);
60 SAL_WARN("sal.osl.mutex", "pthread_muxex_init failed: " << UnixErrnoString(nRet
));
66 pthread_mutexattr_destroy(&aMutexAttr
);
71 void SAL_CALL
osl_destroyMutex(oslMutex pMutex
)
73 SAL_WARN_IF(!pMutex
, "sal.osl.mutex", "null pMutex");
75 if ( pMutex
!= nullptr )
77 int nRet
= pthread_mutex_destroy(&(pMutex
->mutex
));
80 SAL_WARN("sal.osl.mutex", "pthread_mutex_destroy failed: " << UnixErrnoString(nRet
));
88 extern void __coverity_recursive_lock_acquire__(void*);
89 extern void __coverity_recursive_lock_release__(void*);
90 extern void __coverity_assert_locked__(void*);
93 sal_Bool SAL_CALL
osl_acquireMutex(oslMutex pMutex
)
95 SAL_WARN_IF(!pMutex
, "sal.osl.mutex", "null pMutex");
97 if ( pMutex
!= nullptr )
99 int nRet
= pthread_mutex_lock(&(pMutex
->mutex
));
102 SAL_WARN("sal.osl.mutex", "pthread_mutex_lock failed: " << UnixErrnoString(nRet
));
106 __coverity_recursive_lock_acquire__(pMutex
);
111 /* not initialized */
115 sal_Bool SAL_CALL
osl_tryToAcquireMutex(oslMutex pMutex
)
119 SAL_WARN_IF(!pMutex
, "sal.osl.mutex", "null pMutex");
123 int nRet
= pthread_mutex_trylock(&(pMutex
->mutex
));
127 __coverity_recursive_lock_acquire__(pMutex
);
136 sal_Bool SAL_CALL
osl_releaseMutex(oslMutex pMutex
)
139 __coverity_assert_locked__(pMutex
);
141 SAL_WARN_IF(!pMutex
, "sal.osl.mutex", "null pMutex");
145 int nRet
= pthread_mutex_unlock(&(pMutex
->mutex
));
148 SAL_WARN("sal.osl.mutex", "pthread_mutex_unlock failed: " << UnixErrnoString(nRet
));
153 __coverity_recursive_lock_release__(pMutex
);
158 /* not initialized */
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */