tdf#154546 skip dispatch when presenter controller is not set
[LibreOffice.git] / sal / osl / unx / mutex.cxx
blob1ca4958fd01d29e589f0b5a930cd2208b53bfcaa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
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 #include "unixerrnostring.hxx"
29 #include <sal/log.hxx>
30 #include <osl/mutex.h>
32 #include <pthread.h>
33 #include <stdlib.h>
36 typedef struct _oslMutexImpl
38 pthread_mutex_t mutex;
39 } oslMutexImpl;
41 oslMutex SAL_CALL osl_createMutex()
43 oslMutexImpl* pMutex = static_cast<oslMutexImpl*>(malloc(sizeof(oslMutexImpl)));
44 pthread_mutexattr_t aMutexAttr;
45 int nRet=0;
47 SAL_WARN_IF(!pMutex, "sal.osl.mutex", "null pMutex");
49 if ( pMutex == nullptr )
51 return nullptr;
54 pthread_mutexattr_init(&aMutexAttr);
56 nRet = pthread_mutexattr_settype(&aMutexAttr, PTHREAD_MUTEX_RECURSIVE);
57 if( nRet == 0 )
58 nRet = pthread_mutex_init(&(pMutex->mutex), &aMutexAttr);
59 if ( nRet != 0 )
61 SAL_WARN("sal.osl.mutex", "pthread_muxex_init failed: " << UnixErrnoString(nRet));
63 free(pMutex);
64 pMutex = nullptr;
67 pthread_mutexattr_destroy(&aMutexAttr);
69 return pMutex;
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));
79 if ( nRet != 0 )
81 SAL_WARN("sal.osl.mutex", "pthread_mutex_destroy failed: " << UnixErrnoString(nRet));
84 free(pMutex);
88 #ifdef __COVERITY__
89 extern void __coverity_recursive_lock_acquire__(void*);
90 extern void __coverity_recursive_lock_release__(void*);
91 extern void __coverity_assert_locked__(void*);
92 #endif
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));
101 if ( nRet != 0 )
103 SAL_WARN("sal.osl.mutex", "pthread_mutex_lock failed: " << UnixErrnoString(nRet));
104 return false;
106 #ifdef __COVERITY__
107 __coverity_recursive_lock_acquire__(pMutex);
108 #endif
109 return true;
112 /* not initialized */
113 return false;
116 sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutex pMutex)
118 bool result = false;
120 SAL_WARN_IF(!pMutex, "sal.osl.mutex", "null pMutex");
122 if ( pMutex )
124 int nRet = pthread_mutex_trylock(&(pMutex->mutex));
125 if ( nRet == 0 )
127 #ifdef __COVERITY__
128 __coverity_recursive_lock_acquire__(pMutex);
129 #endif
130 result = true;
134 return result;
137 sal_Bool SAL_CALL osl_releaseMutex(oslMutex pMutex)
139 #ifdef __COVERITY__
140 __coverity_assert_locked__(pMutex);
141 #endif
142 SAL_WARN_IF(!pMutex, "sal.osl.mutex", "null pMutex");
144 if ( pMutex )
146 int nRet = pthread_mutex_unlock(&(pMutex->mutex));
147 if ( nRet != 0 )
149 SAL_WARN("sal.osl.mutex", "pthread_mutex_unlock failed: " << UnixErrnoString(nRet));
150 return false;
153 #ifdef __COVERITY__
154 __coverity_recursive_lock_release__(pMutex);
155 #endif
156 return true;
159 /* not initialized */
160 return false;
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */