1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* tests/threads/prof1.c */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright (C) 2004 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert * All rights reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Export of this software from the United States of America may
8*7f2fe78bSCy Schubert * require a specific license from the United States Government.
9*7f2fe78bSCy Schubert * It is the responsibility of any person or organization contemplating
10*7f2fe78bSCy Schubert * export to obtain such a license before exporting.
11*7f2fe78bSCy Schubert *
12*7f2fe78bSCy Schubert * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13*7f2fe78bSCy Schubert * distribute this software and its documentation for any purpose and
14*7f2fe78bSCy Schubert * without fee is hereby granted, provided that the above copyright
15*7f2fe78bSCy Schubert * notice appear in all copies and that both that copyright notice and
16*7f2fe78bSCy Schubert * this permission notice appear in supporting documentation, and that
17*7f2fe78bSCy Schubert * the name of M.I.T. not be used in advertising or publicity pertaining
18*7f2fe78bSCy Schubert * to distribution of the software without specific, written prior
19*7f2fe78bSCy Schubert * permission. Furthermore if you modify this software you must label
20*7f2fe78bSCy Schubert * your software as modified software and not distribute it in such a
21*7f2fe78bSCy Schubert * fashion that it might be confused with the original M.I.T. software.
22*7f2fe78bSCy Schubert * M.I.T. makes no representations about the suitability of
23*7f2fe78bSCy Schubert * this software for any purpose. It is provided "as is" without express
24*7f2fe78bSCy Schubert * or implied warranty.
25*7f2fe78bSCy Schubert */
26*7f2fe78bSCy Schubert
27*7f2fe78bSCy Schubert #include <stdio.h>
28*7f2fe78bSCy Schubert #include <stdlib.h>
29*7f2fe78bSCy Schubert #include <unistd.h>
30*7f2fe78bSCy Schubert #include <pthread.h>
31*7f2fe78bSCy Schubert #include <assert.h>
32*7f2fe78bSCy Schubert #include <sys/types.h>
33*7f2fe78bSCy Schubert #include <time.h>
34*7f2fe78bSCy Schubert #include <sys/time.h>
35*7f2fe78bSCy Schubert #include <utime.h>
36*7f2fe78bSCy Schubert #include <com_err.h>
37*7f2fe78bSCy Schubert #include <profile.h>
38*7f2fe78bSCy Schubert
39*7f2fe78bSCy Schubert int nthreads = 10;
40*7f2fe78bSCy Schubert unsigned int delay = 3600;
41*7f2fe78bSCy Schubert
42*7f2fe78bSCy Schubert volatile int done = 0; /* XXX hack */
43*7f2fe78bSCy Schubert
44*7f2fe78bSCy Schubert const char *path = "/tmp/foo1.conf:/tmp/foo.conf";
45*7f2fe78bSCy Schubert const char *filename = "/tmp/foo.conf";
46*7f2fe78bSCy Schubert
47*7f2fe78bSCy Schubert const char *prog;
48*7f2fe78bSCy Schubert
worker(void * arg)49*7f2fe78bSCy Schubert static void *worker(void *arg)
50*7f2fe78bSCy Schubert {
51*7f2fe78bSCy Schubert profile_t p;
52*7f2fe78bSCy Schubert long err;
53*7f2fe78bSCy Schubert int i;
54*7f2fe78bSCy Schubert const char *const names[] = {
55*7f2fe78bSCy Schubert "one", "two", "three", 0
56*7f2fe78bSCy Schubert };
57*7f2fe78bSCy Schubert char **values;
58*7f2fe78bSCy Schubert const char *mypath = (random() & 1) ? path : filename;
59*7f2fe78bSCy Schubert
60*7f2fe78bSCy Schubert while (!done) {
61*7f2fe78bSCy Schubert err = profile_init_path(mypath, &p);
62*7f2fe78bSCy Schubert if (err) {
63*7f2fe78bSCy Schubert com_err(prog, err, "calling profile_init(\"%s\")", mypath);
64*7f2fe78bSCy Schubert exit(1);
65*7f2fe78bSCy Schubert }
66*7f2fe78bSCy Schubert for (i = 0; i < 10; i++) {
67*7f2fe78bSCy Schubert values = 0;
68*7f2fe78bSCy Schubert err = profile_get_values(p, names, &values);
69*7f2fe78bSCy Schubert if (err == 0 && values != 0)
70*7f2fe78bSCy Schubert profile_free_list(values);
71*7f2fe78bSCy Schubert }
72*7f2fe78bSCy Schubert profile_release(p);
73*7f2fe78bSCy Schubert }
74*7f2fe78bSCy Schubert return 0;
75*7f2fe78bSCy Schubert }
76*7f2fe78bSCy Schubert
modifier(void * arg)77*7f2fe78bSCy Schubert static void *modifier(void *arg)
78*7f2fe78bSCy Schubert {
79*7f2fe78bSCy Schubert struct timespec req;
80*7f2fe78bSCy Schubert while (!done) {
81*7f2fe78bSCy Schubert req.tv_sec = 0;
82*7f2fe78bSCy Schubert req.tv_nsec = random() & 499999999;
83*7f2fe78bSCy Schubert nanosleep(&req, 0);
84*7f2fe78bSCy Schubert utime(filename, 0);
85*7f2fe78bSCy Schubert /* printf("."), fflush(stdout); */
86*7f2fe78bSCy Schubert }
87*7f2fe78bSCy Schubert return 0;
88*7f2fe78bSCy Schubert }
89*7f2fe78bSCy Schubert
main(int argc,char * argv[])90*7f2fe78bSCy Schubert int main(int argc, char *argv[])
91*7f2fe78bSCy Schubert {
92*7f2fe78bSCy Schubert int i;
93*7f2fe78bSCy Schubert pthread_t thr;
94*7f2fe78bSCy Schubert
95*7f2fe78bSCy Schubert prog = argv[0];
96*7f2fe78bSCy Schubert for (i = 0; i < nthreads; i++) {
97*7f2fe78bSCy Schubert assert(0 == pthread_create(&thr, 0, worker, 0));
98*7f2fe78bSCy Schubert }
99*7f2fe78bSCy Schubert sleep(1);
100*7f2fe78bSCy Schubert pthread_create(&thr, 0, modifier, 0);
101*7f2fe78bSCy Schubert sleep(delay);
102*7f2fe78bSCy Schubert done = 1;
103*7f2fe78bSCy Schubert sleep(2);
104*7f2fe78bSCy Schubert return 0;
105*7f2fe78bSCy Schubert }
106