summaryrefslogtreecommitdiff
path: root/src/wsbm_atomic.h
blob: 7be2da9f03af4148f7e35d0c2533900fe325a31f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
 * This file is under no copyright claims due to its
 * simplicity.
 */

#ifndef _WSBM_ATOMIC_H_
#define _WSBM_ATOMIC_H_

#include <stdint.h>

struct _WsbmAtomic
{
    int32_t count;
};

#define wsbmAtomicInit(_i) {(i)}
#define wsbmAtomicSet(_v, _i) (((_v)->count) = (_i))
#define wsbmAtomicRead(_v) ((_v)->count)

static inline int
wsbmAtomicIncZero(struct _WsbmAtomic *v)
{
    unsigned char c;
    __asm__ __volatile__("lock; incl %0; sete %1":"+m"(v->count), "=qm"(c)
			 ::"memory");

    return c != 0;
}

static inline int
wsbmAtomicDecNegative(struct _WsbmAtomic *v)
{
    unsigned char c;
    int i = -1;
    __asm__ __volatile__("lock; addl %2,%0; sets %1":"+m"(v->count), "=qm"(c)
			 :"ir"(i):"memory");

    return c;
}

static inline int
wsbmAtomicDecZero(struct _WsbmAtomic *v)
{
    unsigned char c;

    __asm__ __volatile__("lock; decl %0; sete %1":"+m"(v->count), "=qm"(c)
			 ::"memory");

    return c != 0;
}

static inline void
wsbmAtomicInc(struct _WsbmAtomic *v)
{
    __asm__ __volatile__("lock; incl %0":"+m"(v->count));
}

static inline void
wsbmAtomicDec(struct _WsbmAtomic *v)
{
    __asm__ __volatile__("lock; decl %0":"+m"(v->count));
}

static inline int32_t
wsbmAtomicCmpXchg(volatile struct _WsbmAtomic *v, int32_t old, int32_t new)
{
    int32_t previous;

    __asm__ __volatile__("lock; cmpxchgl %k1,%2":"=a"(previous)
			 :"r"(new), "m"(v->count), "0"(old)
			 :"memory");

    return previous;
}

#endif