/* * Copyright (c) 2000 Paycounter, Inc. * Author: Alfred Perlstein , * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ /* Try to pull in native atomic type */ #include /* if not available, simulate using mutexes */ #ifndef HAVE_ATOMIC_TYPE #include struct atomic { struct mtx at_mtx; long at_cnt; }; /* * constructor must be called on any instantiated atomic struct */ static __inline void atomic_init(struct atomic *a) { mtx_init(&a->at_mtx, "atomic", MTX_DEF); a->at_cnt = 0; } /* * destructor, must be called when freeing an atomic struct */ static __inline void atomic_destroy(struct atomic *a) { mtx_destroy(&a->at_mtx); } /* * add 1 to an atomic counter */ static __inline void atomic_inc(struct atomic *a) { mtx_enter(&a->at_mtx, MTX_DEF); a->at_cnt++; mtx_exit(&a->at_mtx, MTX_DEF); } /* * subtract 1 and return true if it goes to zero */ static __inline int atomic_dec_tstz(struct atomic *a) { long ref; mtx_enter(&a->at_mtx, MTX_DEF); ref = --(a->at_cnt); mtx_exit(&a->at_mtx, MTX_DEF); return (ref == 0); } #endif