1361666Sdim/* Licensed to the Apache Software Foundation (ASF) under one or more
2361666Sdim * contributor license agreements.  See the NOTICE file distributed with
3361666Sdim * this work for additional information regarding copyright ownership.
4361666Sdim * The ASF licenses this file to You under the Apache License, Version 2.0
5361666Sdim * (the "License"); you may not use this file except in compliance with
6361666Sdim * the License.  You may obtain a copy of the License at
7361666Sdim *
8361666Sdim *     http://www.apache.org/licenses/LICENSE-2.0
9361666Sdim *
10361666Sdim * Unless required by applicable law or agreed to in writing, software
11361666Sdim * distributed under the License is distributed on an "AS IS" BASIS,
12361666Sdim * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13361666Sdim * See the License for the specific language governing permissions and
14361666Sdim * limitations under the License.
15361666Sdim */
16361666Sdim
17361666Sdim#include "apr_arch_atomic.h"
18361666Sdim
19361666Sdim#ifdef USE_ATOMICS_BUILTINS
20361666Sdim
21361666SdimAPR_DECLARE(apr_uint64_t) apr_atomic_read64(volatile apr_uint64_t *mem)
22361666Sdim{
23361666Sdim    return *mem;
24361666Sdim}
25361666Sdim
26361666SdimAPR_DECLARE(void) apr_atomic_set64(volatile apr_uint64_t *mem, apr_uint64_t val)
27361666Sdim{
28361666Sdim    *mem = val;
29361666Sdim}
30361666Sdim
31361666SdimAPR_DECLARE(apr_uint64_t) apr_atomic_add64(volatile apr_uint64_t *mem, apr_uint64_t val)
32361666Sdim{
33361666Sdim    return __sync_fetch_and_add(mem, val);
34361666Sdim}
35361666Sdim
36361666SdimAPR_DECLARE(void) apr_atomic_sub64(volatile apr_uint64_t *mem, apr_uint64_t val)
37361666Sdim{
38361666Sdim    __sync_fetch_and_sub(mem, val);
39361666Sdim}
40361666Sdim
41361666SdimAPR_DECLARE(apr_uint64_t) apr_atomic_inc64(volatile apr_uint64_t *mem)
42361666Sdim{
43361666Sdim    return __sync_fetch_and_add(mem, 1);
44361666Sdim}
45361666Sdim
46361666SdimAPR_DECLARE(int) apr_atomic_dec64(volatile apr_uint64_t *mem)
47361666Sdim{
48361666Sdim    return __sync_sub_and_fetch(mem, 1);
49361666Sdim}
50361666Sdim
51361666SdimAPR_DECLARE(apr_uint64_t) apr_atomic_cas64(volatile apr_uint64_t *mem, apr_uint64_t with,
52361666Sdim                                           apr_uint64_t cmp)
53361666Sdim{
54361666Sdim    return __sync_val_compare_and_swap(mem, cmp, with);
55361666Sdim}
56361666Sdim
57361666SdimAPR_DECLARE(apr_uint64_t) apr_atomic_xchg64(volatile apr_uint64_t *mem, apr_uint64_t val)
58361666Sdim{
59361666Sdim    __sync_synchronize();
60361666Sdim
61361666Sdim    return __sync_lock_test_and_set(mem, val);
62361666Sdim}
63361666Sdim
64361666Sdim#endif /* USE_ATOMICS_BUILTINS */
65