]>
Commit | Line | Data |
---|---|---|
440d5faa DL |
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 | ||
2618a52e DL |
21 | #ifdef HAVE_CONFIG_H |
22 | #include "config.h" | |
23 | #endif | |
24 | ||
440d5faa DL |
25 | #include <stdio.h> |
26 | #include <stdint.h> | |
27 | #include <inttypes.h> | |
28 | #include <string.h> | |
29 | #include <unistd.h> | |
30 | #include <assert.h> | |
31 | #include <sys/uio.h> | |
32 | ||
33 | #include "monotime.h" | |
34 | #include "seqlock.h" | |
8dff30f8 | 35 | #include "printfrr.h" |
440d5faa DL |
36 | |
37 | static struct seqlock sqlo; | |
38 | static pthread_t thr1; | |
39 | static struct timeval start; | |
40 | ||
41 | static void writestr(const char *str) | |
42 | { | |
43 | struct iovec iov[2]; | |
44 | char buf[32]; | |
45 | int64_t usec = monotime_since(&start, NULL); | |
46 | ||
8dff30f8 | 47 | snprintfrr(buf, sizeof(buf), "[%02" PRId64 "] ", usec / 100000); |
440d5faa DL |
48 | |
49 | iov[0].iov_base = buf; | |
50 | iov[0].iov_len = strlen(buf); | |
51 | iov[1].iov_base = (char *)str; | |
52 | iov[1].iov_len = strlen(str); | |
53 | writev(1, iov, 2); | |
54 | } | |
55 | ||
56 | static void *thr1func(void *arg) | |
57 | { | |
58 | assert(!seqlock_held(&sqlo)); | |
59 | assert(seqlock_check(&sqlo, 1)); | |
60 | seqlock_wait(&sqlo, 1); | |
61 | writestr("thr1 (unheld)\n"); | |
62 | ||
63 | sleep(2); | |
64 | ||
65 | assert(seqlock_held(&sqlo)); | |
66 | assert(seqlock_check(&sqlo, 1)); | |
67 | seqlock_wait(&sqlo, 1); | |
68 | writestr("thr1 @1\n"); | |
69 | ||
440d5faa DL |
70 | seqlock_wait(&sqlo, 5); |
71 | writestr("thr1 @5\n"); | |
72 | ||
440d5faa DL |
73 | seqlock_wait(&sqlo, 9); |
74 | writestr("thr1 @9\n"); | |
75 | ||
6046b690 DL |
76 | seqlock_wait(&sqlo, 13); |
77 | writestr("thr1 @13\n"); | |
78 | ||
79 | seqlock_wait(&sqlo, 17); | |
80 | writestr("thr1 @17\n"); | |
81 | ||
82 | seqlock_wait(&sqlo, 21); | |
83 | writestr("thr1 @21\n"); | |
440d5faa DL |
84 | return NULL; |
85 | } | |
86 | ||
87 | int main(int argc, char **argv) | |
88 | { | |
89 | monotime(&start); | |
90 | ||
91 | seqlock_init(&sqlo); | |
92 | ||
93 | assert(!seqlock_held(&sqlo)); | |
94 | seqlock_acquire_val(&sqlo, 1); | |
95 | assert(seqlock_held(&sqlo)); | |
96 | ||
97 | assert(seqlock_cur(&sqlo) == 1); | |
98 | assert(seqlock_bump(&sqlo) == 1); | |
6046b690 | 99 | assert(seqlock_cur(&sqlo) == 5); |
440d5faa | 100 | assert(seqlock_bump(&sqlo) == 5); |
6046b690 DL |
101 | assert(seqlock_bump(&sqlo) == 9); |
102 | assert(seqlock_bump(&sqlo) == 13); | |
103 | assert(seqlock_cur(&sqlo) == 17); | |
440d5faa DL |
104 | |
105 | assert(seqlock_held(&sqlo)); | |
106 | seqlock_release(&sqlo); | |
107 | assert(!seqlock_held(&sqlo)); | |
108 | ||
109 | pthread_create(&thr1, NULL, thr1func, NULL); | |
110 | sleep(1); | |
111 | ||
6046b690 DL |
112 | writestr("main @5\n"); |
113 | seqlock_acquire_val(&sqlo, 5); | |
440d5faa DL |
114 | sleep(2); |
115 | ||
6046b690 | 116 | writestr("main @9\n"); |
440d5faa DL |
117 | seqlock_bump(&sqlo); |
118 | sleep(1); | |
119 | ||
6046b690 DL |
120 | writestr("main @17\n"); |
121 | seqlock_acquire_val(&sqlo, 17); | |
440d5faa DL |
122 | sleep(1); |
123 | ||
124 | writestr("main @release\n"); | |
125 | seqlock_release(&sqlo); | |
126 | sleep(1); | |
127 | } |