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 for (n = 0; n < COUNT; n++) {
34 for (d = 0; d < depth; d++) {
35 int err = objc_sync_enter(lock);
36 testassert(err == OBJC_SYNC_SUCCESS);
43 for (d = 0; d < depth; d++) {
44 int err = objc_sync_exit(lock);
45 testassert(err == OBJC_SYNC_SUCCESS);
49 // Verify lack of objc pthread data (should have used sync fast cache)
50 #ifdef __PTK_FRAMEWORK_OBJC_KEY0
51 testassert(! pthread_getspecific(__PTK_FRAMEWORK_OBJC_KEY0));
59 pthread_t threads[THREADS];
63 lock = [[NSObject alloc] init];
65 // Verify objc pthread data on this thread (from +initialize)
66 // Worker threads shouldn't have any because of sync fast cache.
67 #ifdef __PTK_FRAMEWORK_OBJC_KEY0
68 testassert(pthread_getspecific(__PTK_FRAMEWORK_OBJC_KEY0));
72 for (t = 0; t < THREADS; t++) {
73 pthread_create(&threads[t], NULL, &threadfn, (void*)(intptr_t)t);
76 // Wait for threads to finish
77 for (t = 0; t < THREADS; t++) {
78 pthread_join(threads[t], NULL);
81 // Verify lock: should be available
82 // Verify count: should be THREADS*COUNT
83 err = objc_sync_enter(lock);
84 testassert(err == OBJC_SYNC_SUCCESS);
85 testassert(count == THREADS*COUNT);