1/* 2 * Copyright (c) 2011 Apple Inc. All rights reserved. 3 * 4 * @APPLE_APACHE_LICENSE_HEADER_START@ 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 * @APPLE_APACHE_LICENSE_HEADER_END@ 19 */ 20// 21// MainThreadFinalization.m 22// Copyright (c) 2009-2011 Apple Inc. All rights reserved. 23// 24 25#include "BlackBoxTest.h" 26#include <objc/objc-auto.h> 27#include <objc/runtime.h> 28#include <dispatch/dispatch.h> 29 30@interface MainThreadFinalizer : TestFinalizer 31@end 32 33@implementation MainThreadFinalizer 34 35+ (void)initialize 36{ 37 objc_finalizeOnMainThread(self); 38} 39 40@end 41 42 43@interface MainThreadFinalization : BlackBoxTest 44{ 45 BOOL _finalized; 46} 47 48@end 49 50@implementation MainThreadFinalization 51 52- (void)allocate 53{ 54 // force test object out of thread local 55 size_t size = class_getInstanceSize([MainThreadFinalizer class]); 56 for (int i=0; i<1024*1024/size*2; i++) 57 CFRelease(CFRetain([MainThreadFinalizer new])); 58} 59 60- (void)performTest 61{ 62 _finalized = NO; 63 [self allocate]; 64 [self clearStack]; 65 dispatch_sync(dispatch_get_main_queue(), ^{ 66 objc_collect(OBJC_WAIT_UNTIL_DONE|OBJC_FULL_COLLECTION); 67 } ); 68 if (_finalized) 69 [self passed]; 70 else 71 [self fail:@"Did not finalize main thread object"]; 72 [self testFinished]; 73} 74 75- (void)didFinalize:(TestFinalizer *)finalizer 76{ 77 if (!pthread_main_np()) 78 [self fail:@"Finalized called on non main thread"]; 79 _finalized = YES; 80} 81 82@end 83