2 * Copyright 2013 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and
30 * documentation are those of the authors and should not be
31 * interpreted as representing official policies, either expressed
32 * or implied, of GRNET S.A.
39 #include <xtypes/xlock.h>
43 volatile int cond = 0;
44 unsigned long sum = 0;
52 void jobfn(void *q, void *arg)
54 unsigned long c = (unsigned long) arg;
55 xlock_acquire(&lock, c);
60 int test1(unsigned long n)
64 struct work *works = malloc(sizeof(struct work) * n);
65 xwaitq_init(&wq, condfn, NULL, 0);
70 for (i = 0; i < n; i++) {
71 works[i].job_fn = jobfn;
75 for (i = 0; i < n; i++) {
76 xwaitq_enqueue(&wq, &works[i]);
84 return ((sum == n)? 0 : -1);
93 void *thread_test(void *arg)
95 struct thread_arg *targ = (struct thread_arg *)arg;
96 unsigned long n = targ->n;
99 struct work *works = malloc(sizeof(struct work) * n);
101 for (i = 0; i < n; i++) {
102 works[i].job_fn = jobfn;
103 works[i].job = targ->num;
106 for (i = 0; i < n; i++) {
107 xwaitq_enqueue(targ->wq, &works[i]);
110 xwaitq_signal(targ->wq);
117 int test2(unsigned long n, unsigned long nr_threads)
121 xwaitq_init(&wq, condfn, NULL, 0);
124 xlock_release(&lock);
126 struct thread_arg *targs = malloc(sizeof(struct thread_arg)*nr_threads * n);
127 pthread_t *threads = malloc(sizeof(pthread_t) * nr_threads);
129 for (i = 0; i < nr_threads; i++) {
134 for (i = 0; i < nr_threads; i++) {
135 r = pthread_create(&threads[i], NULL, thread_test, &targs[i]);
137 fprintf(stderr, "error pthread_create\n");
142 for (i = 0; i < nr_threads; i++) {
143 pthread_join(threads[i], NULL);
152 unsigned long expected_sum = 0;
153 for (i = 0; i < nr_threads; i++) {
154 expected_sum += n*(i+1);
156 return ((sum == expected_sum) ? 0 : -1);
159 int test3(unsigned long n, unsigned long nr_threads)
163 xwaitq_init(&wq, condfn, NULL, XWAIT_SIGNAL_ONE);
166 xlock_release(&lock);
168 struct thread_arg *targs = malloc(sizeof(struct thread_arg)*nr_threads * n);
169 pthread_t *threads = malloc(sizeof(pthread_t) * nr_threads);
171 for (i = 0; i < nr_threads; i++) {
176 for (i = 0; i < nr_threads; i++) {
177 r = pthread_create(&threads[i], NULL, thread_test, &targs[i]);
179 fprintf(stderr, "error pthread_create\n");
184 for (i = 0; i < nr_threads; i++) {
185 pthread_join(threads[i], NULL);
194 unsigned long expected_sum = 0;
195 for (i = 0; i < nr_threads; i++) {
196 expected_sum += n*(i+1);
198 return ((sum == expected_sum) ? 0 : -1);
201 int main(int argc, const char *argv[])
203 struct timeval start, end, tv;
205 int n = atoi(argv[1]);
206 int t = atoi(argv[2]);
208 fprintf(stderr, "Running test1\n");
209 gettimeofday(&start, NULL);
212 fprintf(stderr, "Test1: FAILED\n");
215 gettimeofday(&end, NULL);
216 timersub(&end, &start, &tv);
217 fprintf(stderr, "Test1: PASSED\n");
218 fprintf(stderr, "Test time: %ds %dusec\n\n", (int)tv.tv_sec, (int)tv.tv_usec);
220 fprintf(stderr, "running test2\n");
221 gettimeofday(&start, NULL);
224 fprintf(stderr, "test2: failed\n");
227 gettimeofday(&end, NULL);
228 fprintf(stderr, "test2: passed\n");
229 timersub(&end, &start, &tv);
230 fprintf(stderr, "Test time: %ds %dusec\n\n", (int)tv.tv_sec, (int)tv.tv_usec);
232 fprintf(stderr, "running test3\n");
233 gettimeofday(&start, NULL);
236 fprintf(stderr, "test3: failed\n");
239 gettimeofday(&end, NULL);
240 fprintf(stderr, "test3: passed\n");
241 timersub(&end, &start, &tv);
242 fprintf(stderr, "Test time: %ds %dusec\n\n", (int)tv.tv_sec, (int)tv.tv_usec);