]>
git.saurik.com Git - apple/libc.git/blob - tests/netbsd_getenv_thread.c
1 /* $NetBSD: t_getenv_thread.c,v 1.2 2012/03/15 02:02:23 joerg Exp $ */
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_getenv_thread.c,v 1.2 2012/03/15 02:02:23 joerg Exp $");
34 #include <darwintest.h>
43 #define THREADED_NUM_THREADS 8
44 #define THREADED_NUM_VARS 16
45 #define THREADED_VAR_NAME "THREADED%zu"
46 #define THREADED_RUN_TIME 10
48 static void *thread_putenv(void *);
49 static void *thread_setenv(void *);
50 static void *thread_unsetenv(void *);
53 thread_putenv(void *arg
)
57 static char vars
[THREADED_NUM_VARS
][128];
59 for (i
= 0; i
< THREADED_NUM_VARS
; i
++) {
60 (void)snprintf(vars
[i
], sizeof(vars
[i
]),
61 THREADED_VAR_NAME
"=putenv %ld", i
, lrand48());
64 endtime
= *(time_t *)arg
;
68 i
= lrand48() % THREADED_NUM_VARS
;
69 (void)strlcpy(name
, vars
[i
], sizeof(name
));
70 *strchr(name
, '=') = '\0';
72 T_QUIET
; T_ASSERT_POSIX_ZERO(unsetenv(name
), NULL
);
73 T_QUIET
; T_ASSERT_POSIX_ZERO(putenv(vars
[i
]), NULL
);
74 } while (time(NULL
) < endtime
);
80 thread_setenv(void *arg
)
84 endtime
= *(time_t *)arg
;
87 char name
[32], value
[64];
89 i
= lrand48() % THREADED_NUM_VARS
;
90 (void)snprintf(name
, sizeof(name
), THREADED_VAR_NAME
, i
);
91 (void)snprintf(value
, sizeof(value
), "setenv %ld", lrand48());
93 T_QUIET
; T_ASSERT_POSIX_ZERO(setenv(name
, value
, 1), NULL
);
94 } while (time(NULL
) < endtime
);
100 thread_unsetenv(void *arg
)
104 endtime
= *(time_t *)arg
;
109 i
= lrand48() % THREADED_NUM_VARS
;
110 (void)snprintf(name
, sizeof(name
), THREADED_VAR_NAME
, i
);
112 T_QUIET
; T_ASSERT_POSIX_ZERO(unsetenv(name
), NULL
);
113 } while (time(NULL
) < endtime
);
118 T_DECL(putenv_thread
, "Test concurrent access by putenv(3)")
120 pthread_t threads
[THREADED_NUM_THREADS
];
121 time_t endtime
= time(NULL
) + THREADED_RUN_TIME
;
123 for (int i
= 0; i
< THREADED_NUM_THREADS
; i
++) {
124 T_QUIET
; T_ASSERT_POSIX_ZERO(
125 pthread_create(&threads
[i
], NULL
, thread_putenv
, &endtime
),
128 for (int i
= 0; i
< THREADED_NUM_THREADS
; i
++) {
129 T_QUIET
; T_ASSERT_POSIX_ZERO(pthread_join(threads
[i
], NULL
), NULL
);
131 T_PASS("putenv_thread() completed successfully");
134 T_DECL(setenv_thread
, "Test concurrent access by setenv(3)")
136 pthread_t threads
[THREADED_NUM_THREADS
];
137 time_t endtime
= time(NULL
) + THREADED_RUN_TIME
;
139 for (int i
= 0; i
< THREADED_NUM_THREADS
; i
++) {
140 T_QUIET
; T_ASSERT_POSIX_ZERO(
141 pthread_create(&threads
[i
], NULL
, thread_setenv
, &endtime
),
144 for (int i
= 0; i
< THREADED_NUM_THREADS
; i
++) {
145 T_QUIET
; T_ASSERT_POSIX_ZERO(pthread_join(threads
[i
], NULL
), NULL
);
147 T_PASS("setenv_thread() completed successfully");
150 T_DECL(unsetenv_thread
, "Test unsetenv(3) with threads")
152 pthread_t threads
[THREADED_NUM_THREADS
];
153 time_t endtime
= time(NULL
) + THREADED_RUN_TIME
;
155 for (int i
= 0; i
< THREADED_NUM_THREADS
; i
++) {
156 T_QUIET
; T_ASSERT_POSIX_ZERO(
157 pthread_create(&threads
[i
], NULL
, thread_unsetenv
, &endtime
),
160 for (int i
= 0; i
< THREADED_NUM_THREADS
; i
++) {
161 T_QUIET
; T_ASSERT_POSIX_ZERO(pthread_join(threads
[i
], NULL
), NULL
);
163 T_PASS("unsetenv_thread() completed successfully");