]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_seqlock.c
bgpd: Do not send next-hop as :: in MP_REACH_NLRI if no link-local exists
[mirror_frr.git] / tests / lib / test_seqlock.c
1 /*
2 * basic test for seqlock
3 *
4 * Copyright (C) 2015 David Lamparter
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 * Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <inttypes.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <assert.h>
27 #include <sys/uio.h>
28
29 #include "monotime.h"
30 #include "seqlock.h"
31
32 static struct seqlock sqlo;
33 static pthread_t thr1;
34 static struct timeval start;
35
36 static void writestr(const char *str)
37 {
38 struct iovec iov[2];
39 char buf[32];
40 int64_t usec = monotime_since(&start, NULL);
41
42 snprintf(buf, sizeof(buf), "[%02"PRId64"] ", usec / 100000);
43
44 iov[0].iov_base = buf;
45 iov[0].iov_len = strlen(buf);
46 iov[1].iov_base = (char *)str;
47 iov[1].iov_len = strlen(str);
48 writev(1, iov, 2);
49 }
50
51 static void *thr1func(void *arg)
52 {
53 assert(!seqlock_held(&sqlo));
54 assert(seqlock_check(&sqlo, 1));
55 seqlock_wait(&sqlo, 1);
56 writestr("thr1 (unheld)\n");
57
58 sleep(2);
59
60 assert(seqlock_held(&sqlo));
61 assert(seqlock_check(&sqlo, 1));
62 seqlock_wait(&sqlo, 1);
63 writestr("thr1 @1\n");
64
65 seqlock_wait(&sqlo, 3);
66 writestr("thr1 @3\n");
67
68 seqlock_wait(&sqlo, 5);
69 writestr("thr1 @5\n");
70
71 seqlock_wait(&sqlo, 7);
72 writestr("thr1 @7\n");
73
74 seqlock_wait(&sqlo, 9);
75 writestr("thr1 @9\n");
76
77 seqlock_wait(&sqlo, 11);
78 writestr("thr1 @11\n");
79 return NULL;
80 }
81
82 int main(int argc, char **argv)
83 {
84 monotime(&start);
85
86 seqlock_init(&sqlo);
87
88 assert(!seqlock_held(&sqlo));
89 seqlock_acquire_val(&sqlo, 1);
90 assert(seqlock_held(&sqlo));
91
92 assert(seqlock_cur(&sqlo) == 1);
93 assert(seqlock_bump(&sqlo) == 1);
94 assert(seqlock_cur(&sqlo) == 3);
95 assert(seqlock_bump(&sqlo) == 3);
96 assert(seqlock_bump(&sqlo) == 5);
97 assert(seqlock_bump(&sqlo) == 7);
98 assert(seqlock_cur(&sqlo) == 9);
99
100 assert(seqlock_held(&sqlo));
101 seqlock_release(&sqlo);
102 assert(!seqlock_held(&sqlo));
103
104 pthread_create(&thr1, NULL, thr1func, NULL);
105 sleep(1);
106
107 writestr("main @3\n");
108 seqlock_acquire_val(&sqlo, 3);
109 sleep(2);
110
111 writestr("main @5\n");
112 seqlock_bump(&sqlo);
113 sleep(1);
114
115 writestr("main @9\n");
116 seqlock_acquire_val(&sqlo, 9);
117 sleep(1);
118
119 writestr("main @release\n");
120 seqlock_release(&sqlo);
121 sleep(1);
122 }