3
MediaTomb - http://www.mediatomb.cc/
5
atomic.h - this file is part of MediaTomb.
7
Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
8
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
10
Copyright (C) 2006-2007 Gena Batyan <bgeradz@mediatomb.cc>,
11
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
12
Leonhard Wimmer <leo@mediatomb.cc>
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.
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.
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.
27
$Id: atomic.h 1294 2007-05-13 16:28:24Z lww $
35
typedef struct { volatile int x; } mt_atomic_t;
36
//#define ATOMIC_INIT(y) {(y)}
38
static inline void atomic_set(mt_atomic_t *at, int val)
43
static inline int atomic_get(mt_atomic_t *at)
52
#error ATOMIC_X86_SMP and ATOMIC_X86 are defined at the same time!
54
#define ASM_LOCK "lock; "
61
#if defined(ATOMIC_X86_SMP) || defined(ATOMIC_X86)
62
#define ATOMIC_DEFINED
63
static inline void atomic_inc(mt_atomic_t *at)
73
static inline bool atomic_dec(mt_atomic_t *at)
77
ASM_LOCK "decl %0; sete %1"
78
:"=m" (at->x), "=g" (c)
88
#error ATOMIC_X86(_SMP) and ATOMIC_TORTURE are defined at the same time!
90
#define ATOMIC_DEFINED
93
// this is NOT atomic in most cases! Don't use ATOMIC_TORTURE!
94
static inline void atomic_inc(mt_atomic_t *at)
99
static inline bool atomic_dec(mt_atomic_t *at)
101
return ((--at->x) == 0);
105
#ifndef ATOMIC_DEFINED
107
#define ATOMIC_NEED_MUTEX
108
static inline void atomic_inc(mt_atomic_t *at, pthread_mutex_t *mutex)
110
pthread_mutex_lock(mutex);
112
pthread_mutex_unlock(mutex);
114
static inline bool atomic_dec(mt_atomic_t *at, pthread_mutex_t *mutex)
116
pthread_mutex_lock(mutex);
117
int newval = (--at->x);
118
pthread_mutex_unlock(mutex);
119
return (newval == 0);
122
#undef ATOMIC_DEFINED
125
#endif // __ATOMIC_H__