-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYSemaphore.m
308 lines (264 loc) · 9.54 KB
/
SSYSemaphore.m
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#import "SSYSemaphore.h"
#import "NSFileManager+SomeMore.h"
#import "SSYOtherApper.h"
#import "NSBundle+SSYMotherApp.h"
#import "NSBundle+MainApp.h"
#import <fcntl.h>
NSString* SSYSemaphoreErrorDomain = @"SSYSemaphoreErrorDomain" ;
@implementation SSYSemaphorePidKey
+ (pid_t)pidFromString:(NSString*)string {
pid_t pid ;
if ([string length] < 5) {
pid = 0 ;
}
else {
pid = (pid_t)[[string substringToIndex:5] integerValue] ;
}
return pid ;
}
+ (NSString*)keyFromString:(NSString*)string {
NSString* key ;
if ([string length] < 6) {
key = nil ;
}
else {
key = [string substringFromIndex:6] ;
}
return key ;
}
+ (NSString*)pidStringForPid:(NSInteger)pid {
return [NSString stringWithFormat:@"%05ld", (long)pid] ;
}
- (NSString*)string {
return [NSString stringWithFormat:
@"%@:%@",
[[self class] pidStringForPid:[self pid]],
[self key]] ;
}
@synthesize pid = m_pid ;
@synthesize key = m_key ;
- (BOOL)isEqual:(SSYSemaphorePidKey*)otherPidKey {
if ([otherPidKey pid] != [self pid]) {
return NO ;
}
if (![[otherPidKey key] isEqualToString:[self key]]) {
return NO ;
}
return YES ;
}
- (id)initWithPid:(pid_t)pid
key:(NSString*)key {
self = [super init] ;
if (self) {
[self setPid:pid] ;
[self setKey:key] ;
}
return self ;
}
- (void)dealloc {
[m_key release] ;
[super dealloc] ;
}
- (NSString*)description {
return [NSString stringWithFormat:
@"%@ pid=%ld key=%@",
[super description],
(long)[self pid],
[self key]] ;
}
+ (SSYSemaphorePidKey*)pidKeyWithPid:(pid_t)pid
key:(NSString*)key {
SSYSemaphorePidKey* instance = [[self alloc] initWithPid:pid
key:key] ;
return [instance autorelease] ;
}
+ (SSYSemaphorePidKey*)pidKeyWithString:(NSString*)pidKeyString {
NSString* key = [self keyFromString:pidKeyString] ;
pid_t pid = [self pidFromString:pidKeyString] ;
return [self pidKeyWithPid:pid
key:key] ;
}
@end
@implementation SSYSemaphore
+ (NSString*)semaphoreName {
return @"SSYSemaphore" ;
}
+ (NSString*)semaphoreBasePath {
NSString* path ;
path = [[NSBundle mainAppBundle] applicationSupportPathForMotherApp] ;
if (!path) {
path = NSHomeDirectory() ;
}
path = [path stringByAppendingPathComponent:[@"." stringByAppendingString:[self semaphoreName]]] ;
return path ;
}
+ (NSString*)infoPath {
NSString* path = [self semaphoreBasePath] ;
path = [path stringByAppendingString:@"-Info"] ;
return path ;
}
+ (NSString*)lockPath {
NSString* path = [self semaphoreBasePath] ;
path = [path stringByAppendingString:@"-Lock"] ;
return path ;
}
+ (int)lockMomentaryLock {
const char* path = [[self lockPath] fileSystemRepresentation] ;
#if 0
// Using code by Stéphane from this blog post
// http://charette.no-ip.com:81/programming/2010-01-13_PosixSemaphores/index.html
// This code has an issue. Running the test code for a half hour or so,
// the result with this code is: 2198 unique acquisitions, 26 dual
// acqusitions (meaning that two contending processes request
// -acquireWithKey:::::::: and both get returned YES.) These "collisions"
// typically occur when these requests occur within 10 milliseconds of each
// other.
int fileDescriptor = open(path,
O_RDWR | // open the file for both read and write access
O_CREAT | // create file if it does not already exist
O_CLOEXEC , // close on execute
S_IRUSR | // user permission: read
S_IWUSR ); // user permission: write
lockf( fileDescriptor, F_TLOCK, 0 ) ; // lock the "semaphore"
#else
// Using code by in answer by Raffi Khatchadourian in this thread:
// http://stackoverflow.com/questions/2053679/how-do-i-recover-a-semaphore-when-the-process-that-decremented-it-to-zero-crashe
// This code is good. Running the test code for a couple hours,
// the result with this code is: 5512 unique acquisitions, 0 dual
// acquisitions.
int fileDescriptor = open(
path,
O_CREAT | //create the file if it's not present.
O_WRONLY | //only need write access for the internal locking semantics.
O_EXLOCK, //use an exclusive lock when opening the file.
S_IRUSR | S_IWUSR) ; //permissions on the file, 600 here.
#endif
return fileDescriptor ;
}
+ (void)relinquishMomentaryLock:(int)fileDescriptor {
if (close(fileDescriptor) != 0) {
NSLog(@"Internal Error 593-2101 %ld", (long)errno) ;
}
return ;
/*
*/
}
+ (SSYSemaphorePidKey*)currentPidKeyEnforcingTimeLimit:(NSTimeInterval)timeLimit {
NSString* currentPidKeyString = nil ;
BOOL overlimit ;
if (timeLimit == 0.0) {
overlimit = NO ;
}
else {
NSTimeInterval timeSinceFileModified = -[[[NSFileManager defaultManager] modificationDateForPath:[self infoPath]] timeIntervalSinceNow] ;
overlimit = (timeSinceFileModified > timeLimit) ;
}
if (overlimit) {
// Current leasee has timed out
[self clearError_p:NULL] ;
}
else {
// Current leasee has not timed out
currentPidKeyString = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:[self infoPath]]
usedEncoding:NULL
error:NULL] ;
}
SSYSemaphorePidKey* currentPidKey ;
if (currentPidKeyString) {
currentPidKey = [SSYSemaphorePidKey pidKeyWithString:currentPidKeyString] ;
}
else {
currentPidKey = nil ;
}
return currentPidKey ;
}
+ (BOOL)acquireWithKey:(NSString*)acquireKey
setKey:(NSString*)newKey
forPid:(pid_t)forPid
initialBackoff:(NSTimeInterval)initialBackoff
backoffFactor:(CGFloat)backoffFactor
maxBackoff:(NSTimeInterval)maxBackoff
timeout:(NSTimeInterval)timeout
timeLimit:(NSTimeInterval)timeLimit
error_p:(NSError**)error_p {
NSTimeInterval backoff = initialBackoff ;
NSDate* deadline = [NSDate dateWithTimeIntervalSinceNow:timeout] ;
NSString* path = [self infoPath] ;
SSYSemaphorePidKey* acquirePidKey = [SSYSemaphorePidKey pidKeyWithPid:forPid
key:newKey] ;
SSYSemaphorePidKey* currentPidKey = nil ;
while ([(NSDate*)[NSDate date] compare:deadline] == NSOrderedAscending) {
int momentaryLockFileDescriptor = [self lockMomentaryLock] ;
if (momentaryLockFileDescriptor >= 0) {
currentPidKey = [self currentPidKeyEnforcingTimeLimit:timeLimit] ;
pid_t currentPid = [currentPidKey pid] ;
SSYOtherApperProcessState currentProcessState = [SSYOtherApper stateOfPid:currentPid] ;
BOOL requestorIsCurrentOwner = ((currentPid == forPid) && ([acquirePidKey isEqual:currentPidKey])) ;
BOOL gotIt = NO ;
if (
(BOOL)(currentPidKey == nil)
// semaphore is not in use or its time limit has expired
||
(requestorIsCurrentOwner)
||
(BOOL)(currentProcessState == SSYOtherApperProcessDoesNotExist)
||
(BOOL)(currentProcessState == SSYOtherApperProcessStateZombie)
||
(BOOL)(currentProcessState == SSYOtherApperProcessUnknown)
) {
// semaphore is available
if (!requestorIsCurrentOwner) {
NSString* newString = [acquirePidKey string] ;
[newString writeToFile:path
atomically:YES
encoding:NSUTF8StringEncoding
error:NULL] ;
}
gotIt = YES ;
}
[self relinquishMomentaryLock:momentaryLockFileDescriptor] ;
if (gotIt) {
return YES ;
}
}
// semaphore is not available
usleep( 1000000 * backoff ) ;
backoff = backoff * backoffFactor ;
backoff = MIN(backoff, maxBackoff) ;
}
// Timeout
if (error_p) {
NSString* msg = [NSString stringWithFormat:
@"Timeout %g secs exceeded attempting to acquire semaphore with old key %@ from %@",
timeout,
acquireKey,
currentPidKey
] ;
*error_p = [NSError errorWithDomain:@"SSYSystemSemaphore"
code:ETIME
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
msg, NSLocalizedDescriptionKey,
nil]] ;
}
return NO ;
}
+ (BOOL)clearError_p:(NSError**)error_p {
NSError* error = nil ;
BOOL ok = [[NSFileManager defaultManager] removeItemAtPath:[self infoPath]
error:&error] ;
// The following false-alarm fix was added in BookMacster 1.12.6
if (!ok) {
if ([[error domain] isEqualToString:NSCocoaErrorDomain] && ([error code] == NSFileNoSuchFileError)) {
// The .Semaphore file has already been removed, presumably
// by some other actor. This is not an error.
ok = YES ;
error = nil ;
}
}
if (!ok && error_p) {
*error_p = error ;
}
return ok ;
}
@end