xref: /aosp_15_r20/external/cronet/third_party/apache-portable-runtime/src/atomic/win32/apr_atomic.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "apr.h"
18 #include "apr_atomic.h"
19 #include "apr_thread_mutex.h"
20 
apr_atomic_init(apr_pool_t * p)21 APR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p)
22 {
23     return APR_SUCCESS;
24 }
25 
apr_atomic_add32(volatile apr_uint32_t * mem,apr_uint32_t val)26 APR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val)
27 {
28 #if (defined(_M_IA64) || defined(_M_AMD64))
29     return InterlockedExchangeAdd(mem, val);
30 #else
31     return InterlockedExchangeAdd((long *)mem, val);
32 #endif
33 }
34 
35 /* Of course we want the 2's compliment of the unsigned value, val */
36 #ifdef _MSC_VER
37 #pragma warning(disable: 4146)
38 #endif
39 
apr_atomic_sub32(volatile apr_uint32_t * mem,apr_uint32_t val)40 APR_DECLARE(void) apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val)
41 {
42 #if (defined(_M_IA64) || defined(_M_AMD64))
43     InterlockedExchangeAdd(mem, -val);
44 #else
45     InterlockedExchangeAdd((long *)mem, -val);
46 #endif
47 }
48 
apr_atomic_inc32(volatile apr_uint32_t * mem)49 APR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem)
50 {
51     /* we return old value, win32 returns new value :( */
52 #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
53     return InterlockedIncrement(mem) - 1;
54 #else
55     return InterlockedIncrement((long *)mem) - 1;
56 #endif
57 }
58 
apr_atomic_dec32(volatile apr_uint32_t * mem)59 APR_DECLARE(int) apr_atomic_dec32(volatile apr_uint32_t *mem)
60 {
61 #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
62     return InterlockedDecrement(mem);
63 #else
64     return InterlockedDecrement((long *)mem);
65 #endif
66 }
67 
apr_atomic_set32(volatile apr_uint32_t * mem,apr_uint32_t val)68 APR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val)
69 {
70 #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
71     InterlockedExchange(mem, val);
72 #else
73     InterlockedExchange((long*)mem, val);
74 #endif
75 }
76 
apr_atomic_read32(volatile apr_uint32_t * mem)77 APR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem)
78 {
79     return *mem;
80 }
81 
apr_atomic_cas32(volatile apr_uint32_t * mem,apr_uint32_t with,apr_uint32_t cmp)82 APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with,
83                                            apr_uint32_t cmp)
84 {
85 #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
86     return InterlockedCompareExchange(mem, with, cmp);
87 #else
88     return InterlockedCompareExchange((long*)mem, with, cmp);
89 #endif
90 }
91 
apr_atomic_casptr(volatile void ** mem,void * with,const void * cmp)92 APR_DECLARE(void *) apr_atomic_casptr(volatile void **mem, void *with, const void *cmp)
93 {
94 #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
95     return InterlockedCompareExchangePointer((void* volatile*)mem, with, (void*)cmp);
96 #else
97     return InterlockedCompareExchangePointer((void**)mem, with, (void*)cmp);
98 #endif
99 }
100 
apr_atomic_xchg32(volatile apr_uint32_t * mem,apr_uint32_t val)101 APR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val)
102 {
103 #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
104     return InterlockedExchange(mem, val);
105 #else
106     return InterlockedExchange((long *)mem, val);
107 #endif
108 }
109 
apr_atomic_xchgptr(volatile void ** mem,void * with)110 APR_DECLARE(void*) apr_atomic_xchgptr(volatile void **mem, void *with)
111 {
112     return InterlockedExchangePointer((void**)mem, with);
113 }
114