1 1.2 christos /* $NetBSD: t_threads.m,v 1.2 2013/10/31 21:02:11 christos Exp $ */ 2 1.1 jmmv 3 1.1 jmmv /* 4 1.1 jmmv * Copyright (c) 2010 The NetBSD Foundation, Inc. 5 1.1 jmmv * All rights reserved. 6 1.1 jmmv * 7 1.1 jmmv * Redistribution and use in source and binary forms, with or without 8 1.1 jmmv * modification, are permitted provided that the following conditions 9 1.1 jmmv * are met: 10 1.1 jmmv * 1. Redistributions of source code must retain the above copyright 11 1.1 jmmv * notice, this list of conditions and the following disclaimer. 12 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmmv * notice, this list of conditions and the following disclaimer in the 14 1.1 jmmv * documentation and/or other materials provided with the distribution. 15 1.1 jmmv * 16 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 jmmv * POSSIBILITY OF SUCH DAMAGE. 27 1.1 jmmv */ 28 1.1 jmmv 29 1.1 jmmv /* Originally written by David Wetzel */ 30 1.1 jmmv 31 1.1 jmmv #include <assert.h> 32 1.1 jmmv #include <stdio.h> 33 1.1 jmmv #include <unistd.h> 34 1.1 jmmv 35 1.1 jmmv #include <atf-c.h> 36 1.1 jmmv 37 1.1 jmmv #include <objc/objc.h> 38 1.2 christos #include <objc/thr.h> 39 1.1 jmmv #include <objc/Object.h> 40 1.2 christos #if __GNUC_PREREQ__(4,8) 41 1.2 christos #include <objc/runtime.h> 42 1.2 christos #endif 43 1.1 jmmv 44 1.1 jmmv static int IsMultithreaded = 0; 45 1.1 jmmv static objc_mutex_t Mutex; 46 1.1 jmmv static objc_condition_t Condition; 47 1.1 jmmv 48 1.1 jmmv @interface MyClass : Object 49 1.1 jmmv { 50 1.1 jmmv } 51 1.1 jmmv -(void)start; 52 1.2 christos #if __GNUC_PREREQ__(4,8) 53 1.2 christos -init; 54 1.2 christos +new; 55 1.2 christos +alloc; 56 1.2 christos -free; 57 1.2 christos #endif 58 1.1 jmmv @end 59 1.1 jmmv 60 1.1 jmmv @implementation MyClass 61 1.1 jmmv -(void)start 62 1.1 jmmv { 63 1.1 jmmv printf("detached thread started!\n"); 64 1.1 jmmv 65 1.1 jmmv objc_condition_signal(Condition); 66 1.1 jmmv } 67 1.2 christos #if __GNUC_PREREQ__(4,8) 68 1.2 christos -init 69 1.2 christos { 70 1.2 christos return self; 71 1.2 christos } 72 1.2 christos 73 1.2 christos +new 74 1.2 christos { 75 1.2 christos return [[self alloc] init]; 76 1.2 christos } 77 1.2 christos 78 1.2 christos +alloc 79 1.2 christos { 80 1.2 christos return class_createInstance(self, 0); 81 1.2 christos } 82 1.2 christos 83 1.2 christos -free 84 1.2 christos { 85 1.2 christos return object_dispose(self); 86 1.2 christos } 87 1.2 christos #endif 88 1.1 jmmv @end 89 1.1 jmmv 90 1.1 jmmv static void 91 1.1 jmmv becomeMultiThreaded(void) 92 1.1 jmmv { 93 1.1 jmmv printf("becoming multithreaded!\n"); 94 1.1 jmmv IsMultithreaded++; 95 1.1 jmmv } 96 1.1 jmmv 97 1.1 jmmv ATF_TC(thread_callback); 98 1.1 jmmv ATF_TC_HEAD(thread_callback, tc) 99 1.1 jmmv { 100 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Checks that the thread callback is only" 101 1.1 jmmv "called once"); 102 1.1 jmmv } 103 1.1 jmmv ATF_TC_BODY(thread_callback, tc) 104 1.1 jmmv { 105 1.1 jmmv id o = [MyClass new]; 106 1.1 jmmv objc_thread_callback cb; 107 1.1 jmmv objc_thread_t rv; 108 1.1 jmmv 109 1.1 jmmv cb = objc_set_thread_callback(becomeMultiThreaded); 110 1.1 jmmv printf("Old Callback: %p\n", cb); 111 1.1 jmmv ATF_CHECK(cb == 0); 112 1.1 jmmv 113 1.1 jmmv Mutex = objc_mutex_allocate(); 114 1.1 jmmv Condition = objc_condition_allocate(); 115 1.1 jmmv 116 1.1 jmmv ATF_CHECK_EQ(0, IsMultithreaded); 117 1.1 jmmv 118 1.1 jmmv rv = objc_thread_detach(@selector(start), o, nil); 119 1.1 jmmv printf("detach value: %p\n", rv); 120 1.1 jmmv assert(rv != NULL); 121 1.1 jmmv 122 1.1 jmmv objc_mutex_lock(Mutex); 123 1.1 jmmv objc_condition_wait(Condition, Mutex); 124 1.1 jmmv objc_mutex_unlock(Mutex); 125 1.1 jmmv 126 1.1 jmmv ATF_CHECK_EQ(1, IsMultithreaded); 127 1.1 jmmv printf("Shutting down\n"); 128 1.1 jmmv } 129 1.1 jmmv 130 1.1 jmmv ATF_TP_ADD_TCS(tp) 131 1.1 jmmv { 132 1.1 jmmv 133 1.1 jmmv ATF_TP_ADD_TC(tp, thread_callback); 134 1.1 jmmv 135 1.1 jmmv return atf_no_error(); 136 1.1 jmmv } 137