stronger typing for SwClient::GetRegisteredIn" and fix SwIterator cast
[LibreOffice.git] / sal / osl / unx / interlck.cxx
blob8bc63211b32853728e01cdbefed4cfc58b675de8
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 #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");
42 nCount = 1;
43 __asm__ __volatile__ (
44 "lock\n\t"
45 "xaddl %0, %1\n\t"
46 : "+r" (nCount), "+m" (*pCount)
47 : /* nothing */
48 : "memory");
49 return ++nCount;
52 oslInterlockedCount SAL_CALL osl_decrementInterlockedCount(oslInterlockedCount* pCount)
54 register oslInterlockedCount nCount asm("%eax");
55 nCount = -1;
56 __asm__ __volatile__ (
57 "lock\n\t"
58 "xaddl %0, %1\n\t"
59 : "+r" (nCount), "+m" (*pCount)
60 : /* nothing */
61 : "memory");
62 return --nCount;
64 #else
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);
74 Count = ++(*pCount);
75 pthread_mutex_unlock(&InterLock);
77 return Count;
80 oslInterlockedCount SAL_CALL osl_decrementInterlockedCount(oslInterlockedCount* pCount)
82 oslInterlockedCount Count;
84 pthread_mutex_lock(&InterLock);
85 Count = --(*pCount);
86 pthread_mutex_unlock(&InterLock);
88 return Count;
91 #endif /* default */
93 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */