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