File: seqlock.h

package info (click to toggle)
fio 3.25-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,812 kB
  • sloc: ansic: 69,429; python: 4,155; sh: 4,058; makefile: 749; yacc: 204; lex: 184
file content (46 lines) | stat: -rw-r--r-- 785 bytes parent folder | download
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
#ifndef FIO_SEQLOCK_H
#define FIO_SEQLOCK_H

#include "types.h"
#include "../arch/arch.h"

struct seqlock {
	volatile unsigned int sequence;
};

static inline void seqlock_init(struct seqlock *s)
{
	s->sequence = 0;
}

static inline unsigned int read_seqlock_begin(struct seqlock *s)
{
	unsigned int seq;

	do {
		seq = atomic_load_acquire(&s->sequence);
		if (!(seq & 1))
			break;
		nop;
	} while (1);

	return seq;
}

static inline bool read_seqlock_retry(struct seqlock *s, unsigned int seq)
{
	read_barrier();
	return s->sequence != seq;
}

static inline void write_seqlock_begin(struct seqlock *s)
{
	s->sequence = atomic_load_acquire(&s->sequence) + 1;
}

static inline void write_seqlock_end(struct seqlock *s)
{
	atomic_store_release(&s->sequence, s->sequence + 1);
}

#endif