summaryrefslogtreecommitdiff
path: root/sys/dev/pci/drm/include/linux/seqlock.h
blob: 7e3ba8dc13626b289079887ff7ce6b50e5039c54 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Public domain. */

#ifndef _LINUX_SEQLOCK_H
#define _LINUX_SEQLOCK_H

#include <sys/types.h>
#include <sys/mutex.h>
#include <sys/atomic.h>
#include <linux/lockdep.h>
#include <linux/processor.h>
#include <linux/preempt.h>
#include <linux/compiler.h>

typedef struct {
	unsigned int sequence;
} seqcount_t;

static inline void
__seqcount_init(seqcount_t *s, const char *name,
    struct lock_class_key *key)
{
	s->sequence = 0;
}

static inline unsigned int
__read_seqcount_begin(const seqcount_t *s)
{
	unsigned int r;
	for (;;) {
		r = s->sequence;
		if ((r & 1) == 0)
			break;
		cpu_relax();
	}
	return r;
}

static inline unsigned int
read_seqcount_begin(const seqcount_t *s)
{
	unsigned int r = __read_seqcount_begin(s);
	membar_consumer();
	return r;
}

static inline int
__read_seqcount_retry(const seqcount_t *s, unsigned start)
{
	return (s->sequence != start);
}

static inline int
read_seqcount_retry(const seqcount_t *s, unsigned start)
{
	membar_consumer();
	return __read_seqcount_retry(s, start);
}

static inline void
write_seqcount_begin(seqcount_t *s)
{
	s->sequence++;
	membar_producer();
}

static inline void
write_seqcount_end(seqcount_t *s)
{
	membar_producer();
	s->sequence++;
}

static inline unsigned int
raw_read_seqcount(const seqcount_t *s)
{
	unsigned int r = s->sequence;
	membar_consumer();
	return r;
}

typedef struct {
	unsigned int seq;
	struct mutex lock;
} seqlock_t;

static inline void
seqlock_init(seqlock_t *sl, int wantipl)
{ 
	sl->seq = 0;
	mtx_init(&sl->lock, wantipl);
}

static inline void
write_seqlock(seqlock_t *sl)
{
	mtx_enter(&sl->lock);
	sl->seq++;
	membar_producer();
}

static inline void
write_seqlock_irqsave(seqlock_t *sl, __unused long flags)
{
	mtx_enter(&sl->lock);
	sl->seq++;
	membar_producer();
}

static inline void
write_sequnlock(seqlock_t *sl)
{
	membar_producer();
	sl->seq++;
	mtx_leave(&sl->lock);
}

static inline void
write_sequnlock_irqrestore(seqlock_t *sl, __unused long flags)
{
	membar_producer();
	sl->seq++;
	mtx_leave(&sl->lock);
}

static inline unsigned int
read_seqbegin(seqlock_t *sl)
{
	return READ_ONCE(sl->seq);
}

static inline unsigned int
read_seqretry(seqlock_t *sl, unsigned int pos)
{
	return sl->seq != pos;
}

#endif