xref: /aoo42x/main/sal/osl/w32/interlck.c (revision 86e1cf34)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "system.h"
25 
26 #include <osl/interlck.h>
27 #include <osl/diagnose.h>
28 
29 extern int osl_isSingleCPU;
30 
31 /* For all Intel x86 above x486 we use a spezial inline assembler implementation.
32    The main reason is that WIN9? does not return the result of the operation.
33    Instead there is only returned a value greater than zero is the increment
34    result is greater than zero, but not the the result of the addition.
35    For Windows NT the native function could be used, because the correct result
36    is returned. Because of simpler code maintenance and performace reasons we use
37    on every x86-Windows-Platform the inline assembler implementation.
38 */
39 
40 /*****************************************************************************/
41 /* osl_incrementInterlockedCount */
42 /*****************************************************************************/
43 oslInterlockedCount SAL_CALL osl_incrementInterlockedCount(oslInterlockedCount* pCount)
44 #ifdef _M_IX86
45 #ifdef __MINGW32__
46 {
47 	asm
48 	(
49 	 "	movl        %0, %%ecx\n"
50 	 "  	movl        $1, %%eax\n"
51 	 "	movl        %1, %%edx\n"
52 	 "	cmpl        $0, %%edx\n"
53 	 "	je          1f\n"
54 	 "  	xadd        %%eax, (%%ecx)\n"
55 	 "	jmp         2f\n"
56 	 "1:\n"
57 	 "  	lock xadd   %%eax, (%%ecx)\n"
58 	 "2:\n"
59 	 "  	incl        %%eax\n"
60 	 ::"m"(pCount),"m"(osl_isSingleCPU)
61 	);
62 }
63 #else
64 #pragma warning(disable: 4035)
65 {
66     __asm
67     {
68         mov         ecx, pCount
69         mov         eax, 1
70         mov         edx, osl_isSingleCPU
71         cmp         edx, 0
72         je          is_not_single
73         xadd        dword ptr [ecx],eax
74         jmp         cont
75     is_not_single:
76         lock xadd   dword ptr [ecx],eax
77     cont:
78         inc         eax
79     }
80 }
81 #pragma warning(default: 4035)
82 #endif
83 #else
84 #pragma message("WARNING: Using system InterlockedIncrement")
85 {
86     return (InterlockedIncrement(pCount));
87 }
88 #endif
89 
90 /*****************************************************************************/
91 /* osl_decrementInterlockedCount */
92 /*****************************************************************************/
93 oslInterlockedCount SAL_CALL osl_decrementInterlockedCount(oslInterlockedCount* pCount)
94 #ifdef _M_IX86
95 #ifdef __MINGW32__
96 {
97 	asm
98 	(
99 	 "	movl        %0, %%ecx\n"
100 	 "  	orl         $-1, %%eax\n"
101 	 "	movl        %1, %%edx\n"
102 	 "	cmpl        $0, %%edx\n"
103 	 "	je          1f\n"
104 	 "  	xadd        %%eax, (%%ecx)\n"
105 	 "	jmp         2f\n"
106 	 "1:\n"
107 	 "  	lock xadd   %%eax, (%%ecx)\n"
108 	 "2:\n"
109 	 "  	decl        %%eax\n"
110 	 ::"m"(pCount),"m"(osl_isSingleCPU)
111 	);
112 }
113 #else
114 #pragma warning(disable: 4035)
115 {
116     __asm
117     {
118         mov         ecx, pCount
119         or          eax, -1
120         mov         edx, osl_isSingleCPU
121         cmp         edx, 0
122         je          is_not_single
123         xadd        dword ptr [ecx],eax
124         jmp         cont
125     is_not_single:
126         lock xadd   dword ptr [ecx],eax
127     cont:
128         dec         eax
129     }
130 }
131 #pragma warning(default: 4035)
132 #endif
133 #else
134 #pragma message("WARNING: Using system InterlockedDecrement")
135 {
136     return (InterlockedDecrement(pCount));
137 }
138 #endif
139