Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / osl / unx / interlck.c
blobcd8b5d2125db248b5d99f0bb62e17f2c277a38ba
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 .
21 #include "system.h"
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");
41 nCount = 1;
42 __asm__ __volatile__ (
43 "xaddl %0, %1\n\t"
44 : "+r" (nCount), "+m" (*pCount)
45 : /* nothing */
46 : "memory");
47 return ++nCount;
49 #if HAVE_GCC_BUILTIN_ATOMIC
50 else
51 return __sync_add_and_fetch (pCount, 1);
52 #else
53 else {
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 #endif
67 oslInterlockedCount SAL_CALL osl_decrementInterlockedCount(oslInterlockedCount* pCount)
69 if ( osl_isSingleCPU ) {
70 register oslInterlockedCount nCount asm("%eax");
71 nCount = -1;
72 __asm__ __volatile__ (
73 "xaddl %0, %1\n\t"
74 : "+r" (nCount), "+m" (*pCount)
75 : /* nothing */
76 : "memory");
77 return --nCount;
79 #if HAVE_GCC_BUILTIN_ATOMIC
80 else
81 return __sync_sub_and_fetch (pCount, 1);
82 #else
83 else {
84 register oslInterlockedCount nCount asm("%eax");
85 nCount = -1;
86 __asm__ __volatile__ (
87 "lock\n\t"
88 "xaddl %0, %1\n\t"
89 : "+r" (nCount), "+m" (*pCount)
90 : /* nothing */
91 : "memory");
92 return --nCount;
94 #endif
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);
105 #else
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);
115 Count = ++(*pCount);
116 pthread_mutex_unlock(&InterLock);
118 return (Count);
121 oslInterlockedCount SAL_CALL osl_decrementInterlockedCount(oslInterlockedCount* pCount)
123 oslInterlockedCount Count;
125 pthread_mutex_lock(&InterLock);
126 Count = --(*pCount);
127 pthread_mutex_unlock(&InterLock);
129 return (Count);
132 #endif /* default */
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */