Browse code

Merge commit '65f1d45dcc71186ede72fff950996099d23359bd'

* commit '65f1d45dcc71186ede72fff950996099d23359bd':
lavu: add support for atomic operations.

Conflicts:
configure

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2013/03/08 23:47:06
Showing 8 changed files
... ...
@@ -1437,10 +1437,12 @@ HAVE_LIST="
1437 1437
     lzo1x_999_compress
1438 1438
     machine_ioctl_bt848_h
1439 1439
     machine_ioctl_meteor_h
1440
+    machine_rw_barrier
1440 1441
     makeinfo
1441 1442
     malloc_h
1442 1443
     MapViewOfFile
1443 1444
     memalign
1445
+    MemoryBarrier
1444 1446
     mkstemp
1445 1447
     mm_empty
1446 1448
     mmap
... ...
@@ -1480,6 +1482,7 @@ HAVE_LIST="
1480 1480
     struct_v4l2_frmivalenum_discrete
1481 1481
     symver_asm_label
1482 1482
     symver_gnu_asm
1483
+    sync_synchronize
1483 1484
     sysconf
1484 1485
     sysctl
1485 1486
     sys_mman_h
... ...
@@ -3792,6 +3795,9 @@ check_func_headers malloc.h _aligned_malloc     && enable aligned_malloc
3792 3792
 check_func  setrlimit
3793 3793
 check_func  strerror_r
3794 3794
 check_func  sched_getaffinity
3795
+check_builtin sync_synchronize "" "__sync_synchronize()"
3796
+check_builtin machine_rw_barrier mbarrier.h "__machine_rw_barrier()"
3797
+check_builtin MemoryBarrier windows.h "MemoryBarrier()"
3795 3798
 check_func  sysconf
3796 3799
 check_func  sysctl
3797 3800
 check_func  usleep
... ...
@@ -59,6 +59,7 @@ BUILT_HEADERS = avconfig.h
59 59
 
60 60
 OBJS = adler32.o                                                        \
61 61
        aes.o                                                            \
62
+       atomic.o                                                         \
62 63
        audio_fifo.o                                                     \
63 64
        avstring.o                                                       \
64 65
        base64.o                                                         \
... ...
@@ -108,6 +109,7 @@ SKIPHEADERS          = old_pix_fmts.h
108 108
 
109 109
 TESTPROGS = adler32                                                     \
110 110
             aes                                                         \
111
+            atomic                                                      \
111 112
             avstring                                                    \
112 113
             base64                                                      \
113 114
             blowfish                                                    \
114 115
new file mode 100644
... ...
@@ -0,0 +1,123 @@
0
+/*
1
+ * Copyright (c) 2012 Ronald S. Bultje <rsbultje@gmail.com>
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "atomic.h"
21
+
22
+#if !HAVE_MEMORYBARRIER && !HAVE_SYNC_SYNCHRONIZE && !HAVE_MACHINE_RW_BARRIER
23
+
24
+#if HAVE_PTHREADS
25
+
26
+#include <pthread.h>
27
+
28
+static pthread_mutex_t atomic_lock = PTHREAD_MUTEX_INITIALIZER;
29
+
30
+int avpriv_atomic_int_get(volatile int *ptr)
31
+{
32
+    int res;
33
+
34
+    pthread_mutex_lock(&atomic_lock);
35
+    res = *ptr;
36
+    pthread_mutex_unlock(&atomic_lock);
37
+
38
+    return res;
39
+}
40
+
41
+void avpriv_atomic_int_set(volatile int *ptr, int val)
42
+{
43
+    pthread_mutex_lock(&atomic_lock);
44
+    *ptr = val;
45
+    pthread_mutex_unlock(&atomic_lock);
46
+}
47
+
48
+int avpriv_atomic_int_add_and_fetch(volatile int *ptr, int inc)
49
+{
50
+    int res;
51
+
52
+    pthread_mutex_lock(&atomic_lock);
53
+    *ptr += inc;
54
+    res = *ptr;
55
+    pthread_mutex_unlock(&atomic_lock);
56
+
57
+    return res;
58
+}
59
+
60
+void *avpriv_atomic_ptr_cas(void * volatile *ptr, void *oldval, void *newval)
61
+{
62
+    void *ret;
63
+    pthread_mutex_lock(&atomic_lock);
64
+    ret = *ptr;
65
+    if (*ptr == oldval)
66
+        *ptr = newval;
67
+    pthread_mutex_unlock(&atomic_lock);
68
+    return ret;
69
+}
70
+
71
+#elif !HAVE_THREADS
72
+
73
+int avpriv_atomic_int_get(volatile int *ptr)
74
+{
75
+    return *ptr;
76
+}
77
+
78
+void avpriv_atomic_int_set(volatile int *ptr, int val)
79
+{
80
+    *ptr = val;
81
+}
82
+
83
+int avpriv_atomic_int_add_and_fetch(volatile int *ptr, int inc)
84
+{
85
+    *ptr += inc;
86
+    return *ptr;
87
+}
88
+
89
+void *avpriv_atomic_ptr_cas(void * volatile *ptr, void *oldval, void *newval)
90
+{
91
+    if (*ptr == oldval) {
92
+        *ptr = newval;
93
+        return oldval;
94
+    }
95
+    return *ptr;
96
+}
97
+
98
+#else
99
+
100
+#error "Threading is enabled, but there is no implementation of atomic operations available"
101
+
102
+#endif /* HAVE_PTHREADS */
103
+
104
+#endif /* !HAVE_MEMORYBARRIER && !HAVE_SYNC_SYNCHRONIZE && !HAVE_MACHINE_RW_BARRIER */
105
+
106
+#ifdef TEST
107
+#include <assert.h>
108
+
109
+int main(void)
110
+{
111
+    volatile int val = 1;
112
+    int res;
113
+
114
+    res = avpriv_atomic_int_add_and_fetch(&val, 1);
115
+    assert(res == 2);
116
+    avpriv_atomic_int_set(&val, 3);
117
+    res = avpriv_atomic_int_get(&val);
118
+    assert(res == 3);
119
+
120
+    return 0;
121
+}
122
+#endif
0 123
new file mode 100644
... ...
@@ -0,0 +1,74 @@
0
+/*
1
+ * Copyright (c) 2012 Ronald S. Bultje <rsbultje@gmail.com>
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#ifndef AVUTIL_ATOMIC_H
21
+#define AVUTIL_ATOMIC_H
22
+
23
+#include "config.h"
24
+
25
+#if HAVE_MEMORYBARRIER
26
+#include "atomic_win32.h"
27
+#elif HAVE_SYNC_SYNCHRONIZE
28
+#include "atomic_gcc.h"
29
+#elif HAVE_MACHINE_RW_BARRIER
30
+#include "atomic_suncc.h"
31
+#else
32
+
33
+/**
34
+ * Load the current value stored in an atomic integer.
35
+ *
36
+ * @param ptr atomic integer
37
+ * @return the current value of the atomic integer
38
+ * @note This acts as a memory barrier.
39
+ */
40
+int avpriv_atomic_int_get(volatile int *ptr);
41
+
42
+/**
43
+ * Store a new value in an atomic integer.
44
+ *
45
+ * @param ptr atomic integer
46
+ * @param val the value to store in the atomic integer
47
+ * @note This acts as a memory barrier.
48
+ */
49
+void avpriv_atomic_int_set(volatile int *ptr, int val);
50
+
51
+/**
52
+ * Add a value to an atomic integer.
53
+ *
54
+ * @param ptr atomic integer
55
+ * @param inc the value to add to the atomic integer (may be negative)
56
+ * @return the new value of the atomic integer.
57
+ * @note This does NOT act as a memory barrier. This is primarily
58
+ *       intended for reference counting.
59
+ */
60
+int avpriv_atomic_int_add_and_fetch(volatile int *ptr, int inc);
61
+
62
+/**
63
+ * Atomic pointer compare and swap.
64
+ *
65
+ * @param ptr pointer to the pointer to operate on
66
+ * @param oldval do the swap if the current value of *ptr equals to oldval
67
+ * @param newval value to replace *ptr with
68
+ * @return the value of *ptr before comparison
69
+ */
70
+void *avpriv_atomic_ptr_cas(void * volatile *ptr, void *oldval, void *newval);
71
+
72
+#endif /* HAVE_MEMORYBARRIER */
73
+#endif /* AVUTIL_ATOMIC_H */
0 74
new file mode 100644
... ...
@@ -0,0 +1,48 @@
0
+/*
1
+ * Copyright (c) 2012 Ronald S. Bultje <rsbultje@gmail.com>
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "atomic.h"
21
+
22
+#define avpriv_atomic_int_get atomic_int_get_gcc
23
+static inline int atomic_int_get_gcc(volatile int *ptr)
24
+{
25
+    __sync_synchronize();
26
+    return *ptr;
27
+}
28
+
29
+#define avpriv_atomic_int_set atomic_int_set_gcc
30
+static inline void atomic_int_set_gcc(volatile int *ptr, int val)
31
+{
32
+    *ptr = val;
33
+    __sync_synchronize();
34
+}
35
+
36
+#define avpriv_atomic_int_add_and_fetch atomic_int_add_and_fetch_gcc
37
+static inline int atomic_int_add_and_fetch_gcc(volatile int *ptr, int inc)
38
+{
39
+    return __sync_add_and_fetch(ptr, inc);
40
+}
41
+
42
+#define avpriv_atomic_ptr_cas atomic_ptr_cas_gcc
43
+static inline void *atomic_ptr_cas_gcc(void * volatile *ptr,
44
+                                       void *oldval, void *newval)
45
+{
46
+    return __sync_val_compare_and_swap(ptr, oldval, newval);
47
+}
0 48
new file mode 100644
... ...
@@ -0,0 +1,51 @@
0
+/*
1
+ *
2
+ * This file is part of FFmpeg.
3
+ *
4
+ * FFmpeg is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2.1 of the License, or (at your option) any later version.
8
+ *
9
+ * FFmpeg is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with FFmpeg; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+ */
18
+
19
+#include <atomic.h>
20
+#include <mbarrier.h>
21
+
22
+#include "atomic.h"
23
+
24
+#define avpriv_atomic_int_get atomic_int_get_suncc
25
+static inline int atomic_int_get_suncc(volatile int *ptr)
26
+{
27
+    __machine_rw_barrier();
28
+    return *ptr;
29
+}
30
+
31
+#define avpriv_atomic_int_set atomic_int_set_suncc
32
+static inline void atomic_int_set_suncc(volatile int *ptr, int val)
33
+{
34
+    *ptr = val;
35
+    __machine_rw_barrier();
36
+}
37
+
38
+#define avpriv_atomic_int_add_and_fetch atomic_int_add_and_fetch_suncc
39
+static inline int atomic_int_add_and_fetch_suncc(volatile int *ptr, int inc)
40
+{
41
+    return atomic_add_int_nv(ptr, inc);
42
+}
43
+
44
+#define avpriv_atomic_ptr_cas atomic_ptr_cas_suncc
45
+static inline void *atomic_ptr_cas_suncc(void * volatile *ptr,
46
+                                         void *oldval, void *newval)
47
+{
48
+    return atomic_cas_ptr(ptr, oldval, newval);
49
+}
50
+
0 51
new file mode 100644
... ...
@@ -0,0 +1,50 @@
0
+/*
1
+ * Copyright (c) 2012 Ronald S. Bultje <rsbultje@gmail.com>
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include <windows.h>
21
+
22
+#include "atomic.h"
23
+
24
+#define avpriv_atomic_int_get atomic_int_get_win32
25
+static inline int atomic_int_get_win32(volatile int *ptr)
26
+{
27
+    MemoryBarrier();
28
+    return *ptr;
29
+}
30
+
31
+#define avpriv_atomic_int_set atomic_int_set_win32
32
+static inline void atomic_int_set_win32(volatile int *ptr, int val)
33
+{
34
+    *ptr = val;
35
+    MemoryBarrier();
36
+}
37
+
38
+#define avpriv_atomic_int_add_and_fetch atomic_int_add_and_fetch_win32
39
+static inline int atomic_int_add_and_fetch_win32(volatile int *ptr, int inc)
40
+{
41
+    return inc + InterlockedExchangeAdd(ptr, inc);
42
+}
43
+
44
+#define avpriv_atomic_ptr_cas atomic_ptr_cas_win32
45
+static inline void *atomic_ptr_cas_win32(void * volatile *ptr,
46
+                                         void *oldval, void *newval)
47
+{
48
+    return InterlockedCompareExchangePointer(ptr, newval, oldval);
49
+}
... ...
@@ -8,6 +8,11 @@ fate-aes: libavutil/aes-test$(EXESUF)
8 8
 fate-aes: CMD = run libavutil/aes-test
9 9
 fate-aes: REF = /dev/null
10 10
 
11
+FATE_LIBAVUTIL += fate-atomic
12
+fate-atomic: libavutil/atomic-test$(EXESUF)
13
+fate-atomic: CMD = run libavutil/atomic-test
14
+fate-atomic: REF = /dev/null
15
+
11 16
 FATE_LIBAVUTIL += fate-avstring
12 17
 fate-avstring: libavutil/avstring-test$(EXESUF)
13 18
 fate-avstring: CMD = run libavutil/avstring-test