1/*
2 * Copyright (c) 2009 Mark Heily <mark@heily.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include "../config.h"
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <unistd.h>
22#include <err.h>
23
24#include <pthread.h>
25#include <sys/event.h>
26
27/* Number of threads to create */
28static const int nthreads = 64;
29
30/* Number of iterations performed by each thread */
31static const int nrounds = 1000000;
32
33//pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
34
35void
36test_kqueue_conc(void)
37{
38    int i, fd;
39
40    for (i = 0; i < 256; i++) {
41        fd = kqueue();
42        if (i < 0)
43            err(1, "kqueue");
44        close(fd);
45    }
46}
47
48void *
49test_harness(void *arg)
50{
51    int id = (long) arg;
52    int i;
53    int kqfd;
54
55    kqfd = kqueue();
56    if (kqfd < 0)
57        err(1, "kqueue");
58
59    printf("thread %d runs %d\n", id, id % 4);
60
61    //test_kqueue_conc();
62    for (i = 0; i < nrounds; i++) {
63        switch (id % 4) {
64            case 0: test_evfilt_user(kqfd);
65                    break;
66            case 1: test_evfilt_read(kqfd);
67                    break;
68            case 2: test_evfilt_timer(kqfd);
69                    break;
70            case 3: test_evfilt_vnode(kqfd);
71                    break;
72        }
73        printf("thread %d round %d / %d\n", id, i, nrounds);
74    }
75    printf("thread %d done\n", id);
76}
77
78int
79main(int argc, char **argv)
80{
81    pthread_t tid[nthreads];
82    long i;
83
84    for (i=0; i<nthreads; i++) {
85        if (pthread_create(&tid[i], NULL, test_harness, (void *)i) != 0)
86            err(1, "pthread_create");
87    }
88    for (i=0; i<nthreads; i++) {
89        if (pthread_join(tid[i], NULL) != 0)
90            err(1, "pthread_join");
91    }
92    printf("\n---\n+OK All tests completed.\n");
93    exit (0);
94}
95