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
24 #if !HAVE_ATOMICS_NATIVE
30 static pthread_mutex_t atomic_lock
= PTHREAD_MUTEX_INITIALIZER
;
32 int avpriv_atomic_int_get(volatile int *ptr
)
36 pthread_mutex_lock(&atomic_lock
);
38 pthread_mutex_unlock(&atomic_lock
);
43 void avpriv_atomic_int_set(volatile int *ptr
, int val
)
45 pthread_mutex_lock(&atomic_lock
);
47 pthread_mutex_unlock(&atomic_lock
);
50 int avpriv_atomic_int_add_and_fetch(volatile int *ptr
, int inc
)
54 pthread_mutex_lock(&atomic_lock
);
57 pthread_mutex_unlock(&atomic_lock
);
62 void *avpriv_atomic_ptr_cas(void * volatile *ptr
, void *oldval
, void *newval
)
65 pthread_mutex_lock(&atomic_lock
);
69 pthread_mutex_unlock(&atomic_lock
);
75 int avpriv_atomic_int_get(volatile int *ptr
)
80 void avpriv_atomic_int_set(volatile int *ptr
, int val
)
85 int avpriv_atomic_int_add_and_fetch(volatile int *ptr
, int inc
)
91 void *avpriv_atomic_ptr_cas(void * volatile *ptr
, void *oldval
, void *newval
)
100 #else /* HAVE_THREADS */
102 /* This should never trigger, unless a new threading implementation
103 * without correct atomics dependencies in configure or a corresponding
104 * atomics implementation is added. */
105 #error "Threading is enabled, but there is no implementation of atomic operations available"
107 #endif /* HAVE_PTHREADS */
109 #endif /* !HAVE_ATOMICS_NATIVE */
112 #include "avassert.h"
116 volatile int val
= 1;
119 res
= avpriv_atomic_int_add_and_fetch(&val
, 1);
120 av_assert0(res
== 2);
121 avpriv_atomic_int_set(&val
, 3);
122 res
= avpriv_atomic_int_get(&val
);
123 av_assert0(res
== 3);