Avoid potential negative array index access to cached text.
[LibreOffice.git] / sal / osl / unx / mutex.cxx
blobe3786e43a1f4c98963b31b16f13e57417c9a659d
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 "unixerrnostring.hxx"
28 #include <sal/log.hxx>
29 #include <osl/mutex.h>
31 #include <pthread.h>
32 #include <stdlib.h>
35 typedef struct _oslMutexImpl
37 pthread_mutex_t mutex;
38 } oslMutexImpl;
40 oslMutex SAL_CALL osl_createMutex()
42 oslMutexImpl* pMutex = static_cast<oslMutexImpl*>(malloc(sizeof(oslMutexImpl)));
43 pthread_mutexattr_t aMutexAttr;
44 int nRet=0;
46 SAL_WARN_IF(!pMutex, "sal.osl.mutex", "null pMutex");
48 if ( pMutex == nullptr )
50 return nullptr;
53 pthread_mutexattr_init(&aMutexAttr);
55 nRet = pthread_mutexattr_settype(&aMutexAttr, PTHREAD_MUTEX_RECURSIVE);
56 if( nRet == 0 )
57 nRet = pthread_mutex_init(&(pMutex->mutex), &aMutexAttr);
58 if ( nRet != 0 )
60 SAL_WARN("sal.osl.mutex", "pthread_muxex_init failed: " << UnixErrnoString(nRet));
62 free(pMutex);
63 pMutex = nullptr;
66 pthread_mutexattr_destroy(&aMutexAttr);
68 return pMutex;
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));
78 if ( nRet != 0 )
80 SAL_WARN("sal.osl.mutex", "pthread_mutex_destroy failed: " << UnixErrnoString(nRet));
83 free(pMutex);
87 #ifdef __COVERITY__
88 extern void __coverity_recursive_lock_acquire__(void*);
89 extern void __coverity_recursive_lock_release__(void*);
90 extern void __coverity_assert_locked__(void*);
91 #endif
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));
100 if ( nRet != 0 )
102 SAL_WARN("sal.osl.mutex", "pthread_mutex_lock failed: " << UnixErrnoString(nRet));
103 return false;
105 #ifdef __COVERITY__
106 __coverity_recursive_lock_acquire__(pMutex);
107 #endif
108 return true;
111 /* not initialized */
112 return false;
115 sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutex pMutex)
117 bool result = false;
119 SAL_WARN_IF(!pMutex, "sal.osl.mutex", "null pMutex");
121 if ( pMutex )
123 int nRet = pthread_mutex_trylock(&(pMutex->mutex));
124 if ( nRet == 0 )
126 #ifdef __COVERITY__
127 __coverity_recursive_lock_acquire__(pMutex);
128 #endif
129 result = true;
133 return result;
136 sal_Bool SAL_CALL osl_releaseMutex(oslMutex pMutex)
138 #ifdef __COVERITY__
139 __coverity_assert_locked__(pMutex);
140 #endif
141 SAL_WARN_IF(!pMutex, "sal.osl.mutex", "null pMutex");
143 if ( pMutex )
145 int nRet = pthread_mutex_unlock(&(pMutex->mutex));
146 if ( nRet != 0 )
148 SAL_WARN("sal.osl.mutex", "pthread_mutex_unlock failed: " << UnixErrnoString(nRet));
149 return false;
152 #ifdef __COVERITY__
153 __coverity_recursive_lock_release__(pMutex);
154 #endif
155 return true;
158 /* not initialized */
159 return false;
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */