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 .
20 #include <config_global.h>
21 #include <osl/interlck.h>
23 #if ( defined (__sun) || defined ( NETBSD ) ) && defined ( SPARC )
24 #error please use asm/interlck_sparc.s
25 #elif defined (__sun) && defined ( X86 )
26 #error please use asm/interlck_x86.s
27 #elif HAVE_GCC_BUILTIN_ATOMIC
28 oslInterlockedCount SAL_CALL
osl_incrementInterlockedCount(oslInterlockedCount
* pCount
)
30 return __sync_add_and_fetch(pCount
, 1);
32 oslInterlockedCount SAL_CALL
osl_decrementInterlockedCount(oslInterlockedCount
* pCount
)
34 return __sync_sub_and_fetch(pCount
, 1);
36 #elif defined ( __GNUC__ ) && ( defined ( X86 ) || defined ( X86_64 ) )
37 /* That's possible on x86-64 too since oslInterlockedCount is a sal_Int32 */
39 oslInterlockedCount SAL_CALL
osl_incrementInterlockedCount(oslInterlockedCount
* pCount
)
41 register oslInterlockedCount nCount
asm("%eax");
43 __asm__
__volatile__ (
46 : "+r" (nCount
), "+m" (*pCount
)
52 oslInterlockedCount SAL_CALL
osl_decrementInterlockedCount(oslInterlockedCount
* pCount
)
54 register oslInterlockedCount nCount
asm("%eax");
56 __asm__
__volatile__ (
59 : "+r" (nCount
), "+m" (*pCount
)
65 /* use only if nothing else works, expensive due to single mutex for all reference counts */
67 static pthread_mutex_t InterLock
= PTHREAD_MUTEX_INITIALIZER
;
69 oslInterlockedCount SAL_CALL
osl_incrementInterlockedCount(oslInterlockedCount
* pCount
)
71 oslInterlockedCount Count
;
73 pthread_mutex_lock(&InterLock
);
75 pthread_mutex_unlock(&InterLock
);
80 oslInterlockedCount SAL_CALL
osl_decrementInterlockedCount(oslInterlockedCount
* pCount
)
82 oslInterlockedCount Count
;
84 pthread_mutex_lock(&InterLock
);
86 pthread_mutex_unlock(&InterLock
);
93 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */