]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_seqlock.c
Merge pull request #12816 from gpnaveen/stc_rte_err_msg
[mirror_frr.git] / tests / lib / test_seqlock.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * basic test for seqlock
4 *
5 * Copyright (C) 2015 David Lamparter
6 */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <inttypes.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <assert.h>
18 #include <sys/uio.h>
19
20 #include "monotime.h"
21 #include "seqlock.h"
22 #include "printfrr.h"
23
24 static struct seqlock sqlo;
25 static pthread_t thr1;
26 static struct timeval start;
27
28 static void writestr(const char *str)
29 {
30 struct iovec iov[2];
31 char buf[32];
32 int64_t usec = monotime_since(&start, NULL);
33
34 snprintfrr(buf, sizeof(buf), "[%02" PRId64 "] ", usec / 100000);
35
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);
40 writev(1, iov, 2);
41 }
42
43 static void *thr1func(void *arg)
44 {
45 assert(!seqlock_held(&sqlo));
46 assert(seqlock_check(&sqlo, 1));
47 seqlock_wait(&sqlo, 1);
48 writestr("thr1 (unheld)\n");
49
50 sleep(2);
51
52 assert(seqlock_held(&sqlo));
53 assert(seqlock_check(&sqlo, 1));
54 seqlock_wait(&sqlo, 1);
55 writestr("thr1 @1\n");
56
57 seqlock_wait(&sqlo, 5);
58 writestr("thr1 @5\n");
59
60 seqlock_wait(&sqlo, 9);
61 writestr("thr1 @9\n");
62
63 seqlock_wait(&sqlo, 13);
64 writestr("thr1 @13\n");
65
66 seqlock_wait(&sqlo, 17);
67 writestr("thr1 @17\n");
68
69 seqlock_wait(&sqlo, 21);
70 writestr("thr1 @21\n");
71 return NULL;
72 }
73
74 int main(int argc, char **argv)
75 {
76 monotime(&start);
77
78 seqlock_init(&sqlo);
79
80 assert(!seqlock_held(&sqlo));
81 seqlock_acquire_val(&sqlo, 1);
82 assert(seqlock_held(&sqlo));
83
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);
91
92 assert(seqlock_held(&sqlo));
93 seqlock_release(&sqlo);
94 assert(!seqlock_held(&sqlo));
95
96 pthread_create(&thr1, NULL, thr1func, NULL);
97 sleep(1);
98
99 writestr("main @5\n");
100 seqlock_acquire_val(&sqlo, 5);
101 sleep(2);
102
103 writestr("main @9\n");
104 seqlock_bump(&sqlo);
105 sleep(1);
106
107 writestr("main @17\n");
108 seqlock_acquire_val(&sqlo, 17);
109 sleep(1);
110
111 writestr("main @release\n");
112 seqlock_release(&sqlo);
113 sleep(1);
114 }