]> git.proxmox.com Git - mirror_frr.git/blame - tests/lib/test_seqlock.c
*: auto-convert to SPDX License IDs
[mirror_frr.git] / tests / lib / test_seqlock.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
440d5faa
DL
2/*
3 * basic test for seqlock
4 *
5 * Copyright (C) 2015 David Lamparter
6 *
440d5faa
DL
7 * You should have received a copy of the GNU General Public License along with
8 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
9 * Place, Suite 330, Boston, MA 02111-1307 USA
10 */
11
2618a52e
DL
12#ifdef HAVE_CONFIG_H
13#include "config.h"
14#endif
15
440d5faa
DL
16#include <stdio.h>
17#include <stdint.h>
18#include <inttypes.h>
19#include <string.h>
20#include <unistd.h>
21#include <assert.h>
22#include <sys/uio.h>
23
24#include "monotime.h"
25#include "seqlock.h"
8dff30f8 26#include "printfrr.h"
440d5faa
DL
27
28static struct seqlock sqlo;
29static pthread_t thr1;
30static struct timeval start;
31
32static void writestr(const char *str)
33{
34 struct iovec iov[2];
35 char buf[32];
36 int64_t usec = monotime_since(&start, NULL);
37
8dff30f8 38 snprintfrr(buf, sizeof(buf), "[%02" PRId64 "] ", usec / 100000);
440d5faa
DL
39
40 iov[0].iov_base = buf;
41 iov[0].iov_len = strlen(buf);
42 iov[1].iov_base = (char *)str;
43 iov[1].iov_len = strlen(str);
44 writev(1, iov, 2);
45}
46
47static void *thr1func(void *arg)
48{
49 assert(!seqlock_held(&sqlo));
50 assert(seqlock_check(&sqlo, 1));
51 seqlock_wait(&sqlo, 1);
52 writestr("thr1 (unheld)\n");
53
54 sleep(2);
55
56 assert(seqlock_held(&sqlo));
57 assert(seqlock_check(&sqlo, 1));
58 seqlock_wait(&sqlo, 1);
59 writestr("thr1 @1\n");
60
440d5faa
DL
61 seqlock_wait(&sqlo, 5);
62 writestr("thr1 @5\n");
63
440d5faa
DL
64 seqlock_wait(&sqlo, 9);
65 writestr("thr1 @9\n");
66
6046b690
DL
67 seqlock_wait(&sqlo, 13);
68 writestr("thr1 @13\n");
69
70 seqlock_wait(&sqlo, 17);
71 writestr("thr1 @17\n");
72
73 seqlock_wait(&sqlo, 21);
74 writestr("thr1 @21\n");
440d5faa
DL
75 return NULL;
76}
77
78int main(int argc, char **argv)
79{
80 monotime(&start);
81
82 seqlock_init(&sqlo);
83
84 assert(!seqlock_held(&sqlo));
85 seqlock_acquire_val(&sqlo, 1);
86 assert(seqlock_held(&sqlo));
87
88 assert(seqlock_cur(&sqlo) == 1);
89 assert(seqlock_bump(&sqlo) == 1);
6046b690 90 assert(seqlock_cur(&sqlo) == 5);
440d5faa 91 assert(seqlock_bump(&sqlo) == 5);
6046b690
DL
92 assert(seqlock_bump(&sqlo) == 9);
93 assert(seqlock_bump(&sqlo) == 13);
94 assert(seqlock_cur(&sqlo) == 17);
440d5faa
DL
95
96 assert(seqlock_held(&sqlo));
97 seqlock_release(&sqlo);
98 assert(!seqlock_held(&sqlo));
99
100 pthread_create(&thr1, NULL, thr1func, NULL);
101 sleep(1);
102
6046b690
DL
103 writestr("main @5\n");
104 seqlock_acquire_val(&sqlo, 5);
440d5faa
DL
105 sleep(2);
106
6046b690 107 writestr("main @9\n");
440d5faa
DL
108 seqlock_bump(&sqlo);
109 sleep(1);
110
6046b690
DL
111 writestr("main @17\n");
112 seqlock_acquire_val(&sqlo, 17);
440d5faa
DL
113 sleep(1);
114
115 writestr("main @release\n");
116 seqlock_release(&sqlo);
117 sleep(1);
118}