Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: webrtc/sdk/objc/Framework/Classes/Common/RTCUIApplicationStatusObserver.m

Issue 3009383002: Enhance RTCUIApplicationStatusObserver thread safety. (Closed)
Patch Set: Rebase Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "RTCUIApplicationStatusObserver.h" 11 #include "RTCUIApplicationStatusObserver.h"
12 12
13 #if defined(WEBRTC_IOS) 13 #if defined(WEBRTC_IOS)
14 14
15 #import <UIKit/UIKit.h> 15 #import <UIKit/UIKit.h>
16 16
17 #include "webrtc/rtc_base/checks.h" 17 #include "webrtc/rtc_base/checks.h"
18 18
19 @interface RTCUIApplicationStatusObserver () 19 @interface RTCUIApplicationStatusObserver ()
20 20
21 @property(nonatomic, assign) BOOL initialized; 21 @property(nonatomic, assign) BOOL initialized;
22 @property(nonatomic, assign) UIApplicationState state; 22 @property(nonatomic, assign) UIApplicationState state;
tkchin_webrtc 2017/09/13 18:32:02 State is read and written to from multiple threads
andersc 2017/09/14 08:08:45 Acknowledged.
23 23
24 @end 24 @end
25 25
26 @implementation RTCUIApplicationStatusObserver { 26 @implementation RTCUIApplicationStatusObserver {
27 BOOL _initialized; 27 BOOL _initialized;
28 dispatch_block_t _initializeBlock; 28 dispatch_block_t _initializeBlock;
29 dispatch_semaphore_t _waitForInitializeSemaphore;
29 UIApplicationState _state; 30 UIApplicationState _state;
30 31
31 id<NSObject> _activeObserver; 32 id<NSObject> _activeObserver;
32 id<NSObject> _backgroundObserver; 33 id<NSObject> _backgroundObserver;
33 } 34 }
34 35
35 @synthesize initialized = _initialized; 36 @synthesize initialized = _initialized;
36 @synthesize state = _state; 37 @synthesize state = _state;
37 38
38 + (instancetype)sharedInstance { 39 + (instancetype)sharedInstance {
39 static id sharedInstance; 40 static id sharedInstance;
40 static dispatch_once_t onceToken; 41 static dispatch_once_t onceToken;
41 dispatch_once(&onceToken, ^{ 42 dispatch_once(&onceToken, ^{
42 sharedInstance = [[self alloc] init]; 43 sharedInstance = [[self alloc] init];
43 }); 44 });
44 45
45 return sharedInstance; 46 return sharedInstance;
46 } 47 }
47 48
49 // Method to make sure observers are added and the initialization block is
50 // scheduled to run on the main queue.
51 + (void)prepareForUse {
tkchin_webrtc 2017/09/13 18:32:02 This class feels more complex than it needs to be.
andersc 2017/09/14 08:08:45 I agree, and I mentioned a solution similar to tha
52 __unused RTCUIApplicationStatusObserver *observer = [self sharedInstance];
53 }
54
48 - (id)init { 55 - (id)init {
49 if (self = [super init]) { 56 if (self = [super init]) {
50 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 57 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
51 __weak RTCUIApplicationStatusObserver *weakSelf = self; 58 __weak RTCUIApplicationStatusObserver *weakSelf = self;
52 _activeObserver = [center addObserverForName:UIApplicationDidBecomeActiveNot ification 59 _activeObserver = [center addObserverForName:UIApplicationDidBecomeActiveNot ification
53 object:nil 60 object:nil
54 queue:[NSOperationQueue mainQueue] 61 queue:[NSOperationQueue mainQueue]
55 usingBlock:^(NSNotification *note) { 62 usingBlock:^(NSNotification *note) {
56 weakSelf.state = 63 weakSelf.state =
57 [UIApplication sharedApplication].ap plicationState; 64 [UIApplication sharedApplication].ap plicationState;
58 }]; 65 }];
59 66
60 _backgroundObserver = [center addObserverForName:UIApplicationDidEnterBackgr oundNotification 67 _backgroundObserver = [center addObserverForName:UIApplicationDidEnterBackgr oundNotification
61 object:nil 68 object:nil
62 queue:[NSOperationQueue mainQueue ] 69 queue:[NSOperationQueue mainQueue ]
63 usingBlock:^(NSNotification *note) { 70 usingBlock:^(NSNotification *note) {
64 weakSelf.state = 71 weakSelf.state =
65 [UIApplication sharedApplication ].applicationState; 72 [UIApplication sharedApplication ].applicationState;
66 }]; 73 }];
67 74
75 _waitForInitializeSemaphore = dispatch_semaphore_create(1);
68 _initialized = NO; 76 _initialized = NO;
69 _initializeBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^ { 77 _initializeBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^ {
70 weakSelf.state = [UIApplication sharedApplication].applicationState; 78 weakSelf.state = [UIApplication sharedApplication].applicationState;
71 weakSelf.initialized = YES; 79 weakSelf.initialized = YES;
72 }); 80 });
73 81
74 dispatch_async(dispatch_get_main_queue(), _initializeBlock); 82 dispatch_async(dispatch_get_main_queue(), _initializeBlock);
75 } 83 }
76 84
77 return self; 85 return self;
78 } 86 }
79 87
80 - (void)dealloc { 88 - (void)dealloc {
81 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 89 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
82 [center removeObserver:_activeObserver]; 90 [center removeObserver:_activeObserver];
83 [center removeObserver:_backgroundObserver]; 91 [center removeObserver:_backgroundObserver];
84 } 92 }
85 93
86 - (BOOL)isApplicationActive { 94 - (BOOL)isApplicationActive {
95 // NOTE: The function `dispatch_block_wait` can only legally be called once.
96 // Because of this, if several threads call the `isApplicationActive` method b efore
97 // the `_initializeBlock` has been executed, instead of multiple threads calli ng
98 // `dispatch_block_wait`, the other threads need to wait for the first waiting thread
99 // instead.
87 if (!_initialized) { 100 if (!_initialized) {
88 long ret = dispatch_block_wait(_initializeBlock, 101 dispatch_semaphore_wait(_waitForInitializeSemaphore, DISPATCH_TIME_FOREVER);
89 dispatch_time(DISPATCH_TIME_NOW, 10.0 * NSEC_ PER_SEC)); 102 if (!_initialized) {
90 RTC_DCHECK_EQ(ret, 0); 103 long ret = dispatch_block_wait(_initializeBlock,
104 dispatch_time(DISPATCH_TIME_NOW, 10.0 * NSE C_PER_SEC));
tkchin_webrtc 2017/09/13 18:32:02 What's the benefit of setting a short timeout here
andersc 2017/09/14 08:08:46 10 seconds was picked to be a long timeout. A test
105 RTC_DCHECK_EQ(ret, 0);
106 }
107 dispatch_semaphore_signal(_waitForInitializeSemaphore);
91 } 108 }
92 return _state == UIApplicationStateActive; 109 return _state == UIApplicationStateActive;
93 } 110 }
94 111
95 @end 112 @end
96 113
97 #endif // WEBRTC_IOS 114 #endif // WEBRTC_IOS
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698