~ubuntu-branches/ubuntu/utopic/glame/utopic

« back to all changes in this revision

Viewing changes to src/include/atomic_x86.h

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2002-04-09 17:14:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020409171412-jzpnov7mbz2w6zsr
Tags: upstream-0.6.2
ImportĀ upstreamĀ versionĀ 0.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef _ATOMIC_X86_H
 
2
#define _ATOMIC_X86_H
 
3
 
 
4
/*
 
5
 * $Id: atomic_x86.h,v 1.8 2001/04/11 08:39:53 richi Exp $
 
6
 *
 
7
 * This code was taken from linux/include/asm-i386/atomic.h from the
 
8
 * Linux kernel source code and adapted to the need of GLAME.
 
9
 * The Linux kernel is
 
10
 * Copyright (C) by Linus Torvalds and others
 
11
 *
 
12
 * This program is free software; you can redistribute it and/or modify
 
13
 * it under the terms of the GNU General Public License as published by
 
14
 * the Free Software Foundation; either version 2 of the License, or
 
15
 * (at your option) any later version.
 
16
 *
 
17
 * This program is distributed in the hope that it will be useful,
 
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
 * GNU General Public License for more details.
 
21
 *
 
22
 * You should have received a copy of the GNU General Public License
 
23
 * along with this program; if not, write to the Free Software
 
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
25
 *
 
26
 */
 
27
 
 
28
/*
 
29
 * Atomic operations that C can't guarantee us.  Useful for
 
30
 * resource counting etc..
 
31
 */
 
32
 
 
33
/*
 
34
 * Make sure gcc doesn't try to be clever and move things around
 
35
 * on us. We need to use _exactly_ the address the user gave us,
 
36
 * not some alias that contains the same information.
 
37
 */
 
38
typedef struct { volatile int counter; } glame_atomic_t;
 
39
 
 
40
#define ATOMIC_INIT(a, val) do { (a).counter = (val); } while(0)
 
41
 
 
42
#define ATOMIC_RELEASE(a)
 
43
 
 
44
#define ATOMIC_VAL(a)           ((a).counter)
 
45
 
 
46
#define atomic_read(v)          ((v)->counter)
 
47
#define atomic_set(v,i)         (((v)->counter) = (i))
 
48
 
 
49
static inline void atomic_add(int i, volatile glame_atomic_t *v)
 
50
{
 
51
        __asm__ __volatile__(
 
52
                "lock ; addl %1,%0"
 
53
                :"=m" (v->counter)
 
54
                :"ir" (i), "m" (v->counter));
 
55
}
 
56
 
 
57
static inline void atomic_sub(int i, volatile glame_atomic_t *v)
 
58
{
 
59
        __asm__ __volatile__(
 
60
                "lock ; subl %1,%0"
 
61
                :"=m" (v->counter)
 
62
                :"ir" (i), "m" (v->counter));
 
63
}
 
64
 
 
65
static inline void atomic_inc(volatile glame_atomic_t *v)
 
66
{
 
67
        __asm__ __volatile__(
 
68
                "lock ; incl %0"
 
69
                :"=m" (v->counter)
 
70
                :"m" (v->counter));
 
71
}
 
72
 
 
73
static inline void atomic_dec(volatile glame_atomic_t *v)
 
74
{
 
75
        __asm__ __volatile__(
 
76
                "lock ; decl %0"
 
77
                :"=m" (v->counter)
 
78
                :"m" (v->counter));
 
79
}
 
80
 
 
81
static inline int atomic_dec_and_test(volatile glame_atomic_t *v)
 
82
{
 
83
        unsigned char c;
 
84
 
 
85
        __asm__ __volatile__(
 
86
                "lock ; decl %0; sete %1"
 
87
                :"=m" (v->counter), "=qm" (c)
 
88
                :"m" (v->counter) : "memory");
 
89
        return c != 0;
 
90
}
 
91
 
 
92
 
 
93
#endif