]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_seqlock.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * basic test for seqlock
5 * Copyright (C) 2015 David Lamparter
24 static struct seqlock sqlo
;
25 static pthread_t thr1
;
26 static struct timeval start
;
28 static void writestr(const char *str
)
32 int64_t usec
= monotime_since(&start
, NULL
);
34 snprintfrr(buf
, sizeof(buf
), "[%02" PRId64
"] ", usec
/ 100000);
36 iov
[0].iov_base
= buf
;
37 iov
[0].iov_len
= strlen(buf
);
38 iov
[1].iov_base
= (char *)str
;
39 iov
[1].iov_len
= strlen(str
);
43 static void *thr1func(void *arg
)
45 assert(!seqlock_held(&sqlo
));
46 assert(seqlock_check(&sqlo
, 1));
47 seqlock_wait(&sqlo
, 1);
48 writestr("thr1 (unheld)\n");
52 assert(seqlock_held(&sqlo
));
53 assert(seqlock_check(&sqlo
, 1));
54 seqlock_wait(&sqlo
, 1);
55 writestr("thr1 @1\n");
57 seqlock_wait(&sqlo
, 5);
58 writestr("thr1 @5\n");
60 seqlock_wait(&sqlo
, 9);
61 writestr("thr1 @9\n");
63 seqlock_wait(&sqlo
, 13);
64 writestr("thr1 @13\n");
66 seqlock_wait(&sqlo
, 17);
67 writestr("thr1 @17\n");
69 seqlock_wait(&sqlo
, 21);
70 writestr("thr1 @21\n");
74 int main(int argc
, char **argv
)
80 assert(!seqlock_held(&sqlo
));
81 seqlock_acquire_val(&sqlo
, 1);
82 assert(seqlock_held(&sqlo
));
84 assert(seqlock_cur(&sqlo
) == 1);
85 assert(seqlock_bump(&sqlo
) == 1);
86 assert(seqlock_cur(&sqlo
) == 5);
87 assert(seqlock_bump(&sqlo
) == 5);
88 assert(seqlock_bump(&sqlo
) == 9);
89 assert(seqlock_bump(&sqlo
) == 13);
90 assert(seqlock_cur(&sqlo
) == 17);
92 assert(seqlock_held(&sqlo
));
93 seqlock_release(&sqlo
);
94 assert(!seqlock_held(&sqlo
));
96 pthread_create(&thr1
, NULL
, thr1func
, NULL
);
99 writestr("main @5\n");
100 seqlock_acquire_val(&sqlo
, 5);
103 writestr("main @9\n");
107 writestr("main @17\n");
108 seqlock_acquire_val(&sqlo
, 17);
111 writestr("main @release\n");
112 seqlock_release(&sqlo
);