Debian Codegraph @

regressions/ck_brlock/validate/validate.c

raw
1 /*
2 * Copyright 2011-2015 Samy Al Bahra.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <errno.h>
28 #include <inttypes.h>
29 #include <pthread.h>
30 #include <math.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <strings.h>
35 #include <unistd.h>
36 #include <sys/time.h>
37
38 #include <ck_pr.h>
39 #include <ck_brlock.h>
40
41 #include "../../common.h"
42
43 #ifndef ITERATE
44 #define ITERATE 1000000
45 #endif
46
47 static struct affinity a;
48 static unsigned int locked = 0;
49 static int nthr;
50 static ck_brlock_t lock = CK_BRLOCK_INITIALIZER;
51
52 static void *
53 thread(void *null CK_CC_UNUSED)
54 {
55 ck_brlock_reader_t r;
56 int i = ITERATE;
57 unsigned int l;
58
59 if (aff_iterate(&a)) {
60 perror("ERROR: Could not affine thread");
61 exit(EXIT_FAILURE);
62 }
63
64 ck_brlock_read_register(&lock, &r);
65
66 while (i--) {
67 ck_brlock_write_lock(&lock);
68 {
69 l = ck_pr_load_uint(&locked);
70 if (l != 0) {
71 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
72 }
73
74 ck_pr_inc_uint(&locked);
75 ck_pr_inc_uint(&locked);
76 ck_pr_inc_uint(&locked);
77 ck_pr_inc_uint(&locked);
78 ck_pr_inc_uint(&locked);
79 ck_pr_inc_uint(&locked);
80 ck_pr_inc_uint(&locked);
81 ck_pr_inc_uint(&locked);
82
83 l = ck_pr_load_uint(&locked);
84 if (l != 8) {
85 ck_error("ERROR [WR:%d]: %u != 2\n", __LINE__, l);
86 }
87
88 ck_pr_dec_uint(&locked);
89 ck_pr_dec_uint(&locked);
90 ck_pr_dec_uint(&locked);
91 ck_pr_dec_uint(&locked);
92 ck_pr_dec_uint(&locked);
93 ck_pr_dec_uint(&locked);
94 ck_pr_dec_uint(&locked);
95 ck_pr_dec_uint(&locked);
96
97 l = ck_pr_load_uint(&locked);
98 if (l != 0) {
99 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
100 }
101 }
102 ck_brlock_write_unlock(&lock);
103
104 ck_brlock_read_lock(&lock, &r);
105 {
106 l = ck_pr_load_uint(&locked);
107 if (l != 0) {
108 ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l);
109 }
110 }
111 ck_brlock_read_unlock(&r);
112 }
113
114 ck_brlock_read_unregister(&lock, &r);
115 return (NULL);
116 }
117
118 int
119 main(int argc, char *argv[])
120 {
121 pthread_t *threads;
122 int i;
123
124 if (argc != 3) {
125 ck_error("Usage: validate <number of threads> <affinity delta>\n");
126 }
127
128 nthr = atoi(argv[1]);
129 if (nthr <= 0) {
130 ck_error("ERROR: Number of threads must be greater than 0\n");
131 }
132
133 threads = malloc(sizeof(pthread_t) * nthr);
134 if (threads == NULL) {
135 ck_error("ERROR: Could not allocate thread structures\n");
136 }
137
138 a.delta = atoi(argv[2]);
139
140 fprintf(stderr, "Creating threads (mutual exclusion)...");
141 for (i = 0; i < nthr; i++) {
142 if (pthread_create(&threads[i], NULL, thread, NULL)) {
143 ck_error("ERROR: Could not create thread %d\n", i);
144 }
145 }
146 fprintf(stderr, "done\n");
147
148 fprintf(stderr, "Waiting for threads to finish correctness regression...");
149 for (i = 0; i < nthr; i++)
150 pthread_join(threads[i], NULL);
151 fprintf(stderr, "done (passed)\n");
152
153 return (0);
154 }
155