2 * Copyright (c) 2012 Ronald S. Bultje <rsbultje@gmail.com>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #if !HAVE_MEMORYBARRIER && !HAVE_SYNC_VAL_COMPARE_AND_SWAP && !HAVE_MACHINE_RW_BARRIER
29 static pthread_mutex_t atomic_lock = PTHREAD_MUTEX_INITIALIZER;
31 int avpriv_atomic_int_get(volatile int *ptr)
35 pthread_mutex_lock(&atomic_lock);
37 pthread_mutex_unlock(&atomic_lock);
42 void avpriv_atomic_int_set(volatile int *ptr, int val)
44 pthread_mutex_lock(&atomic_lock);
46 pthread_mutex_unlock(&atomic_lock);
49 int avpriv_atomic_int_add_and_fetch(volatile int *ptr, int inc)
53 pthread_mutex_lock(&atomic_lock);
56 pthread_mutex_unlock(&atomic_lock);
61 void *avpriv_atomic_ptr_cas(void * volatile *ptr, void *oldval, void *newval)
64 pthread_mutex_lock(&atomic_lock);
68 pthread_mutex_unlock(&atomic_lock);
74 int avpriv_atomic_int_get(volatile int *ptr)
79 void avpriv_atomic_int_set(volatile int *ptr, int val)
84 int avpriv_atomic_int_add_and_fetch(volatile int *ptr, int inc)
90 void *avpriv_atomic_ptr_cas(void * volatile *ptr, void *oldval, void *newval)
101 #error "Threading is enabled, but there is no implementation of atomic operations available"
103 #endif /* HAVE_PTHREADS */
105 #endif /* !HAVE_MEMORYBARRIER && !HAVE_SYNC_VAL_COMPARE_AND_SWAP && !HAVE_MACHINE_RW_BARRIER */
112 volatile int val = 1;
115 res = avpriv_atomic_int_add_and_fetch(&val, 1);
117 avpriv_atomic_int_set(&val, 3);
118 res = avpriv_atomic_int_get(&val);