Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
[iOS] Fix ios mutlithread related issues. Refactor WXThreadSafeMutabl…
Browse files Browse the repository at this point in the history
…eXXX containers. (#1716)
  • Loading branch information
wqyfavor authored and cxfeng1 committed Nov 7, 2018
1 parent 04eee5c commit dbde671
Show file tree
Hide file tree
Showing 16 changed files with 284 additions and 255 deletions.
1 change: 0 additions & 1 deletion ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#import "WXSDKManager.h"
#import "WXDebugTool.h"
#import "WXSDKInstance_private.h"
#import "WXThreadSafeMutableArray.h"
#import "WXAppConfiguration.h"
#import "WXInvocationConfig.h"
#import "WXComponentMethod.h"
Expand Down
9 changes: 2 additions & 7 deletions ios/sdk/WeexSDK/Sources/Component/WXListComponent.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#import "WXSDKInstance_private.h"
#import "WXRefreshComponent.h"
#import "WXLoadingComponent.h"
#import "WXThreadSafeMutableArray.h"

@interface WXTableView : UITableView

Expand Down Expand Up @@ -95,12 +94,8 @@ @implementation WXSectionComponent
- (instancetype)init
{
if (self = [super init]) {
if ([WXUtility listSectionRowThreadSafe]) {
_rows = [WXThreadSafeMutableArray array];
} else {
_rows = [NSMutableArray array];
} }

_rows = [NSMutableArray array];
}
return self;
}

Expand Down
8 changes: 3 additions & 5 deletions ios/sdk/WeexSDK/Sources/Controller/WXRootViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

#import "WXRootViewController.h"
#import "WXBaseViewController.h"
#import "WXThreadSafeMutableArray.h"
#import "WXDefine.h"

typedef void(^OperationBlock)(void);

@interface WXRootViewController() <UIGestureRecognizerDelegate>

@property(nonatomic, strong) WXThreadSafeMutableArray *operationArray;
@property (nonatomic, strong) NSMutableArray *operationArray;
@property (nonatomic, assign) BOOL operationInProcess;

@end
Expand Down Expand Up @@ -131,11 +130,10 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
return YES;
}

- (NSMutableArray *)pendingBlocks
- (NSMutableArray *)operationArray
{

if (nil == _operationArray) {
_operationArray = [[WXThreadSafeMutableArray alloc] init];
_operationArray = [[NSMutableArray alloc] init];
}

return _operationArray;
Expand Down
17 changes: 8 additions & 9 deletions ios/sdk/WeexSDK/Sources/Manager/WXBridgeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
#import "WXTracingManager.h"
#import "WXMonitor.h"
#import "WXSDKInstance_performance.h"
#import "WXThreadSafeMutableArray.h"

@interface WXBridgeManager ()

@property (nonatomic, strong) WXBridgeContext *bridgeCtx;
@property (nonatomic, assign) BOOL stopRunning;
@property (nonatomic, strong) NSMutableArray *instanceIdStack;
@property (nonatomic, assign) BOOL stopRunning;
@property (nonatomic, strong) WXBridgeContext *bridgeCtx;
@property (nonatomic, strong) WXThreadSafeMutableArray *instanceIdStack;

@end

Expand Down Expand Up @@ -202,26 +203,24 @@ - (void)createInstance:(NSString *)instance
}


- (NSMutableArray *)instanceIdStack
- (WXThreadSafeMutableArray *)instanceIdStack
{
if (_instanceIdStack) return _instanceIdStack;

_instanceIdStack = [NSMutableArray array];
_instanceIdStack = [[WXThreadSafeMutableArray alloc] init];

return _instanceIdStack;
}

- (NSArray *)getInstanceIdStack;
{
return self.instanceIdStack;
return [self.instanceIdStack copy];
}

- (void)destroyInstance:(NSString *)instance
{
if (!instance) return;
if([self.instanceIdStack containsObject:instance]){
[self.instanceIdStack removeObject:instance];
}
[self.instanceIdStack removeObject:instance];

__weak typeof(self) weakSelf = self;
WXPerformBlockOnBridgeThread(^(){
Expand Down
6 changes: 5 additions & 1 deletion ios/sdk/WeexSDK/Sources/Manager/WXComponentManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ - (instancetype)initWithWeexInstance:(id)weexInstance
pthread_mutexattr_init(&_propertMutexAttr);
pthread_mutexattr_settype(&_propertMutexAttr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&_propertyMutex, &_propertMutexAttr);
[self _startDisplayLink];

WXPerformBlockOnComponentThread(^{
// We should ensure that [WXDisplayLinkManager sharedInstance] is only invoked in component thread.
[self _startDisplayLink];
});
}

return self;
Expand Down
2 changes: 1 addition & 1 deletion ios/sdk/WeexSDK/Sources/Manager/WXSDKManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ + (void)removeInstanceforID:(NSString *)identifier

+ (void)unload
{
for (NSString *instanceID in [self sharedInstance].instanceDict) {
for (NSString *instanceID in [[self sharedInstance].instanceDict allKeys]) {
WXSDKInstance *instance = [[self sharedInstance].instanceDict objectForKey:instanceID];
[instance destroyInstance];
}
Expand Down
2 changes: 0 additions & 2 deletions ios/sdk/WeexSDK/Sources/Model/WXComponent.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#import "WXConvert.h"
#import "WXMonitor.h"
#import "WXAssert.h"
#import "WXThreadSafeMutableDictionary.h"
#import "WXThreadSafeMutableArray.h"
#import "WXTransform.h"
#import "WXRoundedRect.h"
#import <pthread/pthread.h>
Expand Down
4 changes: 2 additions & 2 deletions ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ extern NSString *const bundleUrlOptionKey;
@property (nonatomic, strong) NSDictionary* containerInfo;

/**
* Whether this instance is rendered or not. Please MUST not render an instance twice.
* Whether this instance is rendered or not. Please MUST not render an instance twice even if you have called destroyInstance.
**/
@property (nonatomic, assign, readonly) BOOL isRendered;

Expand Down Expand Up @@ -276,7 +276,7 @@ typedef NS_ENUM(NSInteger, WXErrorCode) {//error.code
- (void)refreshInstance:(id)data;

/**
* Destroys current instance.
* Destroys current instance. An instance destroyed should not be used for rendering again, please create another instance.
**/
- (void)destroyInstance;

Expand Down
34 changes: 7 additions & 27 deletions ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ @implementation WXSDKInstance
WXRootView *_rootView;
WXThreadSafeMutableDictionary *_moduleEventObservers;
BOOL _performanceCommit;
BOOL _syncDestroyComponentManager;
BOOL _debugJS;
id<WXBridgeProtocol> _instanceJavaScriptContext; // sandbox javaScript context
CGFloat _defaultPixelScaleFactor;
Expand All @@ -87,11 +86,6 @@ - (void)dealloc
{
[_moduleEventObservers removeAllObjects];
[self removeObservers];
if (_syncDestroyComponentManager) {
WXPerformBlockSyncOnComponentThread(^{
_componentManager = nil;
});
}
}

- (instancetype)init
Expand Down Expand Up @@ -123,10 +117,6 @@ - (instancetype)init
_performance = [[WXPerformance alloc] init];
_apmInstance = [[WXApmForInstance alloc] init];

id configCenter = [WXSDKEngine handlerForProtocol:@protocol(WXConfigCenterProtocol)];
if ([configCenter respondsToSelector:@selector(configForKey:defaultValue:isDefault:)]) {
_syncDestroyComponentManager = [[configCenter configForKey:@"iOS_weex_ext_config.syncDestroyComponentManager" defaultValue:@(YES) isDefault:NULL] boolValue];
}
_defaultPixelScaleFactor = CGFLOAT_MIN;
_bReleaseInstanceInMainThread = YES;
_defaultDataRender = NO;
Expand Down Expand Up @@ -447,14 +437,9 @@ - (BOOL)_handleConfigCenter
if ([configCenter respondsToSelector:@selector(configForKey:defaultValue:isDefault:)]) {
BOOL useCoreText = [[configCenter configForKey:@"iOS_weex_ext_config.text_render_useCoreText" defaultValue:@YES isDefault:NULL] boolValue];
[WXTextComponent setRenderUsingCoreText:useCoreText];
BOOL useThreadSafeLock = [[configCenter configForKey:@"iOS_weex_ext_config.useThreadSafeLock" defaultValue:@YES isDefault:NULL] boolValue];
[WXUtility setThreadSafeCollectionUsingLock:useThreadSafeLock];

BOOL unregisterFontWhenCollision = [[configCenter configForKey:@"iOS_weex_ext_config.unregisterFontWhenCollision" defaultValue:@NO isDefault:NULL] boolValue];
[WXUtility setUnregisterFontWhenCollision:unregisterFontWhenCollision];

BOOL listSectionRowThreadSafe = [[configCenter configForKey:@"iOS_weex_ext_config.listSectionRowThreadSafe" defaultValue:@(YES) isDefault:NULL] boolValue];
[WXUtility setListSectionRowThreadSafe:listSectionRowThreadSafe];

BOOL useJSCApiForCreateInstance = [[configCenter configForKey:@"iOS_weex_ext_config.useJSCApiForCreateInstance" defaultValue:@(YES) isDefault:NULL] boolValue];
[WXUtility setUseJSCApiForCreateInstance:useJSCApiForCreateInstance];
Expand Down Expand Up @@ -663,25 +648,22 @@ - (void)destroyInstance
[WXPrerenderManager destroyTask:self.instanceId];
[[WXSDKManager bridgeMgr] destroyInstance:self.instanceId];

__weak typeof(self) weakSelf = self;
WXComponentManager* componentManager = self.componentManager;
NSString* instanceId = self.instanceId;

WXPerformBlockOnComponentThread(^{
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf == nil) {
return;
}

// Destroy components and views in main thread. Unbind with underneath RenderObjects.
[strongSelf.componentManager unload];
[componentManager unload];

// Destroy weexcore c++ page and objects.
[WXCoreBridge closePage:strongSelf.instanceId];
[WXCoreBridge closePage:instanceId];

// Reading config from orange for Release instance in Main Thread or not, for Bug #15172691 +{
if (!_bReleaseInstanceInMainThread) {
[WXSDKManager removeInstanceforID:strongSelf.instanceId];
[WXSDKManager removeInstanceforID:instanceId];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
[WXSDKManager removeInstanceforID:strongSelf.instanceId];
[WXSDKManager removeInstanceforID:instanceId];
});
}
//+}
Expand All @@ -690,8 +672,6 @@ - (void)destroyInstance
if (url.length > 0) {
[WXPrerenderManager addGlobalTask:url callback:nil];
}

_isRendered = NO;
}

- (void)forceGarbageCollection
Expand Down
24 changes: 24 additions & 0 deletions ios/sdk/WeexSDK/Sources/Utility/WXThreadSafeMutableArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,28 @@
*/
@interface WXThreadSafeMutableArray : NSMutableArray

- (instancetype)init;
- (instancetype)initWithCapacity:(NSUInteger)numItems;
- (instancetype)initWithArray:(NSArray *)array;
- (instancetype)initWithCoder:(NSCoder *)aDecoder;
- (instancetype)initWithObjects:(const id [])objects count:(NSUInteger)cnt;

- (NSUInteger)count;
- (id)objectAtIndex:(NSUInteger)index;
- (id)objectAtIndexedSubscript:(NSUInteger)index;
- (id)firstObject;
- (id)lastObject;
- (BOOL)containsObject:(id)anObject;
- (NSEnumerator *)objectEnumerator;
- (NSEnumerator *)reverseObjectEnumerator;
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
- (void)setObject:(id)anObject atIndexedSubscript:(NSUInteger)index;
- (void)addObject:(id)anObject;
- (void)removeObject:(id)anObject;
- (void)removeObjectAtIndex:(NSUInteger)index;
- (void)removeLastObject;
- (void)removeAllObjects;
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
- (NSUInteger)indexOfObject:(id)anObject;

@end
Loading

0 comments on commit dbde671

Please sign in to comment.