xref: /haiku/src/tests/system/kernel/unit/TestThread.h (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef TEST_THREAD_H
6 #define TEST_THREAD_H
7 
8 
9 #include <KernelExport.h>
10 
11 #include "TestContext.h"
12 
13 
14 template<typename ObjectType, typename ParameterType>
15 class TestThread {
16 public:
17 	TestThread(ObjectType* object,
18 		void (ObjectType::*method)(TestContext&, ParameterType*),
19 		ParameterType* argument)
20 		:
21 		fObject(object),
22 		fMethod(method),
23 		fArgument(argument)
24 	{
25 	}
26 
27 	thread_id Spawn(const char* name, int32 priority)
28 	{
29 		return GlobalTestContext::Current()->SpawnThread(_Entry, name, priority,
30 			this);
31 	}
32 
33 private:
34 	static status_t _Entry(void* data)
35 	{
36 		TestThread* thread = (TestThread*)data;
37 		(thread->fObject->*thread->fMethod)(
38 			*GlobalTestContext::Current()->CurrentContext(), thread->fArgument);
39 		delete thread;
40 		return B_OK;
41 	}
42 
43 private:
44 	ObjectType*		fObject;
45 	void			(ObjectType::*fMethod)(TestContext&, ParameterType*);
46 	ParameterType*	fArgument;
47 };
48 
49 
50 template<typename ObjectType, typename ParameterType>
51 thread_id
52 SpawnThread(ObjectType* object,
53 	void (ObjectType::*method)(TestContext&, ParameterType*), const char* name,
54 	int32 priority, ParameterType* arg)
55 {
56 	TestThread<ObjectType, ParameterType>* thread
57 		= new(std::nothrow) TestThread<ObjectType, ParameterType>(object,
58 			method, arg);
59 	if (thread == NULL)
60 		return B_NO_MEMORY;
61 
62 	return thread->Spawn(name, priority);
63 }
64 
65 
66 #endif	// TEST_THREAD_H
67