-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNSRecursiveCondition.m
More file actions
97 lines (82 loc) · 2.18 KB
/
NSRecursiveCondition.m
File metadata and controls
97 lines (82 loc) · 2.18 KB
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
/*!
* NSRecursiveCondition.m
*
* Created by Bradley Snyder on 2/12/14.
*
* Copyright 2014 Bradley J. Snyder <snyder.bradleyj@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "NSRecursiveCondition.h"
#import <pthread.h>
@interface NSRecursiveCondition() {
pthread_mutex_t _lock;
pthread_cond_t _cond;
}
@end
@implementation NSRecursiveCondition
- (id)init
{
self = [super init];
if( self )
{
// Init recursive mutex
pthread_mutexattr_t mattr;
pthread_mutexattr_init( &mattr );
pthread_mutexattr_settype( &mattr, PTHREAD_MUTEX_RECURSIVE );
pthread_mutex_init( &_lock, &mattr );
pthread_mutexattr_destroy( &mattr );
// Init condition variable
pthread_condattr_t cattr;
pthread_condattr_init( &cattr );
pthread_cond_init( &_cond, &cattr );
pthread_condattr_destroy( &cattr );
}
return self;
}
- (void)dealloc
{
// Destroy pthread vars
pthread_cond_destroy( &_cond );
pthread_mutex_destroy( &_lock );
}
- (void)lock
{
pthread_mutex_lock( &_lock );
}
- (void)unlock
{
pthread_mutex_unlock( &_lock );
}
- (void)signal
{
pthread_cond_signal( &_cond );
}
- (void)broadcast
{
pthread_cond_broadcast( &_cond );
}
- (void)wait
{
pthread_cond_wait( &_cond, &_lock );
}
- (BOOL)waitUntilDate:(NSDate*)limit
{
// Convert NSDate value to struct timespec representation
struct timespec expireTime;
NSTimeInterval utime = [limit timeIntervalSince1970];
expireTime.tv_sec = utime;
expireTime.tv_nsec = (utime - expireTime.tv_sec) * 1000000000.f;
return !(pthread_cond_timedwait( &_cond, &_lock, &expireTime ));
}
@end