5 #include <objc/runtime.h>
6 #include <objc/objc-sync.h>
7 #include <Foundation/Foundation.h>
8 #include <System/pthread_machdep.h>
10 // synchronized stress test
11 // Single locked counter incremented by many threads.
13 // 64 / 1024*24 test takes about 20s on 4x2.6GHz Mac Pro
20 static void *threadfn(void *arg)
23 int depth = 1 + (int)(intptr_t)arg % 4;
25 objc_registerThreadWithCollector();
27 for (n = 0; n < COUNT; n++) {
29 for (d = 0; d < depth; d++) {
30 int err = objc_sync_enter(lock);
31 testassert(err == OBJC_SYNC_SUCCESS);
38 for (d = 0; d < depth; d++) {
39 int err = objc_sync_exit(lock);
40 testassert(err == OBJC_SYNC_SUCCESS);
44 // Verify lack of objc pthread data (should have used sync fast cache)
45 if (_pthread_has_direct_tsd()) {
46 testassert(! pthread_getspecific(__PTK_FRAMEWORK_OBJC_KEY5));
53 pthread_t threads[THREADS];
57 lock = [[NSObject alloc] init];
59 // Verify objc pthread data on this thread (from +initialize)
60 // Worker threads shouldn't have any because of sync fast cache.
61 if (_pthread_has_direct_tsd()) {
62 testassert(pthread_getspecific(__PTK_FRAMEWORK_OBJC_KEY5));
66 for (t = 0; t < THREADS; t++) {
67 pthread_create(&threads[t], NULL, &threadfn, (void*)(intptr_t)t);
70 // Wait for threads to finish
71 for (t = 0; t < THREADS; t++) {
72 pthread_join(threads[t], NULL);
75 // Verify lock: should be available
76 // Verify count: should be THREADS*COUNT
77 err = objc_sync_enter(lock);
78 testassert(err == OBJC_SYNC_SUCCESS);
79 testassert(count == THREADS*COUNT);