forked from fachrifaul/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASRecursiveUnfairLockTests.mm
184 lines (161 loc) · 4.69 KB
/
ASRecursiveUnfairLockTests.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
// ASRecursiveUnfairLockTests.mm
// Texture
//
// Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import "ASTestCase.h"
#import <AsyncDisplayKit/ASRecursiveUnfairLock.h>
#import <stdatomic.h>
#import <os/lock.h>
@interface ASRecursiveUnfairLockTests : ASTestCase
@end
@implementation ASRecursiveUnfairLockTests {
ASRecursiveUnfairLock lock;
}
- (void)setUp
{
[super setUp];
lock = AS_RECURSIVE_UNFAIR_LOCK_INIT;
}
- (void)testTheAtomicIsLockFree
{
XCTAssertTrue(atomic_is_lock_free(&lock._thread));
}
- (void)testRelockingFromSameThread
{
ASRecursiveUnfairLockLock(&lock);
ASRecursiveUnfairLockLock(&lock);
ASRecursiveUnfairLockUnlock(&lock);
// Now try locking from another thread.
XCTestExpectation *e1 = [self expectationWithDescription:@"Other thread tried lock."];
[NSThread detachNewThreadWithBlock:^{
XCTAssertFalse(ASRecursiveUnfairLockTryLock(&self->lock));
[e1 fulfill];
}];
[self waitForExpectationsWithTimeout:1 handler:nil];
ASRecursiveUnfairLockUnlock(&lock);
XCTestExpectation *e2 = [self expectationWithDescription:@"Other thread tried lock again"];
[NSThread detachNewThreadWithBlock:^{
XCTAssertTrue(ASRecursiveUnfairLockTryLock(&self->lock));
ASRecursiveUnfairLockUnlock(&self->lock);
[e2 fulfill];
}];
[self waitForExpectationsWithTimeout:1 handler:nil];
}
- (void)testThatUnlockingWithoutHoldingMakesAssertion
{
#ifdef NS_BLOCK_ASSERTIONS
#warning Assertions should be on for `testThatUnlockingWithoutHoldingMakesAssertion`
NSLog(@"Passing because assertions are off.");
#else
ASRecursiveUnfairLockLock(&lock);
XCTestExpectation *e1 = [self expectationWithDescription:@"Other thread tried lock."];
[NSThread detachNewThreadWithBlock:^{
XCTAssertThrows(ASRecursiveUnfairLockUnlock(&self->lock));
[e1 fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:nil];
ASRecursiveUnfairLockUnlock(&lock);
#endif
}
#define CHAOS_TEST_BODY(contested, prefix, infix, postfix) \
dispatch_group_t g = dispatch_group_create(); \
for (int i = 0; i < (contested ? 16 : 2); i++) {\
dispatch_group_enter(g);\
[NSThread detachNewThreadWithBlock:^{\
for (int i = 0; i < 20000; i++) {\
prefix;\
value += 150;\
infix;\
value -= 150;\
postfix;\
}\
dispatch_group_leave(g);\
}];\
}\
dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
#pragma mark - Correctness Tests
- (void)testRecursiveUnfairLockContested
{
__block int value = 0;
[self measureBlock:^{
CHAOS_TEST_BODY(YES, ASRecursiveUnfairLockLock(&self->lock), {}, ASRecursiveUnfairLockUnlock(&self->lock));
}];
XCTAssertEqual(value, 0);
}
- (void)testRecursiveUnfairLockUncontested
{
__block int value = 0;
[self measureBlock:^{
CHAOS_TEST_BODY(NO, ASRecursiveUnfairLockLock(&self->lock), {}, ASRecursiveUnfairLockUnlock(&self->lock));
}];
XCTAssertEqual(value, 0);
}
#pragma mark - Lock performance tests
#if RUN_LOCK_PERF_TESTS
- (void)testNoLockContested
{
__block int value = 0;
[self measureBlock:^{
CHAOS_TEST_BODY(YES, {}, {}, {});
}];
XCTAssertNotEqual(value, 0);
}
- (void)testPlainUnfairLockContested
{
__block int value = 0;
__block os_unfair_lock unfairLock = OS_UNFAIR_LOCK_INIT;
[self measureBlock:^{
CHAOS_TEST_BODY(YES, os_unfair_lock_lock(&unfairLock), {}, os_unfair_lock_unlock(&unfairLock));
}];
XCTAssertEqual(value, 0);
}
- (void)testRecursiveMutexContested
{
__block int value = 0;
pthread_mutexattr_t attr;
pthread_mutexattr_init (&attr);
pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
__block pthread_mutex_t m;
pthread_mutex_init (&m, &attr);
pthread_mutexattr_destroy (&attr);
[self measureBlock:^{
CHAOS_TEST_BODY(YES, pthread_mutex_lock(&m), {}, pthread_mutex_unlock(&m));
}];
pthread_mutex_destroy(&m);
}
- (void)testNoLockUncontested
{
__block int value = 0;
[self measureBlock:^{
CHAOS_TEST_BODY(NO, {}, {}, {});
}];
XCTAssertNotEqual(value, 0);
}
- (void)testPlainUnfairLockUncontested
{
__block int value = 0;
__block os_unfair_lock unfairLock = OS_UNFAIR_LOCK_INIT;
[self measureBlock:^{
CHAOS_TEST_BODY(NO, os_unfair_lock_lock(&unfairLock), {}, os_unfair_lock_unlock(&unfairLock));
}];
XCTAssertEqual(value, 0);
}
- (void)testRecursiveMutexUncontested
{
__block int value = 0;
pthread_mutexattr_t attr;
pthread_mutexattr_init (&attr);
pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
__block pthread_mutex_t m;
pthread_mutex_init (&m, &attr);
pthread_mutexattr_destroy (&attr);
[self measureBlock:^{
CHAOS_TEST_BODY(NO, pthread_mutex_lock(&m), {}, pthread_mutex_unlock(&m));
}];
pthread_mutex_destroy(&m);
}
#endif // RUN_LOCK_PERF_TESTS
@end