]> git.saurik.com Git - apple/objc4.git/blob - test/supported-inline-refcnt.m
objc4-787.1.tar.gz
[apple/objc4.git] / test / supported-inline-refcnt.m
1 // TEST_CONFIG MEM=mrc
2 // TEST_CFLAGS -framework CoreFoundation -Weverything
3
4 #pragma clang diagnostic push
5 #pragma clang diagnostic ignored "-Weverything"
6 #include "test.h"
7 #pragma clang diagnostic pop
8 #include <objc/NSObject.h>
9 #include <objc/objc-internal.h>
10 #include <CoreFoundation/CoreFoundation.h>
11
12
13 // Some warnings just aren't feasible to work around. We'll disable them instead.
14 #pragma clang diagnostic ignored "-Watomic-implicit-seq-cst"
15 #pragma clang diagnostic ignored "-Wdirect-ivar-access"
16 #pragma clang diagnostic ignored "-Wold-style-cast"
17
18 static int deallocCount;
19 @interface Refcnt: NSObject @end
20 @implementation Refcnt {
21 int _rc;
22 }
23
24 _OBJC_SUPPORTED_INLINE_REFCNT(_rc)
25
26 - (void)dealloc {
27 deallocCount++;
28 [super dealloc];
29 }
30
31 @end
32
33 @interface MainRefcnt: NSObject @end
34 @implementation MainRefcnt {
35 int _rc;
36 }
37
38 _OBJC_SUPPORTED_INLINE_REFCNT_WITH_DEALLOC2MAIN(_rc)
39
40 - (void)dealloc {
41 testassert(pthread_main_np());
42 deallocCount++;
43 [super dealloc];
44 }
45
46 @end
47
48 int main()
49 {
50 Refcnt *obj = [Refcnt new];
51 [obj retain];
52 [obj retain];
53 [obj retain];
54 [obj release];
55 [obj release];
56 [obj release];
57 [obj release];
58 testassert(deallocCount == 1);
59
60 MainRefcnt *obj2 = [MainRefcnt new];
61 [obj2 retain];
62 [obj2 retain];
63 [obj2 retain];
64
65 dispatch_group_t group = dispatch_group_create();
66 dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
67 [obj2 release];
68 });
69 dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
70 [obj2 release];
71 });
72 dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
73 [obj2 release];
74 });
75 dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
76 [obj2 release];
77 });
78
79 dispatch_group_notify(group, dispatch_get_main_queue(), ^{
80 testassert(deallocCount == 2);
81 succeed(__FILE__);
82 });
83
84 CFRunLoopRun();
85 }