1 /* $NetBSD: t_threads.m,v 1.2 2013/10/31 21:02:11 christos Exp $ */
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 /* Originally written by David Wetzel */
37 #include <objc/objc.h>
39 #include <objc/Object.h>
40 #if __GNUC_PREREQ__(4,8)
41 #include <objc/runtime.h>
44 static int IsMultithreaded = 0;
45 static objc_mutex_t Mutex;
46 static objc_condition_t Condition;
48 @interface MyClass : Object
52 #if __GNUC_PREREQ__(4,8)
60 @implementation MyClass
63 printf("detached thread started!\n");
65 objc_condition_signal(Condition);
67 #if __GNUC_PREREQ__(4,8)
75 return [[self alloc] init];
80 return class_createInstance(self, 0);
85 return object_dispose(self);
91 becomeMultiThreaded(void)
93 printf("becoming multithreaded!\n");
97 ATF_TC(thread_callback);
98 ATF_TC_HEAD(thread_callback, tc)
100 atf_tc_set_md_var(tc, "descr", "Checks that the thread callback is only"
103 ATF_TC_BODY(thread_callback, tc)
105 id o = [MyClass new];
106 objc_thread_callback cb;
109 cb = objc_set_thread_callback(becomeMultiThreaded);
110 printf("Old Callback: %p\n", cb);
113 Mutex = objc_mutex_allocate();
114 Condition = objc_condition_allocate();
116 ATF_CHECK_EQ(0, IsMultithreaded);
118 rv = objc_thread_detach(@selector(start), o, nil);
119 printf("detach value: %p\n", rv);
122 objc_mutex_lock(Mutex);
123 objc_condition_wait(Condition, Mutex);
124 objc_mutex_unlock(Mutex);
126 ATF_CHECK_EQ(1, IsMultithreaded);
127 printf("Shutting down\n");
133 ATF_TP_ADD_TC(tp, thread_callback);
135 return atf_no_error();