]> git.saurik.com Git - apple/dyld.git/blob - unit-tests/test-cases/concurrent-dlopen-initializers/main.c
dyld-655.1.tar.gz
[apple/dyld.git] / unit-tests / test-cases / concurrent-dlopen-initializers / main.c
1 /*
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 #include <stdio.h> // fprintf(), NULL
24 #include <stdlib.h> // exit(), EXIT_SUCCESS
25 #include <string.h>
26 #include <unistd.h>
27 #include <mach-o/dyld_priv.h>
28
29 #include <dlfcn.h>
30 #include <pthread.h>
31
32 #include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
33
34
35 static void* work(void* libName)
36 {
37 for (int i=0; i < 500; ++i) {
38 void* handle = dlopen((char*)libName, 0);
39 if ( handle == NULL ) {
40 FAIL("dlopen failed: %s", dlerror());
41 exit(0);
42 }
43 dlclose(handle);
44 }
45
46 return NULL;
47 }
48
49 static const char* batchMappedHandler(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
50 {
51 usleep(10000);
52 return NULL;
53 }
54
55
56 int main()
57 {
58 // tell dyld we want to know when images are mapped
59 dyld_register_image_state_change_handler(dyld_image_state_initialized, false, batchMappedHandler);
60
61 // other thread dlopens and closes libfoo.dylib 500 times
62 pthread_t t1;
63 if ( pthread_create(&t1, NULL, work, "libfoo1.dylib") != 0 ) {
64 FAIL("pthread_create failed");
65 exit(0);
66 }
67
68 pthread_t t2;
69 if ( pthread_create(&t2, NULL, work, "libfoo2.dylib") != 0 ) {
70 FAIL("pthread_create failed");
71 exit(0);
72 }
73
74 pthread_t t3;
75 if ( pthread_create(&t3, NULL, work, "libfoo3.dylib") != 0 ) {
76 FAIL("pthread_create failed");
77 exit(0);
78 }
79
80 pthread_t t4;
81 if ( pthread_create(&t4, NULL, work, "libfoo4.dylib") != 0 ) {
82 FAIL("pthread_create failed");
83 exit(0);
84 }
85
86 void* result;
87 pthread_join(t1, &result);
88 pthread_join(t2, &result);
89 pthread_join(t3, &result);
90 pthread_join(t4, &result);
91
92 PASS("concurrent-dlopen-initializers");
93 return 0;
94 }