drd/tests/swapcontext: Improve the portability of this test further
[valgrind.git] / drd / tests / concurrent_close.cpp
blob9f7e6d622fbc55dcaf1098e818eca6f6e37ed3ab
1 /*
2 * Reproducer for bug #323905. See also
3 * http://bugs.kde.org/show_bug.cgi?id=323905.
4 */
6 #include <climits> /* PTHREAD_STACK_MIN */
7 #include <cstdio> /* fprintf() */
8 #include <fcntl.h> /* O_RDONLY */
9 #include <pthread.h>
10 #include <unistd.h> /* close() */
12 /* Happens with two threads also */
13 #define THREAD_COUNT 256
15 void* thread(void*)
17 int fd;
19 /* Happens with any file, not just /dev/null */
20 fd = open("/dev/null", O_RDONLY);
21 if (fd >= 0)
22 close(fd);
23 else
24 fprintf(stderr, "Failed to open /dev/null\n");
25 return 0;
28 int main()
30 int i, r;
31 pthread_attr_t attr;
32 pthread_t threads[THREAD_COUNT];
34 pthread_attr_init(&attr);
35 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
36 for (i = 0; i < THREAD_COUNT; ++i) {
37 r = pthread_create(&threads[i], &attr, thread, 0);
38 if (r != 0) {
39 fprintf(stderr, "Failed to create thread %d\n", i);
40 return 1;
43 pthread_attr_destroy(&attr);
44 for (i = 0; i < THREAD_COUNT; ++i) {
45 r = pthread_join(threads[i], 0);
46 if (r != 0) {
47 fprintf(stderr, "Failed to join thread %d\n", i);
48 return 1;
51 fprintf(stderr, "Done.\n");
52 return 0;