dyld-733.6.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 //fprintf(stderr, "waitForState(%d), currently %d\n", value, sValue);
45 pthread_mutex_lock(&sBarrierMutex);
46 while ( sValue < value ) {
47 struct timeval tvNow;
48 struct timespec tsTimeout;
49 gettimeofday(&tvNow, NULL);
50 TIMEVAL_TO_TIMESPEC(&tvNow, &tsTimeout);
51 tsTimeout.tv_sec += 2; // fail if block for 2 seconds
52 if ( pthread_cond_timedwait(&sBarrierFree, &sBarrierMutex, &tsTimeout) == ETIMEDOUT ) {
53 FAIL("dlopen-dyld-locking: lock timed out");
54 exit(0);
55 }
56 }
57 pthread_mutex_unlock(&sBarrierMutex);
58 }
59
60
61 void setState(int value)
62 {
63 pthread_mutex_lock(&sBarrierMutex);
64 //fprintf(stderr, "setState(%d)\n", value);
65 sValue = value;
66 pthread_cond_broadcast(&sBarrierFree);
67 pthread_mutex_unlock(&sBarrierMutex);
68 }
69
70