t_threads.m revision 1.1.12.1 1 /* $NetBSD: t_threads.m,v 1.1.12.1 2014/08/20 00:04:51 tls Exp $ */
2
3 /*
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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.
27 */
28
29 /* Originally written by David Wetzel */
30
31 #include <assert.h>
32 #include <stdio.h>
33 #include <unistd.h>
34
35 #include <atf-c.h>
36
37 #include <objc/objc.h>
38 #include <objc/thr.h>
39 #include <objc/Object.h>
40 #if __GNUC_PREREQ__(4,8)
41 #include <objc/runtime.h>
42 #endif
43
44 static int IsMultithreaded = 0;
45 static objc_mutex_t Mutex;
46 static objc_condition_t Condition;
47
48 @interface MyClass : Object
49 {
50 }
51 -(void)start;
52 #if __GNUC_PREREQ__(4,8)
53 -init;
54 +new;
55 +alloc;
56 -free;
57 #endif
58 @end
59
60 @implementation MyClass
61 -(void)start
62 {
63 printf("detached thread started!\n");
64
65 objc_condition_signal(Condition);
66 }
67 #if __GNUC_PREREQ__(4,8)
68 -init
69 {
70 return self;
71 }
72
73 +new
74 {
75 return [[self alloc] init];
76 }
77
78 +alloc
79 {
80 return class_createInstance(self, 0);
81 }
82
83 -free
84 {
85 return object_dispose(self);
86 }
87 #endif
88 @end
89
90 static void
91 becomeMultiThreaded(void)
92 {
93 printf("becoming multithreaded!\n");
94 IsMultithreaded++;
95 }
96
97 ATF_TC(thread_callback);
98 ATF_TC_HEAD(thread_callback, tc)
99 {
100 atf_tc_set_md_var(tc, "descr", "Checks that the thread callback is only"
101 "called once");
102 }
103 ATF_TC_BODY(thread_callback, tc)
104 {
105 id o = [MyClass new];
106 objc_thread_callback cb;
107 objc_thread_t rv;
108
109 cb = objc_set_thread_callback(becomeMultiThreaded);
110 printf("Old Callback: %p\n", cb);
111 ATF_CHECK(cb == 0);
112
113 Mutex = objc_mutex_allocate();
114 Condition = objc_condition_allocate();
115
116 ATF_CHECK_EQ(0, IsMultithreaded);
117
118 rv = objc_thread_detach(@selector(start), o, nil);
119 printf("detach value: %p\n", rv);
120 assert(rv != NULL);
121
122 objc_mutex_lock(Mutex);
123 objc_condition_wait(Condition, Mutex);
124 objc_mutex_unlock(Mutex);
125
126 ATF_CHECK_EQ(1, IsMultithreaded);
127 printf("Shutting down\n");
128 }
129
130 ATF_TP_ADD_TCS(tp)
131 {
132
133 ATF_TP_ADD_TC(tp, thread_callback);
134
135 return atf_no_error();
136 }
137