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