~vanvugt/ubuntu/oneiric/mediatomb/fix-770964-784431

« back to all changes in this revision

Viewing changes to src/zmm/atomic.h

  • Committer: Bazaar Package Importer
  • Author(s): Andres Mejia
  • Date: 2008-02-02 01:42:48 UTC
  • Revision ID: james.westby@ubuntu.com-20080202014248-cjouolddb8gi2zkz
Tags: upstream-0.10.0.dfsg1
ImportĀ upstreamĀ versionĀ 0.10.0.dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*MT*
 
2
    
 
3
    MediaTomb - http://www.mediatomb.cc/
 
4
    
 
5
    atomic.h - this file is part of MediaTomb.
 
6
    
 
7
    Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
 
8
                       Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
 
9
    
 
10
    Copyright (C) 2006-2007 Gena Batyan <bgeradz@mediatomb.cc>,
 
11
                            Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
 
12
                            Leonhard Wimmer <leo@mediatomb.cc>
 
13
    
 
14
    MediaTomb is free software; you can redistribute it and/or modify
 
15
    it under the terms of the GNU General Public License version 2
 
16
    as published by the Free Software Foundation.
 
17
    
 
18
    MediaTomb is distributed in the hope that it will be useful,
 
19
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
    GNU General Public License for more details.
 
22
    
 
23
    You should have received a copy of the GNU General Public License
 
24
    version 2 along with MediaTomb; if not, write to the Free Software
 
25
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 
26
    
 
27
    $Id: atomic.h 1294 2007-05-13 16:28:24Z lww $
 
28
*/
 
29
 
 
30
/// \file atomic.h
 
31
 
 
32
#ifndef __ATOMIC_H__
 
33
#define __ATOMIC_H__
 
34
 
 
35
typedef struct { volatile int x; } mt_atomic_t;
 
36
//#define ATOMIC_INIT(y) {(y)}
 
37
 
 
38
static inline void atomic_set(mt_atomic_t *at, int val)
 
39
{
 
40
    at->x = val;
 
41
}
 
42
 
 
43
static inline int atomic_get(mt_atomic_t *at)
 
44
{
 
45
    return (at->x);
 
46
}
 
47
 
 
48
#undef ATOMIC_DEFINED
 
49
 
 
50
#ifdef ATOMIC_X86_SMP
 
51
    #ifdef ATOMIC_X86
 
52
        #error ATOMIC_X86_SMP and ATOMIC_X86 are defined at the same time!
 
53
    #endif
 
54
    #define ASM_LOCK "lock; "
 
55
#endif
 
56
 
 
57
#ifdef ATOMIC_X86
 
58
    #define ASM_LOCK
 
59
#endif
 
60
 
 
61
#if defined(ATOMIC_X86_SMP) || defined(ATOMIC_X86)
 
62
    #define ATOMIC_DEFINED
 
63
    static inline void atomic_inc(mt_atomic_t *at)
 
64
    {
 
65
        __asm__ __volatile__(
 
66
            ASM_LOCK "incl %0"
 
67
            :"=m" (at->x)
 
68
            :"m" (at->x)
 
69
            :"cc"
 
70
        );
 
71
    }
 
72
    
 
73
    static inline bool atomic_dec(mt_atomic_t *at)
 
74
    {
 
75
        unsigned char c;
 
76
        __asm__ __volatile__(
 
77
            ASM_LOCK "decl %0; sete %1"
 
78
            :"=m" (at->x), "=g" (c)
 
79
            :"m" (at->x)
 
80
            :"cc"
 
81
        );
 
82
        return (c!=0);
 
83
    }
 
84
#endif
 
85
 
 
86
#ifdef ATOMIC_TORTURE
 
87
    #ifdef ATOMIC_DEFINED
 
88
        #error ATOMIC_X86(_SMP) and ATOMIC_TORTURE are defined at the same time!
 
89
    #else
 
90
        #define ATOMIC_DEFINED
 
91
    #endif
 
92
 
 
93
    // this is NOT atomic in most cases! Don't use ATOMIC_TORTURE!
 
94
    static inline void atomic_inc(mt_atomic_t *at)
 
95
    {
 
96
        at->x++;
 
97
    }
 
98
    
 
99
    static inline bool atomic_dec(mt_atomic_t *at)
 
100
    {
 
101
        return ((--at->x) == 0);
 
102
    }
 
103
#endif
 
104
 
 
105
#ifndef ATOMIC_DEFINED
 
106
    #include <pthread.h>
 
107
    #define ATOMIC_NEED_MUTEX
 
108
    static inline void atomic_inc(mt_atomic_t *at, pthread_mutex_t *mutex)
 
109
    {
 
110
        pthread_mutex_lock(mutex);
 
111
        at->x++;
 
112
        pthread_mutex_unlock(mutex);
 
113
    }
 
114
    static inline bool atomic_dec(mt_atomic_t *at, pthread_mutex_t *mutex)
 
115
    {
 
116
        pthread_mutex_lock(mutex);
 
117
        int newval = (--at->x);
 
118
        pthread_mutex_unlock(mutex);
 
119
        return (newval == 0);
 
120
    }
 
121
#else
 
122
    #undef ATOMIC_DEFINED
 
123
#endif
 
124
 
 
125
#endif // __ATOMIC_H__