1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions * testthreadcall.cpp
3*1031c584SApple OSS Distributions * testkext
4*1031c584SApple OSS Distributions *
5*1031c584SApple OSS Distributions */
6*1031c584SApple OSS Distributions
7*1031c584SApple OSS Distributions #include "testthreadcall.h"
8*1031c584SApple OSS Distributions
9*1031c584SApple OSS Distributions #include <kern/thread_call.h>
10*1031c584SApple OSS Distributions #include <pexpert/pexpert.h>
11*1031c584SApple OSS Distributions
12*1031c584SApple OSS Distributions #define super IOService
13*1031c584SApple OSS Distributions OSDefineMetaClassAndStructors(testthreadcall, super);
14*1031c584SApple OSS Distributions
15*1031c584SApple OSS Distributions extern "C" {
16*1031c584SApple OSS Distributions static void thread_call_test_func(thread_call_param_t param0,
17*1031c584SApple OSS Distributions thread_call_param_t param1);
18*1031c584SApple OSS Distributions
19*1031c584SApple OSS Distributions static void thread_call_test_func2(thread_call_param_t param0,
20*1031c584SApple OSS Distributions thread_call_param_t param1);
21*1031c584SApple OSS Distributions }
22*1031c584SApple OSS Distributions
23*1031c584SApple OSS Distributions static int my_event;
24*1031c584SApple OSS Distributions
25*1031c584SApple OSS Distributions bool
start(IOService * provider)26*1031c584SApple OSS Distributions testthreadcall::start( IOService * provider )
27*1031c584SApple OSS Distributions {
28*1031c584SApple OSS Distributions boolean_t ret;
29*1031c584SApple OSS Distributions uint64_t deadline;
30*1031c584SApple OSS Distributions int sleepret;
31*1031c584SApple OSS Distributions uint32_t kernel_configuration;
32*1031c584SApple OSS Distributions
33*1031c584SApple OSS Distributions IOLog("%s\n", __PRETTY_FUNCTION__);
34*1031c584SApple OSS Distributions
35*1031c584SApple OSS Distributions if (!super::start(provider)) {
36*1031c584SApple OSS Distributions return false;
37*1031c584SApple OSS Distributions }
38*1031c584SApple OSS Distributions
39*1031c584SApple OSS Distributions kernel_configuration = PE_i_can_has_kernel_configuration();
40*1031c584SApple OSS Distributions IOLog("%s: Assertions %s\n", __PRETTY_FUNCTION__,
41*1031c584SApple OSS Distributions (kernel_configuration & kPEICanHasAssertions) ? "enabled" : "disabled");
42*1031c584SApple OSS Distributions IOLog("%s: Statistics %s\n", __PRETTY_FUNCTION__,
43*1031c584SApple OSS Distributions (kernel_configuration & kPEICanHasStatistics) ? "enabled" : "disabled");
44*1031c584SApple OSS Distributions IOLog("%s: Diagnostic API %s\n", __PRETTY_FUNCTION__,
45*1031c584SApple OSS Distributions (kernel_configuration & kPEICanHasDiagnosticAPI) ? "enabled" : "disabled");
46*1031c584SApple OSS Distributions
47*1031c584SApple OSS Distributions IOLog("Attempting thread_call_allocate\n");
48*1031c584SApple OSS Distributions tcall = thread_call_allocate(thread_call_test_func, this);
49*1031c584SApple OSS Distributions IOLog("thread_call_t %p\n", tcall);
50*1031c584SApple OSS Distributions
51*1031c584SApple OSS Distributions tlock = IOSimpleLockAlloc();
52*1031c584SApple OSS Distributions IOLog("tlock %p\n", tlock);
53*1031c584SApple OSS Distributions
54*1031c584SApple OSS Distributions clock_interval_to_deadline(5, NSEC_PER_SEC, &deadline);
55*1031c584SApple OSS Distributions IOLog("%d sec deadline is %llu\n", 5, deadline);
56*1031c584SApple OSS Distributions
57*1031c584SApple OSS Distributions ret = thread_call_enter_delayed(tcall, deadline);
58*1031c584SApple OSS Distributions
59*1031c584SApple OSS Distributions IOLog("Attempting thread_call_allocate\n");
60*1031c584SApple OSS Distributions tcall2 = thread_call_allocate(thread_call_test_func2, this);
61*1031c584SApple OSS Distributions IOLog("thread_call_t %p\n", tcall);
62*1031c584SApple OSS Distributions
63*1031c584SApple OSS Distributions tlock2 = IOLockAlloc();
64*1031c584SApple OSS Distributions IOLog("tlock2 %p\n", tlock2);
65*1031c584SApple OSS Distributions
66*1031c584SApple OSS Distributions clock_interval_to_deadline(2, NSEC_PER_SEC, &deadline);
67*1031c584SApple OSS Distributions IOLog("%d sec deadline is %llu\n", 2, deadline);
68*1031c584SApple OSS Distributions
69*1031c584SApple OSS Distributions ret = thread_call_enter_delayed(tcall2, deadline);
70*1031c584SApple OSS Distributions
71*1031c584SApple OSS Distributions IOLockLock(tlock2);
72*1031c584SApple OSS Distributions
73*1031c584SApple OSS Distributions clock_interval_to_deadline(3, NSEC_PER_SEC, &deadline);
74*1031c584SApple OSS Distributions IOLog("%d sec deadline is %llu\n", 3, deadline);
75*1031c584SApple OSS Distributions sleepret = IOLockSleepDeadline(tlock2, &my_event, deadline, THREAD_INTERRUPTIBLE);
76*1031c584SApple OSS Distributions IOLog("IOLockSleepDeadline(&my_event, %llu) returned %d, expected 0\n", deadline, sleepret);
77*1031c584SApple OSS Distributions
78*1031c584SApple OSS Distributions IOLockUnlock(tlock2);
79*1031c584SApple OSS Distributions
80*1031c584SApple OSS Distributions clock_interval_to_deadline(4, NSEC_PER_SEC, &deadline);
81*1031c584SApple OSS Distributions IOLog("%d sec deadline is %llu\n", 4, deadline);
82*1031c584SApple OSS Distributions
83*1031c584SApple OSS Distributions ret = thread_call_enter_delayed(tcall2, deadline);
84*1031c584SApple OSS Distributions
85*1031c584SApple OSS Distributions IOLockLock(tlock2);
86*1031c584SApple OSS Distributions
87*1031c584SApple OSS Distributions clock_interval_to_deadline(3, NSEC_PER_SEC, &deadline);
88*1031c584SApple OSS Distributions IOLog("%d sec deadline is %llu\n", 3, deadline);
89*1031c584SApple OSS Distributions sleepret = IOLockSleepDeadline(tlock2, &my_event, deadline, THREAD_INTERRUPTIBLE);
90*1031c584SApple OSS Distributions IOLog("IOLockSleepDeadline(&my_event, %llu) returned %d, expected 1\n", deadline, sleepret);
91*1031c584SApple OSS Distributions
92*1031c584SApple OSS Distributions IOLockUnlock(tlock2);
93*1031c584SApple OSS Distributions
94*1031c584SApple OSS Distributions return true;
95*1031c584SApple OSS Distributions }
96*1031c584SApple OSS Distributions
97*1031c584SApple OSS Distributions static void
thread_call_test_func(thread_call_param_t param0,thread_call_param_t param1)98*1031c584SApple OSS Distributions thread_call_test_func(thread_call_param_t param0,
99*1031c584SApple OSS Distributions thread_call_param_t param1)
100*1031c584SApple OSS Distributions {
101*1031c584SApple OSS Distributions testthreadcall *self = (testthreadcall *)param0;
102*1031c584SApple OSS Distributions
103*1031c584SApple OSS Distributions IOLog("thread_call_test_func %p %p\n", param0, param1);
104*1031c584SApple OSS Distributions
105*1031c584SApple OSS Distributions IOSimpleLockLock(self->tlock);
106*1031c584SApple OSS Distributions IOSimpleLockUnlock(self->tlock);
107*1031c584SApple OSS Distributions }
108*1031c584SApple OSS Distributions
109*1031c584SApple OSS Distributions static void
thread_call_test_func2(thread_call_param_t param0,thread_call_param_t param1)110*1031c584SApple OSS Distributions thread_call_test_func2(thread_call_param_t param0,
111*1031c584SApple OSS Distributions thread_call_param_t param1)
112*1031c584SApple OSS Distributions {
113*1031c584SApple OSS Distributions testthreadcall *self = (testthreadcall *)param0;
114*1031c584SApple OSS Distributions
115*1031c584SApple OSS Distributions IOLog("thread_call_test_func2 %p %p\n", param0, param1);
116*1031c584SApple OSS Distributions
117*1031c584SApple OSS Distributions IOLockWakeup(self->tlock2, &my_event, false);
118*1031c584SApple OSS Distributions }
119