129759Swollman/* Licensed to the Apache Software Foundation (ASF) under one or more
229759Swollman * contributor license agreements.  See the NOTICE file distributed with
329759Swollman * this work for additional information regarding copyright ownership.
429759Swollman * The ASF licenses this file to You under the Apache License, Version 2.0
529759Swollman * (the "License"); you may not use this file except in compliance with
629759Swollman * the License.  You may obtain a copy of the License at
729759Swollman *
829759Swollman *     http://www.apache.org/licenses/LICENSE-2.0
929759Swollman *
1029759Swollman * Unless required by applicable law or agreed to in writing, software
1129759Swollman * distributed under the License is distributed on an "AS IS" BASIS,
1229759Swollman * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1329759Swollman * See the License for the specific language governing permissions and
1429759Swollman * limitations under the License.
15226396Sed */
1629759Swollman
1729759Swollman#include "apr_arch_atomic.h"
1829759Swollman
1929759Swollman#ifdef USE_ATOMICS_BUILTINS
2029759Swollman
2129759SwollmanAPR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p)
2229759Swollman{
2329759Swollman    return APR_SUCCESS;
2429759Swollman}
2529759Swollman
2629759SwollmanAPR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem)
2729759Swollman{
2829759Swollman    return *mem;
2929759Swollman}
3029759Swollman
3129759SwollmanAPR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val)
3229759Swollman{
3329759Swollman    *mem = val;
3429759Swollman}
3529759Swollman
3629759SwollmanAPR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val)
3729759Swollman{
3829759Swollman    return __sync_fetch_and_add(mem, val);
3929759Swollman}
4029759Swollman
4129759SwollmanAPR_DECLARE(void) apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val)
4229759Swollman{
4329759Swollman    __sync_fetch_and_sub(mem, val);
4429759Swollman}
4529759Swollman
4629759SwollmanAPR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem)
4729759Swollman{
4829759Swollman    return __sync_fetch_and_add(mem, 1);
4929759Swollman}
5029759Swollman
5129759SwollmanAPR_DECLARE(int) apr_atomic_dec32(volatile apr_uint32_t *mem)
5229759Swollman{
5329759Swollman    return __sync_sub_and_fetch(mem, 1);
5429759Swollman}
5529759Swollman
5629759SwollmanAPR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with,
5729759Swollman                                           apr_uint32_t cmp)
5829759Swollman{
5987715Smarkm    return __sync_val_compare_and_swap(mem, cmp, with);
6087715Smarkm}
6187715Smarkm
6287715SmarkmAPR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val)
63200462Sdelphij{
64200462Sdelphij    __sync_synchronize();
6529759Swollman
6629759Swollman    return __sync_lock_test_and_set(mem, val);
6729759Swollman}
6829759Swollman
6929759SwollmanAPR_DECLARE(void*) apr_atomic_casptr(volatile void **mem, void *with, const void *cmp)
7029759Swollman{
7129759Swollman    return (void*) __sync_val_compare_and_swap(mem, cmp, with);
7229759Swollman}
7329759Swollman
7487715SmarkmAPR_DECLARE(void*) apr_atomic_xchgptr(volatile void **mem, void *with)
7529759Swollman{
7629759Swollman    __sync_synchronize();
7729759Swollman
7829759Swollman    return (void*) __sync_lock_test_and_set(mem, with);
7929759Swollman}
8029759Swollman
8129759Swollman#endif /* USE_ATOMICS_BUILTINS */
8229759Swollman