Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9333816

Browse files
committed
+ added ability to switch between OSSpinLock and os_unfair_lock depending from possibilities of OS on which code is running at this moment (CE-2910).
1 parent 13ba68f commit 9333816

13 files changed

Lines changed: 826 additions & 437 deletions

File tree

Example/PubNub Example.xcodeproj/project.pbxproj

Lines changed: 52 additions & 52 deletions
Large diffs are not rendered by default.

Example/PubNub/Images.xcassets/AppIcon.appiconset/Contents.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
{
22
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"size" : "20x20",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"size" : "20x20",
11+
"scale" : "3x"
12+
},
313
{
414
"idiom" : "iphone",
515
"size" : "29x29",
@@ -30,6 +40,16 @@
3040
"size" : "60x60",
3141
"scale" : "3x"
3242
},
43+
{
44+
"idiom" : "ipad",
45+
"size" : "20x20",
46+
"scale" : "1x"
47+
},
48+
{
49+
"idiom" : "ipad",
50+
"size" : "20x20",
51+
"scale" : "2x"
52+
},
3353
{
3454
"idiom" : "ipad",
3555
"size" : "29x29",
@@ -59,6 +79,11 @@
5979
"idiom" : "ipad",
6080
"size" : "76x76",
6181
"scale" : "2x"
82+
},
83+
{
84+
"idiom" : "ipad",
85+
"size" : "83.5x83.5",
86+
"scale" : "2x"
6287
}
6388
],
6489
"info" : {

Framework/PubNub Framework.xcodeproj/project.pbxproj

Lines changed: 48 additions & 0 deletions
Large diffs are not rendered by default.

PubNub.podspec

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ Pod::Spec.new do |spec|
4646
end
4747

4848
spec.subspec 'Logger' do |logger|
49-
logger.source_files = 'PubNub/Misc/Logger/{Core,Data}/**/*'
50-
logger.private_header_files = 'PubNub/Misc/Logger/Data/*.h'
49+
logger.source_files = 'PubNub/Misc/Logger/{Core,Data}/**/*', 'PubNub/Misc/Helpers/{PNLockSupport,PNDefines}.h'
50+
logger.private_header_files = [
51+
'PubNub/Misc/Logger/Data/*.h',
52+
'PubNub/Misc/Helpers/{PNLockSupport,PNDefines}.h'
53+
]
5154
end
5255

5356
spec.subspec 'Fabric' do |fabric|

PubNub/Data/Managers/PNPublishSequence.m

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
#import <AppKit/AppKit.h>
88
#endif // TARGET_OS_OSX
99

10-
#import <libkern/OSAtomic.h>
1110
#import "PNConfiguration.h"
1211
#import "PubNub+Core.h"
1312
#import "PNKeychain.h"
13+
#import "PNHelpers.h"
1414

1515

1616
NS_ASSUME_NONNULL_BEGIN
@@ -39,7 +39,7 @@
3939
4040
@since 4.5.2
4141
*/
42-
static OSSpinLock publishSequenceKeychainAccessLock = OS_SPINLOCK_INIT;
42+
static os_unfair_lock publishSequenceKeychainAccessLock = OS_UNFAIR_LOCK_INIT;
4343

4444

4545
#pragma mark - Structures
@@ -83,7 +83,7 @@ @interface PNPublishSequence () {
8383
8484
@since 4.5.2
8585
*/
86-
OSSpinLock _lock;
86+
os_unfair_lock _lock;
8787
}
8888

8989

@@ -198,19 +198,19 @@ @implementation PNPublishSequence
198198

199199
- (NSUInteger)sequenceNumber {
200200

201-
OSSpinLockLock(&_lock);
202-
NSUInteger sequenceNumber = _sequenceNumber;
203-
OSSpinLockUnlock(&_lock);
201+
__block NSUInteger sequenceNumber = 0;
202+
pn_lock(&_lock, ^{ sequenceNumber = _sequenceNumber; });
204203

205204
return sequenceNumber;
206205
}
207206

208207
- (NSUInteger)nextSequenceNumber:(BOOL)shouldUpdateCurrent {
209208

210-
OSSpinLockLock(&_lock);
211-
NSUInteger sequenceNumber = (_sequenceNumber == NSUIntegerMax ? 1 : _sequenceNumber + 1);
212-
if (shouldUpdateCurrent) { _sequenceNumber = sequenceNumber; }
213-
OSSpinLockUnlock(&_lock);
209+
__block NSUInteger sequenceNumber = 0;
210+
pn_lock(&_lock, ^{
211+
sequenceNumber = (_sequenceNumber == NSUIntegerMax ? 1 : _sequenceNumber + 1);
212+
if (shouldUpdateCurrent) { _sequenceNumber = sequenceNumber; }
213+
});
214214

215215
return sequenceNumber;
216216
}
@@ -251,7 +251,7 @@ - (instancetype)initForClient:(PubNub *)client {
251251

252252
_client = client;
253253
_publishKey = client.currentConfiguration.publishKey;
254-
_lock = OS_SPINLOCK_INIT;
254+
_lock = OS_UNFAIR_LOCK_INIT;
255255

256256
[self loadFromPersistentStorage];
257257
[self cleanUpIfRequired];
@@ -263,9 +263,7 @@ - (instancetype)initForClient:(PubNub *)client {
263263

264264
- (void)reset {
265265

266-
OSSpinLockLock(&_lock);
267-
_sequenceNumber = 0;
268-
OSSpinLockUnlock(&_lock);
266+
pn_lock(&_lock, ^{ _sequenceNumber = 0; });
269267
[self saveToPersistentStorage];
270268
}
271269

@@ -274,58 +272,65 @@ - (void)reset {
274272

275273
- (void)loadFromPersistentStorage {
276274

277-
OSSpinLockLock(&publishSequenceKeychainAccessLock);
278-
[PNKeychain valueForKey:kPNPublishSequenceDataKey withCompletionBlock:^(NSDictionary *sequences) {
275+
pn_lock_async(&publishSequenceKeychainAccessLock, ^(dispatch_block_t completion) {
279276

280-
NSMutableDictionary *mutableSequences = [(sequences?: @{}) mutableCopy];
281-
NSMutableDictionary *sequenceData = [(mutableSequences[self.publishKey]?: @{}) mutableCopy];
282-
NSNumber *sequenceNumber = (NSNumber *)sequenceData[PNPublishSequenceData.sequence];
283-
sequenceData[PNPublishSequenceData.lastSaveDate] = @([NSDate date].timeIntervalSince1970);
284-
_sequenceNumber = sequenceNumber.unsignedIntegerValue;
285-
[PNKeychain storeValue:mutableSequences forKey:kPNPublishSequenceDataKey
286-
withCompletionBlock:^(BOOL stored) { OSSpinLockUnlock(&publishSequenceKeychainAccessLock); }];
287-
}];
277+
[PNKeychain valueForKey:kPNPublishSequenceDataKey withCompletionBlock:^(NSDictionary *sequences) {
278+
279+
NSMutableDictionary *mutableSequences = [(sequences?: @{}) mutableCopy];
280+
NSMutableDictionary *sequenceData = [(mutableSequences[self.publishKey]?: @{}) mutableCopy];
281+
NSNumber *sequenceNumber = (NSNumber *)sequenceData[PNPublishSequenceData.sequence];
282+
sequenceData[PNPublishSequenceData.lastSaveDate] = @([NSDate date].timeIntervalSince1970);
283+
_sequenceNumber = sequenceNumber.unsignedIntegerValue;
284+
[PNKeychain storeValue:mutableSequences forKey:kPNPublishSequenceDataKey
285+
withCompletionBlock:^(BOOL stored) { completion(); }];
286+
}];
287+
});
288288
}
289289

290290
- (void)saveToPersistentStorage {
291291

292-
OSSpinLockLock(&publishSequenceKeychainAccessLock);
293-
[PNKeychain valueForKey:kPNPublishSequenceDataKey withCompletionBlock:^(NSDictionary *sequences) {
292+
pn_lock_async(&publishSequenceKeychainAccessLock, ^(dispatch_block_t completion) {
294293

295-
NSMutableDictionary *mutableSequences = [(sequences?: @{}) mutableCopy];
296-
NSMutableDictionary *sequenceData = [(mutableSequences[self.publishKey]?: @{}) mutableCopy];
297-
sequenceData[PNPublishSequenceData.sequence] = @(self.sequenceNumber);
298-
sequenceData[PNPublishSequenceData.lastSaveDate] = @([NSDate date].timeIntervalSince1970);
299-
mutableSequences[self.publishKey] = sequenceData;
300-
[PNKeychain storeValue:mutableSequences forKey:kPNPublishSequenceDataKey
301-
withCompletionBlock:^(BOOL stored) { OSSpinLockUnlock(&publishSequenceKeychainAccessLock); }];
302-
}];
294+
[PNKeychain valueForKey:kPNPublishSequenceDataKey withCompletionBlock:^(NSDictionary *sequences) {
295+
296+
NSMutableDictionary *mutableSequences = [(sequences?: @{}) mutableCopy];
297+
NSMutableDictionary *sequenceData = [(mutableSequences[self.publishKey]?: @{}) mutableCopy];
298+
sequenceData[PNPublishSequenceData.sequence] = @(self.sequenceNumber);
299+
sequenceData[PNPublishSequenceData.lastSaveDate] = @([NSDate date].timeIntervalSince1970);
300+
mutableSequences[self.publishKey] = sequenceData;
301+
[PNKeychain storeValue:mutableSequences forKey:kPNPublishSequenceDataKey
302+
withCompletionBlock:^(BOOL stored) { completion(); }];
303+
}];
304+
});
303305
}
304306

305307
- (void)cleanUpIfRequired {
306308

307309
NSTimeInterval currentTimestamp = [NSDate date].timeIntervalSince1970;
308-
OSSpinLockLock(&publishSequenceKeychainAccessLock);
309-
[PNKeychain valueForKey:kPNPublishSequenceDataKey withCompletionBlock:^(NSDictionary *sequences) {
310+
pn_lock_async(&publishSequenceKeychainAccessLock, ^(dispatch_block_t completion) {
310311

311-
NSMutableDictionary *mutableSequences = [(sequences?: @{}) mutableCopy];
312-
[mutableSequences enumerateKeysAndObjectsUsingBlock:^(NSString *publishKey, NSDictionary *sequenceData,
313-
BOOL *sequencesEnumeratorStop) {
312+
[PNKeychain valueForKey:kPNPublishSequenceDataKey withCompletionBlock:^(NSDictionary *sequences) {
314313

315-
if (![publishKey isEqualToString:self.publishKey]) {
314+
NSMutableDictionary *mutableSequences = [(sequences?: @{}) mutableCopy];
315+
[mutableSequences enumerateKeysAndObjectsUsingBlock:^(NSString *publishKey,
316+
NSDictionary *sequenceData,
317+
BOOL *sequencesEnumeratorStop) {
316318

317-
NSNumber *lastUpdateDate = sequenceData[PNPublishSequenceData.lastSaveDate];
318-
NSTimeInterval lastUpdateTimestamp = lastUpdateDate.doubleValue;
319-
if (ABS(currentTimestamp - lastUpdateTimestamp) > kPNMaximumPublishSequenceDataAge) {
319+
if (![publishKey isEqualToString:self.publishKey]) {
320320

321-
mutableSequences[publishKey] = nil;
321+
NSNumber *lastUpdateDate = sequenceData[PNPublishSequenceData.lastSaveDate];
322+
NSTimeInterval lastUpdateTimestamp = lastUpdateDate.doubleValue;
323+
if (ABS(currentTimestamp - lastUpdateTimestamp) > kPNMaximumPublishSequenceDataAge) {
324+
325+
mutableSequences[publishKey] = nil;
326+
}
322327
}
323-
}
328+
}];
329+
330+
[PNKeychain storeValue:mutableSequences forKey:kPNPublishSequenceDataKey
331+
withCompletionBlock:^(BOOL stored) { completion(); }];
324332
}];
325-
326-
[PNKeychain storeValue:mutableSequences forKey:kPNPublishSequenceDataKey
327-
withCompletionBlock:^(BOOL stored) { OSSpinLockUnlock(&publishSequenceKeychainAccessLock); }];
328-
}];
333+
});
329334
}
330335

331336

PubNub/Data/PNAES.m

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#import <CommonCrypto/CommonCryptor.h>
88
#import <CommonCrypto/CommonHMAC.h>
99
#import "PubNub+CorePrivate.h"
10-
#import <libkern/OSAtomic.h>
1110
#import "PNErrorCodes.h"
1211
#import "PNConstants.h"
1312
#import "PNLogMacro.h"
@@ -214,24 +213,26 @@ + (NSData *)decrypt:(NSString *)object withKey:(NSString *)key andError:(NSError
214213

215214
+ (NSData *)SHA256HexFromKey:(NSString *)cipherKey {
216215

217-
static OSSpinLock _cipherKeysSpinLock;
216+
static os_unfair_lock _cipherKeysSpinLock;
218217
static NSMutableDictionary *_cipherKeys;
219218
static dispatch_once_t onceToken;
220219
dispatch_once(&onceToken, ^{
221220

222-
_cipherKeysSpinLock = OS_SPINLOCK_INIT;
221+
_cipherKeysSpinLock = OS_UNFAIR_LOCK_INIT;
223222
_cipherKeys = [NSMutableDictionary new];
224223
});
225224

226-
OSSpinLockLock(&_cipherKeysSpinLock);
227-
NSData *key = _cipherKeys[cipherKey];
228-
if (!key) {
225+
__block NSData *key = nil;
226+
pn_lock(&_cipherKeysSpinLock, ^{
229227

230-
NSString *SHA256String = [PNData HEXFrom:[PNString SHA256DataFrom:cipherKey]];
231-
key = [PNString UTF8DataFrom:[SHA256String lowercaseString]];
232-
_cipherKeys[cipherKey] = key;
233-
}
234-
OSSpinLockUnlock(&_cipherKeysSpinLock);
228+
key = _cipherKeys[cipherKey];
229+
if (!key) {
230+
231+
NSString *SHA256String = [PNData HEXFrom:[PNString SHA256DataFrom:cipherKey]];
232+
key = [PNString UTF8DataFrom:[SHA256String lowercaseString]];
233+
_cipherKeys[cipherKey] = key;
234+
}
235+
});
235236

236237
return key;
237238
}

PubNub/Misc/Helpers/PNDefines.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
@author Sergey Mamontov
3+
@since 4.5.15
4+
@copyright © 2009-2017 PubNub, Inc.
5+
*/
6+
#ifndef PNDefines_h
7+
#define PNDefines_h
8+
9+
// Whether iOS/tvOS 10.0, watchOS 3.0 and macOS 10.12 SDK API can be used (project's base SDK set to allow).
10+
#define PN_OS_VERSION_10_SDK_API_AVAILABLE 0
11+
#if (TARGET_OS_IOS && __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000) || \
12+
(TARGET_OS_WATCH && __WATCH_OS_VERSION_MAX_ALLOWED >= 30000) || \
13+
(TARGET_OS_TV && __TV_OS_VERSION_MAX_ALLOWED >= 100000) || \
14+
(TARGET_OS_OSX && __MAC_OS_X_VERSION_MAX_ALLOWED >= 101200)
15+
#undef PN_OS_VERSION_10_SDK_API_AVAILABLE
16+
#define PN_OS_VERSION_10_SDK_API_AVAILABLE 1
17+
#endif
18+
19+
// Whether iOS/tvOS 10.0, watchOS 3.0 and macOS 10.12 SDK API can be used safely basing on minimum allowed OS
20+
// version which is set in project.
21+
#define PN_OS_VERSION_10_SDK_API_IS_SAFE 0
22+
#if (TARGET_OS_IOS && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000) || \
23+
(TARGET_OS_WATCH && __WATCH_OS_VERSION_MIN_REQUIRED >= 30000) || \
24+
(TARGET_OS_TV && __TV_OS_VERSION_MIN_REQUIRED >= 100000) || \
25+
(TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200)
26+
#undef PN_OS_VERSION_10_SDK_API_IS_SAFE
27+
#define PN_OS_VERSION_10_SDK_API_IS_SAFE 1
28+
#endif
29+
30+
#if PN_OS_VERSION_10_SDK_API_AVAILABLE
31+
#define PN_URLSESSION_TRANSACTION_METRICS_AVAILABLE 1
32+
#define PN_OS_UNFAIR_LOCK_AVAILABILE 1
33+
#else
34+
#define PN_URLSESSION_TRANSACTION_METRICS_AVAILABLE 0
35+
#define PN_OS_UNFAIR_LOCK_AVAILABILE 0
36+
#endif
37+
38+
#endif // PNDefines_h

PubNub/Misc/Helpers/PNHelpers.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/**
22
@author Sergey Mamontov
33
@since 4.0
4-
@copyright © 2009-2016 PubNub, Inc.
4+
@copyright © 2009-2017 PubNub, Inc.
55
*/
66
#ifndef PNHelpers_h
77
#define PNHelpers_h
88

9+
#import "PNLockSupport.h"
910
#import "PNURLRequest.h"
1011
#import "PNDictionary.h"
12+
#import "PNDefines.h"
1113
#import "PNChannel.h"
1214
#import "PNString.h"
1315
#import "PNNumber.h"
@@ -17,6 +19,8 @@
1719
#import "PNGZIP.h"
1820

1921

22+
#pragma mark - GCD helpers
23+
2024
/**
2125
@brief GCD async block call wrapper.
2226
@discussion Wrapper allow to verify whether all passed data not \c nil and can be used with

0 commit comments

Comments
 (0)