Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4349 Serge 1
/*
2
 * Copyright (c) 2012 Ronald S. Bultje 
3
 *
4
 * This file is part of FFmpeg.
5
 *
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.
10
 *
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.
15
 *
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
19
 */
20
 
21
#include "atomic.h"
22
 
23
#if !HAVE_ATOMICS_NATIVE
24
 
25
#if HAVE_PTHREADS
26
 
27
#include 
28
 
29
static pthread_mutex_t atomic_lock = PTHREAD_MUTEX_INITIALIZER;
30
 
31
int avpriv_atomic_int_get(volatile int *ptr)
32
{
33
    int res;
34
 
35
    pthread_mutex_lock(&atomic_lock);
36
    res = *ptr;
37
    pthread_mutex_unlock(&atomic_lock);
38
 
39
    return res;
40
}
41
 
42
void avpriv_atomic_int_set(volatile int *ptr, int val)
43
{
44
    pthread_mutex_lock(&atomic_lock);
45
    *ptr = val;
46
    pthread_mutex_unlock(&atomic_lock);
47
}
48
 
49
int avpriv_atomic_int_add_and_fetch(volatile int *ptr, int inc)
50
{
51
    int res;
52
 
53
    pthread_mutex_lock(&atomic_lock);
54
    *ptr += inc;
55
    res = *ptr;
56
    pthread_mutex_unlock(&atomic_lock);
57
 
58
    return res;
59
}
60
 
61
void *avpriv_atomic_ptr_cas(void * volatile *ptr, void *oldval, void *newval)
62
{
63
    void *ret;
64
    pthread_mutex_lock(&atomic_lock);
65
    ret = *ptr;
66
    if (*ptr == oldval)
67
        *ptr = newval;
68
    pthread_mutex_unlock(&atomic_lock);
69
    return ret;
70
}
71
 
72
#elif !HAVE_THREADS
73
 
74
int avpriv_atomic_int_get(volatile int *ptr)
75
{
76
    return *ptr;
77
}
78
 
79
void avpriv_atomic_int_set(volatile int *ptr, int val)
80
{
81
    *ptr = val;
82
}
83
 
84
int avpriv_atomic_int_add_and_fetch(volatile int *ptr, int inc)
85
{
86
    *ptr += inc;
87
    return *ptr;
88
}
89
 
90
void *avpriv_atomic_ptr_cas(void * volatile *ptr, void *oldval, void *newval)
91
{
92
    if (*ptr == oldval) {
93
        *ptr = newval;
94
        return oldval;
95
    }
96
    return *ptr;
97
}
98
 
99
#else
100
 
101
#error "Threading is enabled, but there is no implementation of atomic operations available"
102
 
103
#endif /* HAVE_PTHREADS */
104
 
105
#endif /* !HAVE_MEMORYBARRIER && !HAVE_SYNC_VAL_COMPARE_AND_SWAP && !HAVE_MACHINE_RW_BARRIER */
106
 
107
#ifdef TEST
108
#include "avassert.h"
109
 
110
int main(void)
111
{
112
    volatile int val = 1;
113
    int res;
114
 
115
    res = avpriv_atomic_int_add_and_fetch(&val, 1);
116
    av_assert0(res == 2);
117
    avpriv_atomic_int_set(&val, 3);
118
    res = avpriv_atomic_int_get(&val);
119
    av_assert0(res == 3);
120
 
121
    return 0;
122
}
123
#endif