]> git.saurik.com Git - apple/dyld.git/blob - unit-tests/test-cases/dlopen-dyld-locking/base.c
dyld-97.1.tar.gz
[apple/dyld.git] / unit-tests / test-cases / dlopen-dyld-locking / base.c
1 /*
2 * Copyright (c) 2007 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 <pthread.h>
24 #include <time.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <sys/time.h>
28
29 #include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
30
31 static pthread_mutex_t sBarrierMutex;
32 static pthread_cond_t sBarrierFree;
33
34 static volatile int sValue = 0;
35
36 static void __attribute__((constructor)) myinit()
37 {
38 pthread_mutex_init(&sBarrierMutex, NULL);
39 pthread_cond_init(&sBarrierFree, NULL);
40 }
41
42 void waitForState(int value)
43 {
44 pthread_mutex_lock(&sBarrierMutex);
45 while ( sValue < value ) {
46 struct timeval tvNow;
47 struct timespec tsTimeout;
48 gettimeofday(&tvNow, NULL);
49 TIMEVAL_TO_TIMESPEC(&tvNow, &tsTimeout);
50 tsTimeout.tv_sec += 2; // fail if block for 2 seconds
51 if ( pthread_cond_timedwait(&sBarrierFree, &sBarrierMutex, &tsTimeout) == ETIMEDOUT ) {
52 FAIL("dlsym-dyld-locking");
53 exit(0);
54 }
55 }
56 pthread_mutex_unlock(&sBarrierMutex);
57 }
58
59
60 void setState(int value)
61 {
62 pthread_mutex_lock(&sBarrierMutex);
63 sValue = value;
64 pthread_cond_broadcast(&sBarrierFree);
65 pthread_mutex_unlock(&sBarrierMutex);
66 }
67
68