xref: /netbsd/tests/lib/libobjc/t_threads.m (revision 6550d01e)
1/* $NetBSD: t_threads.m,v 1.1 2010/07/18 12:41:51 jmmv 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/objc-api.h>
39#include <objc/Object.h>
40
41static int IsMultithreaded = 0;
42static objc_mutex_t Mutex;
43static objc_condition_t Condition;
44
45@interface MyClass : Object
46{
47}
48-(void)start;
49@end
50
51@implementation MyClass
52-(void)start
53{
54	printf("detached thread started!\n");
55
56	objc_condition_signal(Condition);
57}
58@end
59
60static void
61becomeMultiThreaded(void)
62{
63	printf("becoming multithreaded!\n");
64	IsMultithreaded++;
65}
66
67ATF_TC(thread_callback);
68ATF_TC_HEAD(thread_callback, tc)
69{
70	atf_tc_set_md_var(tc, "descr", "Checks that the thread callback is only"
71	    "called once");
72}
73ATF_TC_BODY(thread_callback, tc)
74{
75	id o = [MyClass new];
76	objc_thread_callback cb;
77	objc_thread_t rv;
78
79	cb = objc_set_thread_callback(becomeMultiThreaded);
80	printf("Old Callback: %p\n", cb);
81	ATF_CHECK(cb == 0);
82
83	Mutex = objc_mutex_allocate();
84	Condition = objc_condition_allocate();
85
86	ATF_CHECK_EQ(0, IsMultithreaded);
87
88	rv = objc_thread_detach(@selector(start), o, nil);
89	printf("detach value: %p\n", rv);
90	assert(rv != NULL);
91
92	objc_mutex_lock(Mutex);
93	objc_condition_wait(Condition, Mutex);
94	objc_mutex_unlock(Mutex);
95
96	ATF_CHECK_EQ(1, IsMultithreaded);
97	printf("Shutting down\n");
98}
99
100ATF_TP_ADD_TCS(tp)
101{
102
103	ATF_TP_ADD_TC(tp, thread_callback);
104
105	return atf_no_error();
106}
107