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