Debian Codegraph @

regressions/ck_barrier/validate/barrier_mcs.c

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