vlc_atomic.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * vlc_atomic.h:
00003  *****************************************************************************
00004  * Copyright (C) 2010 Rémi Denis-Courmont
00005  *
00006  * This program is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 2.1 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00019  *****************************************************************************/
00020 
00021 #ifndef VLC_ATOMIC_H
00022 # define VLC_ATOMIC_H
00023 
00024 /**
00025  * \file
00026  * Atomic operations do not require locking, but they are not very powerful.
00027  */
00028 
00029 /** Static initializer for \ref vlc_atomic_t */
00030 # define VLC_ATOMIC_INIT(val) { (val) }
00031 
00032 /* All functions return the atom value _after_ the operation. */
00033 
00034 VLC_API uintptr_t vlc_atomic_get(const vlc_atomic_t *);
00035 VLC_API uintptr_t vlc_atomic_set(vlc_atomic_t *, uintptr_t);
00036 VLC_API uintptr_t vlc_atomic_add(vlc_atomic_t *, uintptr_t);
00037 
00038 static inline uintptr_t vlc_atomic_sub (vlc_atomic_t *atom, uintptr_t v)
00039 {
00040     return vlc_atomic_add (atom, -v);
00041 }
00042 
00043 static inline uintptr_t vlc_atomic_inc (vlc_atomic_t *atom)
00044 {
00045     return vlc_atomic_add (atom, 1);
00046 }
00047 
00048 static inline uintptr_t vlc_atomic_dec (vlc_atomic_t *atom)
00049 {
00050     return vlc_atomic_sub (atom, 1);
00051 }
00052 
00053 VLC_API uintptr_t vlc_atomic_swap(vlc_atomic_t *, uintptr_t);
00054 VLC_API uintptr_t vlc_atomic_compare_swap(vlc_atomic_t *, uintptr_t, uintptr_t);
00055 
00056 /** Helper to retrieve a single precision from an atom. */
00057 static inline float vlc_atomic_getf(const vlc_atomic_t *atom)
00058 {
00059     union { float f; uintptr_t i; } u;
00060     u.i = vlc_atomic_get(atom);
00061     return u.f;
00062 }
00063 
00064 /** Helper to store a single precision into an atom. */
00065 static inline float vlc_atomic_setf(vlc_atomic_t *atom, float f)
00066 {
00067     union { float f; uintptr_t i; } u;
00068     u.f = f;
00069     vlc_atomic_set(atom, u.i);
00070     return f;
00071 }
00072 
00073 #endif
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines