7 #include <objc/runtime.h>
8 #include <objc/objc-sync.h>
9 #include <Foundation/NSObject.h>
10 #include <System/pthread_machdep.h>
12 // synchronized stress test
13 // Single locked counter incremented by many threads.
19 // 64 / 1024*24 test takes about 20s on 4x2.6GHz Mac Pro
27 static void *threadfn(void *arg)
30 int depth = 1 + (int)(intptr_t)arg % 4;
32 objc_registerThreadWithCollector();
34 for (n = 0; n < COUNT; n++) {
36 for (d = 0; d < depth; d++) {
37 int err = objc_sync_enter(lock);
38 testassert(err == OBJC_SYNC_SUCCESS);
45 for (d = 0; d < depth; d++) {
46 int err = objc_sync_exit(lock);
47 testassert(err == OBJC_SYNC_SUCCESS);
51 // Verify lack of objc pthread data (should have used sync fast cache)
52 #ifdef __PTK_FRAMEWORK_OBJC_KEY0
53 testassert(! pthread_getspecific(__PTK_FRAMEWORK_OBJC_KEY0));
61 pthread_t threads[THREADS];
65 lock = [[NSObject alloc] init];
67 // Verify objc pthread data on this thread (from +initialize)
68 // Worker threads shouldn't have any because of sync fast cache.
69 #ifdef __PTK_FRAMEWORK_OBJC_KEY0
70 testassert(pthread_getspecific(__PTK_FRAMEWORK_OBJC_KEY0));
74 for (t = 0; t < THREADS; t++) {
75 pthread_create(&threads[t], NULL, &threadfn, (void*)(intptr_t)t);
78 // Wait for threads to finish
79 for (t = 0; t < THREADS; t++) {
80 pthread_join(threads[t], NULL);
83 // Verify lock: should be available
84 // Verify count: should be THREADS*COUNT
85 err = objc_sync_enter(lock);
86 testassert(err == OBJC_SYNC_SUCCESS);
87 testassert(count == THREADS*COUNT);