2 * Copyright (c) 2007 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@
29 #include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
31 static pthread_mutex_t sBarrierMutex
;
32 static pthread_cond_t sBarrierFree
;
34 static volatile int sValue
= 0;
36 static void __attribute__((constructor
)) myinit()
38 pthread_mutex_init(&sBarrierMutex
, NULL
);
39 pthread_cond_init(&sBarrierFree
, NULL
);
42 void waitForState(int value
)
44 //fprintf(stderr, "waitForState(%d), currently %d\n", value, sValue);
45 pthread_mutex_lock(&sBarrierMutex
);
46 while ( sValue
< value
) {
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");
57 pthread_mutex_unlock(&sBarrierMutex
);
61 void setState(int value
)
63 pthread_mutex_lock(&sBarrierMutex
);
64 //fprintf(stderr, "setState(%d)\n", value);
66 pthread_cond_broadcast(&sBarrierFree
);
67 pthread_mutex_unlock(&sBarrierMutex
);