5 #include <Foundation/NSObject.h>
9 #include <objc/runtime.h>
10 #include <objc/objc-sync.h>
12 // Basic @synchronized tests.
18 static semaphore_t go;
19 static semaphore_t stop;
21 void *thread(void *arg __unused)
25 objc_registerThreadWithCollector();
27 // non-blocking sync_enter
28 err = objc_sync_enter(obj);
29 testassert(err == OBJC_SYNC_SUCCESS);
32 // main thread: sync_exit of object locked on some other thread
35 err = objc_sync_exit(obj);
36 testassert(err == OBJC_SYNC_SUCCESS);
37 err = objc_sync_enter(obj);
38 testassert(err == OBJC_SYNC_SUCCESS);
41 // main thread: blocking sync_enter
42 testassert(WAIT_SEC/3*3 == WAIT_SEC);
44 // recursive enter while someone waits
45 err = objc_sync_enter(obj);
46 testassert(err == OBJC_SYNC_SUCCESS);
48 // recursive exit while someone waits
49 err = objc_sync_exit(obj);
50 testassert(err == OBJC_SYNC_SUCCESS);
52 // sync_exit while someone waits
53 err = objc_sync_exit(obj);
54 testassert(err == OBJC_SYNC_SUCCESS);
63 struct timeval start, end;
65 obj = [[NSObject alloc] init];
67 // sync_exit of never-locked object
68 err = objc_sync_exit(obj);
69 testassert(err == OBJC_SYNC_NOT_OWNING_THREAD_ERROR);
71 semaphore_create(mach_task_self(), &go, 0, 0);
72 semaphore_create(mach_task_self(), &stop, 0, 0);
73 pthread_create(&th, NULL, &thread, NULL);
76 // sync_exit of object locked on some other thread
77 err = objc_sync_exit(obj);
78 testassert(err == OBJC_SYNC_NOT_OWNING_THREAD_ERROR);
80 semaphore_signal(stop);
83 // blocking sync_enter
84 gettimeofday(&start, NULL);
85 err = objc_sync_enter(obj);
86 gettimeofday(&end, NULL);
87 testassert(err == OBJC_SYNC_SUCCESS);
88 // should have waited more than WAIT_SEC but less than WAIT_SEC+1
89 // fixme hack: sleep(1) is ending 500 usec too early on x86_64 buildbot
91 testassert(end.tv_sec*1000000LL+end.tv_usec >=
92 start.tv_sec*1000000LL+start.tv_usec + WAIT_SEC*1000000LL
94 testassert(end.tv_sec*1000000LL+end.tv_usec <
95 start.tv_sec*1000000LL+start.tv_usec + (1+WAIT_SEC)*1000000LL);
97 err = objc_sync_exit(obj);
98 testassert(err == OBJC_SYNC_SUCCESS);
100 err = objc_sync_exit(obj);
101 testassert(err == OBJC_SYNC_NOT_OWNING_THREAD_ERROR);