t_threads.m revision 272343
1272343Sngie/* $NetBSD: t_threads.m,v 1.2 2013/10/31 21:02:11 christos Exp $ */ 2272343Sngie 3272343Sngie/* 4272343Sngie * Copyright (c) 2010 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * Redistribution and use in source and binary forms, with or without 8272343Sngie * modification, are permitted provided that the following conditions 9272343Sngie * are met: 10272343Sngie * 1. Redistributions of source code must retain the above copyright 11272343Sngie * notice, this list of conditions and the following disclaimer. 12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 13272343Sngie * notice, this list of conditions and the following disclaimer in the 14272343Sngie * documentation and/or other materials provided with the distribution. 15272343Sngie * 16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26272343Sngie * POSSIBILITY OF SUCH DAMAGE. 27272343Sngie */ 28272343Sngie 29272343Sngie/* Originally written by David Wetzel */ 30272343Sngie 31272343Sngie#include <assert.h> 32272343Sngie#include <stdio.h> 33272343Sngie#include <unistd.h> 34272343Sngie 35272343Sngie#include <atf-c.h> 36272343Sngie 37272343Sngie#include <objc/objc.h> 38272343Sngie#include <objc/thr.h> 39272343Sngie#include <objc/Object.h> 40272343Sngie#if __GNUC_PREREQ__(4,8) 41272343Sngie#include <objc/runtime.h> 42272343Sngie#endif 43272343Sngie 44272343Sngiestatic int IsMultithreaded = 0; 45272343Sngiestatic objc_mutex_t Mutex; 46272343Sngiestatic objc_condition_t Condition; 47272343Sngie 48272343Sngie@interface MyClass : Object 49272343Sngie{ 50272343Sngie} 51272343Sngie-(void)start; 52272343Sngie#if __GNUC_PREREQ__(4,8) 53272343Sngie-init; 54272343Sngie+new; 55272343Sngie+alloc; 56272343Sngie-free; 57272343Sngie#endif 58272343Sngie@end 59272343Sngie 60272343Sngie@implementation MyClass 61272343Sngie-(void)start 62272343Sngie{ 63272343Sngie printf("detached thread started!\n"); 64272343Sngie 65272343Sngie objc_condition_signal(Condition); 66272343Sngie} 67272343Sngie#if __GNUC_PREREQ__(4,8) 68272343Sngie-init 69272343Sngie{ 70272343Sngie return self; 71272343Sngie} 72272343Sngie 73272343Sngie+new 74272343Sngie{ 75272343Sngie return [[self alloc] init]; 76272343Sngie} 77272343Sngie 78272343Sngie+alloc 79272343Sngie{ 80272343Sngie return class_createInstance(self, 0); 81272343Sngie} 82272343Sngie 83272343Sngie-free 84272343Sngie{ 85272343Sngie return object_dispose(self); 86272343Sngie} 87272343Sngie#endif 88272343Sngie@end 89272343Sngie 90272343Sngiestatic void 91272343SngiebecomeMultiThreaded(void) 92272343Sngie{ 93272343Sngie printf("becoming multithreaded!\n"); 94272343Sngie IsMultithreaded++; 95272343Sngie} 96272343Sngie 97272343SngieATF_TC(thread_callback); 98272343SngieATF_TC_HEAD(thread_callback, tc) 99272343Sngie{ 100272343Sngie atf_tc_set_md_var(tc, "descr", "Checks that the thread callback is only" 101272343Sngie "called once"); 102272343Sngie} 103272343SngieATF_TC_BODY(thread_callback, tc) 104272343Sngie{ 105272343Sngie id o = [MyClass new]; 106272343Sngie objc_thread_callback cb; 107272343Sngie objc_thread_t rv; 108272343Sngie 109272343Sngie cb = objc_set_thread_callback(becomeMultiThreaded); 110272343Sngie printf("Old Callback: %p\n", cb); 111272343Sngie ATF_CHECK(cb == 0); 112272343Sngie 113272343Sngie Mutex = objc_mutex_allocate(); 114272343Sngie Condition = objc_condition_allocate(); 115272343Sngie 116272343Sngie ATF_CHECK_EQ(0, IsMultithreaded); 117272343Sngie 118272343Sngie rv = objc_thread_detach(@selector(start), o, nil); 119272343Sngie printf("detach value: %p\n", rv); 120272343Sngie assert(rv != NULL); 121272343Sngie 122272343Sngie objc_mutex_lock(Mutex); 123272343Sngie objc_condition_wait(Condition, Mutex); 124272343Sngie objc_mutex_unlock(Mutex); 125272343Sngie 126272343Sngie ATF_CHECK_EQ(1, IsMultithreaded); 127272343Sngie printf("Shutting down\n"); 128272343Sngie} 129272343Sngie 130272343SngieATF_TP_ADD_TCS(tp) 131272343Sngie{ 132272343Sngie 133272343Sngie ATF_TP_ADD_TC(tp, thread_callback); 134272343Sngie 135272343Sngie return atf_no_error(); 136272343Sngie} 137