builtins64.c revision 361666
172172Sphantom/* Licensed to the Apache Software Foundation (ASF) under one or more
272172Sphantom * contributor license agreements.  See the NOTICE file distributed with
388473Sphantom * this work for additional information regarding copyright ownership.
4118459Smtm * The ASF licenses this file to You under the Apache License, Version 2.0
5123682Sache * (the "License"); you may not use this file except in compliance with
686073Sache * the License.  You may obtain a copy of the License at
7108428Sache *
877984Sache *     http://www.apache.org/licenses/LICENSE-2.0
977984Sache *
10193867Sedwin * Unless required by applicable law or agreed to in writing, software
1177984Sache * distributed under the License is distributed on an "AS IS" BASIS,
1277984Sache * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1377984Sache * See the License for the specific language governing permissions and
1477984Sache * limitations under the License.
1577984Sache */
16134437Stjr
1777984Sache#include "apr_arch_atomic.h"
1890583Sphantom
1977984Sache#ifdef USE_ATOMICS_BUILTINS
2087043Sache
2177984SacheAPR_DECLARE(apr_uint64_t) apr_atomic_read64(volatile apr_uint64_t *mem)
2277984Sache{
23117259Sache    return *mem;
2477984Sache}
2577984Sache
2688473SphantomAPR_DECLARE(void) apr_atomic_set64(volatile apr_uint64_t *mem, apr_uint64_t val)
27125208Sache{
2888473Sphantom    *mem = val;
29105965Sache}
30162940Sache
3177984SacheAPR_DECLARE(apr_uint64_t) apr_atomic_add64(volatile apr_uint64_t *mem, apr_uint64_t val)
3277984Sache{
3377984Sache    return __sync_fetch_and_add(mem, val);
3493885Sphantom}
3577984Sache
3689077SacheAPR_DECLARE(void) apr_atomic_sub64(volatile apr_uint64_t *mem, apr_uint64_t val)
3772208Sasmodai{
3877984Sache    __sync_fetch_and_sub(mem, val);
3977984Sache}
40105445Sache
41105445SacheAPR_DECLARE(apr_uint64_t) apr_atomic_inc64(volatile apr_uint64_t *mem)
4277984Sache{
4377984Sache    return __sync_fetch_and_add(mem, 1);
4472565Sache}
45118174Sache
4672172SphantomAPR_DECLARE(int) apr_atomic_dec64(volatile apr_uint64_t *mem)
47136659Sru{
4876105Sphantom    return __sync_sub_and_fetch(mem, 1);
4972172Sphantom}
5072172Sphantom
5172172SphantomAPR_DECLARE(apr_uint64_t) apr_atomic_cas64(volatile apr_uint64_t *mem, apr_uint64_t with,
5272172Sphantom                                           apr_uint64_t cmp)
5372172Sphantom{
54136659Sru    return __sync_val_compare_and_swap(mem, cmp, with);
55136659Sru}
5672172Sphantom
57136659SruAPR_DECLARE(apr_uint64_t) apr_atomic_xchg64(volatile apr_uint64_t *mem, apr_uint64_t val)
58136659Sru{
5972258Swollman    __sync_synchronize();
60136659Sru
61136659Sru    return __sync_lock_test_and_set(mem, val);
62136659Sru}
63136659Sru
64136659Sru#endif /* USE_ATOMICS_BUILTINS */
65136659Sru