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 .
23 #include <osl/interlck.h>
24 #include <osl/diagnose.h>
26 #if ( defined ( SOLARIS ) || defined ( NETBSD ) ) && defined ( SPARC )
27 #error please use asm/interlck_sparc.s
28 #elif defined ( SOLARIS) && defined ( X86 )
29 #error please use asm/interlck_x86.s
30 #elif defined ( __GNUC__ ) && ( defined ( X86 ) || defined ( X86_64 ) )
31 /* That's possible on x86-64 too since oslInterlockedCount is a sal_Int32 */
33 extern int osl_isSingleCPU
;
35 oslInterlockedCount SAL_CALL
osl_incrementInterlockedCount(oslInterlockedCount
* pCount
)
37 // Fast case for old, slow, single CPU Intel machines for whom
38 // interlocking is a performance nightmare.
39 if ( osl_isSingleCPU
) {
40 register oslInterlockedCount nCount
asm("%eax");
42 __asm__
__volatile__ (
44 : "+r" (nCount
), "+m" (*pCount
)
49 #if HAVE_GCC_BUILTIN_ATOMIC
51 return __sync_add_and_fetch (pCount
, 1);
54 register oslInterlockedCount nCount
asm("%eax");
56 __asm__
__volatile__ (
59 : "+r" (nCount
), "+m" (*pCount
)
67 oslInterlockedCount SAL_CALL
osl_decrementInterlockedCount(oslInterlockedCount
* pCount
)
69 if ( osl_isSingleCPU
) {
70 register oslInterlockedCount nCount
asm("%eax");
72 __asm__
__volatile__ (
74 : "+r" (nCount
), "+m" (*pCount
)
79 #if HAVE_GCC_BUILTIN_ATOMIC
81 return __sync_sub_and_fetch (pCount
, 1);
84 register oslInterlockedCount nCount
asm("%eax");
86 __asm__
__volatile__ (
89 : "+r" (nCount
), "+m" (*pCount
)
96 #elif HAVE_GCC_BUILTIN_ATOMIC
97 oslInterlockedCount SAL_CALL
osl_incrementInterlockedCount(oslInterlockedCount
* pCount
)
99 return __sync_add_and_fetch(pCount
, 1);
101 oslInterlockedCount SAL_CALL
osl_decrementInterlockedCount(oslInterlockedCount
* pCount
)
103 return __sync_sub_and_fetch(pCount
, 1);
106 /* use only if nothing else works, expensive due to single mutex for all reference counts */
108 static pthread_mutex_t InterLock
= PTHREAD_MUTEX_INITIALIZER
;
110 oslInterlockedCount SAL_CALL
osl_incrementInterlockedCount(oslInterlockedCount
* pCount
)
112 oslInterlockedCount Count
;
114 pthread_mutex_lock(&InterLock
);
116 pthread_mutex_unlock(&InterLock
);
121 oslInterlockedCount SAL_CALL
osl_decrementInterlockedCount(oslInterlockedCount
* pCount
)
123 oslInterlockedCount Count
;
125 pthread_mutex_lock(&InterLock
);
127 pthread_mutex_unlock(&InterLock
);
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */