1855239e5SApple OSS Distributions /*
2855239e5SApple OSS Distributions  *  testthreadcall.cpp
3855239e5SApple OSS Distributions  *  testkext
4855239e5SApple OSS Distributions  *
5855239e5SApple OSS Distributions  */
6855239e5SApple OSS Distributions 
7855239e5SApple OSS Distributions #include "testthreadcall.h"
8855239e5SApple OSS Distributions 
9855239e5SApple OSS Distributions #include <kern/thread_call.h>
100f3703acSApple OSS Distributions #include <pexpert/pexpert.h>
11855239e5SApple OSS Distributions 
12855239e5SApple OSS Distributions #define super IOService
13855239e5SApple OSS Distributions OSDefineMetaClassAndStructors(testthreadcall, super);
14855239e5SApple OSS Distributions 
15855239e5SApple OSS Distributions extern "C" {
16855239e5SApple OSS Distributions static void thread_call_test_func(thread_call_param_t param0,
17855239e5SApple OSS Distributions     thread_call_param_t param1);
18855239e5SApple OSS Distributions 
19a3bb9fccSApple OSS Distributions static void thread_call_test_func2(thread_call_param_t param0,
20a3bb9fccSApple OSS Distributions     thread_call_param_t param1);
21855239e5SApple OSS Distributions }
22855239e5SApple OSS Distributions 
230f3703acSApple OSS Distributions static int my_event;
240f3703acSApple OSS Distributions 
25855239e5SApple OSS Distributions bool
start(IOService * provider)26855239e5SApple OSS Distributions testthreadcall::start( IOService * provider )
27855239e5SApple OSS Distributions {
28855239e5SApple OSS Distributions 	boolean_t ret;
29855239e5SApple OSS Distributions 	uint64_t deadline;
30a3bb9fccSApple OSS Distributions 	int sleepret;
310f3703acSApple OSS Distributions 	uint32_t kernel_configuration;
32855239e5SApple OSS Distributions 
33855239e5SApple OSS Distributions 	IOLog("%s\n", __PRETTY_FUNCTION__);
34855239e5SApple OSS Distributions 
35855239e5SApple OSS Distributions 	if (!super::start(provider)) {
36855239e5SApple OSS Distributions 		return false;
37855239e5SApple OSS Distributions 	}
38855239e5SApple OSS Distributions 
390f3703acSApple OSS Distributions 	kernel_configuration = PE_i_can_has_kernel_configuration();
400f3703acSApple OSS Distributions 	IOLog("%s: Assertions %s\n", __PRETTY_FUNCTION__,
410f3703acSApple OSS Distributions 	    (kernel_configuration & kPEICanHasAssertions) ? "enabled" : "disabled");
420f3703acSApple OSS Distributions 	IOLog("%s: Statistics %s\n", __PRETTY_FUNCTION__,
430f3703acSApple OSS Distributions 	    (kernel_configuration & kPEICanHasStatistics) ? "enabled" : "disabled");
440f3703acSApple OSS Distributions 	IOLog("%s: Diagnostic API %s\n", __PRETTY_FUNCTION__,
450f3703acSApple OSS Distributions 	    (kernel_configuration & kPEICanHasDiagnosticAPI) ? "enabled" : "disabled");
460f3703acSApple OSS Distributions 
47855239e5SApple OSS Distributions 	IOLog("Attempting thread_call_allocate\n");
48855239e5SApple OSS Distributions 	tcall = thread_call_allocate(thread_call_test_func, this);
49855239e5SApple OSS Distributions 	IOLog("thread_call_t %p\n", tcall);
50855239e5SApple OSS Distributions 
51855239e5SApple OSS Distributions 	tlock = IOSimpleLockAlloc();
52855239e5SApple OSS Distributions 	IOLog("tlock %p\n", tlock);
53855239e5SApple OSS Distributions 
54855239e5SApple OSS Distributions 	clock_interval_to_deadline(5, NSEC_PER_SEC, &deadline);
55855239e5SApple OSS Distributions 	IOLog("%d sec deadline is %llu\n", 5, deadline);
56855239e5SApple OSS Distributions 
57855239e5SApple OSS Distributions 	ret = thread_call_enter_delayed(tcall, deadline);
58855239e5SApple OSS Distributions 
59a3bb9fccSApple OSS Distributions 	IOLog("Attempting thread_call_allocate\n");
60a3bb9fccSApple OSS Distributions 	tcall2 = thread_call_allocate(thread_call_test_func2, this);
61a3bb9fccSApple OSS Distributions 	IOLog("thread_call_t %p\n", tcall);
62a3bb9fccSApple OSS Distributions 
63a3bb9fccSApple OSS Distributions 	tlock2 = IOLockAlloc();
64a3bb9fccSApple OSS Distributions 	IOLog("tlock2 %p\n", tlock2);
65a3bb9fccSApple OSS Distributions 
66a3bb9fccSApple OSS Distributions 	clock_interval_to_deadline(2, NSEC_PER_SEC, &deadline);
67a3bb9fccSApple OSS Distributions 	IOLog("%d sec deadline is %llu\n", 2, deadline);
68a3bb9fccSApple OSS Distributions 
69a3bb9fccSApple OSS Distributions 	ret = thread_call_enter_delayed(tcall2, deadline);
70a3bb9fccSApple OSS Distributions 
71a3bb9fccSApple OSS Distributions 	IOLockLock(tlock2);
72a3bb9fccSApple OSS Distributions 
73a3bb9fccSApple OSS Distributions 	clock_interval_to_deadline(3, NSEC_PER_SEC, &deadline);
74a3bb9fccSApple OSS Distributions 	IOLog("%d sec deadline is %llu\n", 3, deadline);
750f3703acSApple OSS Distributions 	sleepret = IOLockSleepDeadline(tlock2, &my_event, deadline, THREAD_INTERRUPTIBLE);
760f3703acSApple OSS Distributions 	IOLog("IOLockSleepDeadline(&my_event, %llu) returned %d, expected 0\n", deadline, sleepret);
77a3bb9fccSApple OSS Distributions 
78a3bb9fccSApple OSS Distributions 	IOLockUnlock(tlock2);
79a3bb9fccSApple OSS Distributions 
80a3bb9fccSApple OSS Distributions 	clock_interval_to_deadline(4, NSEC_PER_SEC, &deadline);
81a3bb9fccSApple OSS Distributions 	IOLog("%d sec deadline is %llu\n", 4, deadline);
82a3bb9fccSApple OSS Distributions 
83a3bb9fccSApple OSS Distributions 	ret = thread_call_enter_delayed(tcall2, deadline);
84a3bb9fccSApple OSS Distributions 
85a3bb9fccSApple OSS Distributions 	IOLockLock(tlock2);
86a3bb9fccSApple OSS Distributions 
87a3bb9fccSApple OSS Distributions 	clock_interval_to_deadline(3, NSEC_PER_SEC, &deadline);
88a3bb9fccSApple OSS Distributions 	IOLog("%d sec deadline is %llu\n", 3, deadline);
890f3703acSApple OSS Distributions 	sleepret = IOLockSleepDeadline(tlock2, &my_event, deadline, THREAD_INTERRUPTIBLE);
900f3703acSApple OSS Distributions 	IOLog("IOLockSleepDeadline(&my_event, %llu) returned %d, expected 1\n", deadline, sleepret);
91a3bb9fccSApple OSS Distributions 
92a3bb9fccSApple OSS Distributions 	IOLockUnlock(tlock2);
93a3bb9fccSApple OSS Distributions 
94855239e5SApple OSS Distributions 	return true;
95855239e5SApple OSS Distributions }
96855239e5SApple OSS Distributions 
97*a5e72196SApple OSS Distributions static void
thread_call_test_func(thread_call_param_t param0,thread_call_param_t param1)98*a5e72196SApple OSS Distributions thread_call_test_func(thread_call_param_t param0,
99855239e5SApple OSS Distributions     thread_call_param_t param1)
100855239e5SApple OSS Distributions {
101855239e5SApple OSS Distributions 	testthreadcall *self = (testthreadcall *)param0;
102855239e5SApple OSS Distributions 
103855239e5SApple OSS Distributions 	IOLog("thread_call_test_func %p %p\n", param0, param1);
104855239e5SApple OSS Distributions 
105855239e5SApple OSS Distributions 	IOSimpleLockLock(self->tlock);
106855239e5SApple OSS Distributions 	IOSimpleLockUnlock(self->tlock);
107a3bb9fccSApple OSS Distributions }
108855239e5SApple OSS Distributions 
109*a5e72196SApple OSS Distributions static void
thread_call_test_func2(thread_call_param_t param0,thread_call_param_t param1)110*a5e72196SApple OSS Distributions thread_call_test_func2(thread_call_param_t param0,
111a3bb9fccSApple OSS Distributions     thread_call_param_t param1)
112a3bb9fccSApple OSS Distributions {
113a3bb9fccSApple OSS Distributions 	testthreadcall *self = (testthreadcall *)param0;
114a3bb9fccSApple OSS Distributions 
115a3bb9fccSApple OSS Distributions 	IOLog("thread_call_test_func2 %p %p\n", param0, param1);
116a3bb9fccSApple OSS Distributions 
1170f3703acSApple OSS Distributions 	IOLockWakeup(self->tlock2, &my_event, false);
118855239e5SApple OSS Distributions }
119