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)
26 // non-blocking sync_enter
27 err = objc_sync_enter(obj);
28 testassert(err == OBJC_SYNC_SUCCESS);
30 // recursive try_sync_enter
31 locked = objc_sync_try_enter(obj);
33 err = objc_sync_exit(obj);
34 testassert(err == OBJC_SYNC_SUCCESS);
37 // main thread: sync_exit of object locked on some other thread
40 err = objc_sync_exit(obj);
41 testassert(err == OBJC_SYNC_SUCCESS);
42 err = objc_sync_enter(obj);
43 testassert(err == OBJC_SYNC_SUCCESS);
46 // main thread: blocking sync_enter
47 testassert(WAIT_SEC/3*3 == WAIT_SEC);
49 // recursive enter while someone waits
50 err = objc_sync_enter(obj);
51 testassert(err == OBJC_SYNC_SUCCESS);
53 // recursive exit while someone waits
54 err = objc_sync_exit(obj);
55 testassert(err == OBJC_SYNC_SUCCESS);
57 // sync_exit while someone waits
58 err = objc_sync_exit(obj);
59 testassert(err == OBJC_SYNC_SUCCESS);
68 struct timeval start, end;
71 obj = [[NSObject alloc] init];
73 // sync_exit of never-locked object
74 err = objc_sync_exit(obj);
75 testassert(err == OBJC_SYNC_NOT_OWNING_THREAD_ERROR);
77 semaphore_create(mach_task_self(), &go, 0, 0);
78 semaphore_create(mach_task_self(), &stop, 0, 0);
79 pthread_create(&th, NULL, &thread, NULL);
82 // sync_exit of object locked on some other thread
83 err = objc_sync_exit(obj);
84 testassert(err == OBJC_SYNC_NOT_OWNING_THREAD_ERROR);
86 semaphore_signal(stop);
89 // contended try_sync_enter
90 locked = objc_sync_try_enter(obj);
93 // blocking sync_enter
94 gettimeofday(&start, NULL);
95 err = objc_sync_enter(obj);
96 gettimeofday(&end, NULL);
97 testassert(err == OBJC_SYNC_SUCCESS);
98 // should have waited more than WAIT_SEC but less than WAIT_SEC+1
99 // fixme hack: sleep(1) is ending 500 usec too early on x86_64 buildbot
101 testassert(end.tv_sec*1000000LL+end.tv_usec >=
102 start.tv_sec*1000000LL+start.tv_usec + WAIT_SEC*1000000LL
104 testassert(end.tv_sec*1000000LL+end.tv_usec <
105 start.tv_sec*1000000LL+start.tv_usec + (1+WAIT_SEC)*1000000LL);
107 err = objc_sync_exit(obj);
108 testassert(err == OBJC_SYNC_SUCCESS);
110 err = objc_sync_exit(obj);
111 testassert(err == OBJC_SYNC_NOT_OWNING_THREAD_ERROR);