2 * Copyright (c) 2010 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <stdio.h> // fprintf(), NULL
24 #include <stdlib.h> // exit(), EXIT_SUCCESS
27 #include <mach-o/dyld_priv.h>
32 #include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
35 static void* work(void* libName
)
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());
49 static const char* batchMappedHandler(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info info
[])
58 // tell dyld we want to know when images are mapped
59 dyld_register_image_state_change_handler(dyld_image_state_initialized
, false, batchMappedHandler
);
61 // other thread dlopens and closes libfoo.dylib 500 times
63 if ( pthread_create(&t1
, NULL
, work
, "libfoo1.dylib") != 0 ) {
64 FAIL("pthread_create failed");
69 if ( pthread_create(&t2
, NULL
, work
, "libfoo2.dylib") != 0 ) {
70 FAIL("pthread_create failed");
75 if ( pthread_create(&t3
, NULL
, work
, "libfoo3.dylib") != 0 ) {
76 FAIL("pthread_create failed");
81 if ( pthread_create(&t4
, NULL
, work
, "libfoo4.dylib") != 0 ) {
82 FAIL("pthread_create failed");
87 pthread_join(t1
, &result
);
88 pthread_join(t2
, &result
);
89 pthread_join(t3
, &result
);
90 pthread_join(t4
, &result
);
92 PASS("concurrent-dlopen-initializers");