Debian Codegraph @

regressions/ck_tflock/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_tflock.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;
49 static int nthr;
50 static ck_tflock_ticket_t lock = CK_TFLOCK_TICKET_INITIALIZER;
51
52 static void *
53 thread(void *null CK_CC_UNUSED)
54 {
55 unsigned int i = ITERATE;
56 unsigned int l;
57
58 if (aff_iterate(&a)) {
59 perror("ERROR: Could not affine thread");
60 exit(EXIT_FAILURE);
61 }
62
63 while (i--) {
64 ck_tflock_ticket_write_lock(&lock);
65 {
66 l = ck_pr_load_uint(&locked);
67 if (l != 0) {
68 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
69 }
70
71 ck_pr_inc_uint(&locked);
72 ck_pr_inc_uint(&locked);
73 ck_pr_inc_uint(&locked);
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
80 l = ck_pr_load_uint(&locked);
81 if (l != 8) {
82 ck_error("ERROR [WR:%d]: %u != 2\n", __LINE__, l);
83 }
84
85 ck_pr_dec_uint(&locked);
86 ck_pr_dec_uint(&locked);
87 ck_pr_dec_uint(&locked);
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
94 l = ck_pr_load_uint(&locked);
95 if (l != 0) {
96 ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
97 }
98 }
99 ck_tflock_ticket_write_unlock(&lock);
100
101 ck_tflock_ticket_read_lock(&lock);
102 {
103 l = ck_pr_load_uint(&locked);
104 if (l != 0) {
105 ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l);
106 }
107 }
108 ck_tflock_ticket_read_unlock(&lock);
109 }
110
111 return (NULL);
112 }
113
114 static void
115 tflock_ticket_test(pthread_t *threads, void *(*f)(void *), const char *test)
116 {
117 int i;
118
119 fprintf(stderr, "Creating threads (%s)...", test);
120 for (i = 0; i < nthr; i++) {
121 if (pthread_create(&threads[i], NULL, f, NULL)) {
122 ck_error("ERROR: Could not create thread %d\n", i);
123 }
124 }
125 fprintf(stderr, ".");
126
127 for (i = 0; i < nthr; i++)
128 pthread_join(threads[i], NULL);
129 fprintf(stderr, "done (passed)\n");
130 return;
131 }
132
133 int
134 main(int argc, char *argv[])
135 {
136 pthread_t *threads;
137
138 if (argc != 3) {
139 ck_error("Usage: validate <number of threads> <affinity delta>\n");
140 }
141
142 nthr = atoi(argv[1]);
143 if (nthr <= 0) {
144 ck_error("ERROR: Number of threads must be greater than 0\n");
145 }
146
147 threads = malloc(sizeof(pthread_t) * nthr);
148 if (threads == NULL) {
149 ck_error("ERROR: Could not allocate thread structures\n");
150 }
151
152 a.delta = atoi(argv[2]);
153
154 tflock_ticket_test(threads, thread, "regular");
155 ck_tflock_ticket_init(&lock);
156 return 0;
157 }
158