]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_seqlock.c
2 * basic test for seqlock
4 * Copyright (C) 2015 David Lamparter
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)
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
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
32 static struct seqlock sqlo
;
33 static pthread_t thr1
;
34 static struct timeval start
;
36 static void writestr(const char *str
)
40 int64_t usec
= monotime_since(&start
, NULL
);
42 snprintf(buf
, sizeof(buf
), "[%02"PRId64
"] ", usec
/ 100000);
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
);
51 static void *thr1func(void *arg
)
53 assert(!seqlock_held(&sqlo
));
54 assert(seqlock_check(&sqlo
, 1));
55 seqlock_wait(&sqlo
, 1);
56 writestr("thr1 (unheld)\n");
60 assert(seqlock_held(&sqlo
));
61 assert(seqlock_check(&sqlo
, 1));
62 seqlock_wait(&sqlo
, 1);
63 writestr("thr1 @1\n");
65 seqlock_wait(&sqlo
, 3);
66 writestr("thr1 @3\n");
68 seqlock_wait(&sqlo
, 5);
69 writestr("thr1 @5\n");
71 seqlock_wait(&sqlo
, 7);
72 writestr("thr1 @7\n");
74 seqlock_wait(&sqlo
, 9);
75 writestr("thr1 @9\n");
77 seqlock_wait(&sqlo
, 11);
78 writestr("thr1 @11\n");
82 int main(int argc
, char **argv
)
88 assert(!seqlock_held(&sqlo
));
89 seqlock_acquire_val(&sqlo
, 1);
90 assert(seqlock_held(&sqlo
));
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);
100 assert(seqlock_held(&sqlo
));
101 seqlock_release(&sqlo
);
102 assert(!seqlock_held(&sqlo
));
104 pthread_create(&thr1
, NULL
, thr1func
, NULL
);
107 writestr("main @3\n");
108 seqlock_acquire_val(&sqlo
, 3);
111 writestr("main @5\n");
115 writestr("main @9\n");
116 seqlock_acquire_val(&sqlo
, 9);
119 writestr("main @release\n");
120 seqlock_release(&sqlo
);