Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | /* * Copyright (c) 2017 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #if !(DEVELOPMENT || DEBUG) #error "Testing is not enabled on RELEASE configurations" #endif #include <tests/xnupost.h> #include <kern/thread_call.h> #include <kern/locks.h> #include <kern/sched_prim.h> kern_return_t test_thread_call(void); lck_grp_t test_lock_grp; lck_mtx_t test_lock; typedef enum { TEST_ARG1 = 0x1234, TEST_ARG2 = 0x3456, } test_param; int wait_for_callback; int wait_for_main; int once_callback_counter = 0; static void test_once_callback(thread_call_param_t param0, thread_call_param_t param1) { T_ASSERT_EQ_INT((test_param)param0, TEST_ARG1, "param0 is correct"); T_ASSERT_EQ_INT((test_param)param1, TEST_ARG2, "param1 is correct"); once_callback_counter++; T_ASSERT_EQ_INT(once_callback_counter, 1, "only one callback"); lck_mtx_lock(&test_lock); thread_wakeup(&wait_for_callback); uint64_t deadline; clock_interval_to_deadline(10, NSEC_PER_SEC, &deadline); kern_return_t kr; /* wait for the main thread to finish, time out after 10s */ kr = lck_mtx_sleep_deadline(&test_lock, LCK_SLEEP_DEFAULT, &wait_for_main, THREAD_UNINT, deadline); T_ASSERT_EQ_INT(kr, THREAD_AWAKENED, " callback woken by main function"); lck_mtx_unlock(&test_lock); /* sleep for 1s to let the main thread begin the cancel and wait */ delay_for_interval(1, NSEC_PER_SEC); } static void test_once_thread_call(void) { lck_grp_init(&test_lock_grp, "test_thread_call", LCK_GRP_ATTR_NULL); lck_mtx_init(&test_lock, &test_lock_grp, LCK_ATTR_NULL); thread_call_t call; call = thread_call_allocate_with_options(&test_once_callback, (thread_call_param_t)TEST_ARG1, THREAD_CALL_PRIORITY_HIGH, THREAD_CALL_OPTIONS_ONCE); thread_call_param_t arg2_param = (thread_call_param_t)TEST_ARG2; lck_mtx_lock(&test_lock); thread_call_enter1(call, arg2_param); uint64_t deadline; clock_interval_to_deadline(10, NSEC_PER_SEC, &deadline); kern_return_t kr; /* wait for the call to execute, time out after 10s */ kr = lck_mtx_sleep_deadline(&test_lock, LCK_SLEEP_DEFAULT, &wait_for_callback, THREAD_UNINT, deadline); T_ASSERT_EQ_INT(kr, THREAD_AWAKENED, "main function woken by callback"); lck_mtx_unlock(&test_lock); /* at this point the callback is stuck waiting */ T_ASSERT_EQ_INT(once_callback_counter, 1, "callback fired"); boolean_t canceled, pending, freed; canceled = thread_call_cancel(call); T_ASSERT_EQ_INT(canceled, FALSE, "thread_call_cancel should not succeed"); pending = thread_call_enter1(call, arg2_param); T_ASSERT_EQ_INT(pending, FALSE, "call should not be pending"); /* sleep for 10ms, the call should not execute */ delay_for_interval(10, NSEC_PER_MSEC); canceled = thread_call_cancel(call); T_ASSERT_EQ_INT(canceled, TRUE, "thread_call_cancel should succeed"); pending = thread_call_enter1(call, arg2_param); T_ASSERT_EQ_INT(pending, FALSE, "call should not be pending"); freed = thread_call_free(call); T_ASSERT_EQ_INT(freed, FALSE, "thread_call_free should not succeed"); pending = thread_call_enter1(call, arg2_param); T_ASSERT_EQ_INT(pending, TRUE, "call should be pending"); thread_wakeup(&wait_for_main); canceled = thread_call_cancel_wait(call); T_ASSERT_EQ_INT(canceled, TRUE, "thread_call_cancel_wait should succeed"); canceled = thread_call_cancel(call); T_ASSERT_EQ_INT(canceled, FALSE, "thread_call_cancel should not succeed"); freed = thread_call_free(call); T_ASSERT_EQ_INT(freed, TRUE, "thread_call_free should succeed"); } int signal_callback_counter = 0; static void test_signal_callback(__unused thread_call_param_t param0, __unused thread_call_param_t param1) { /* * ktest sometimes panics if you assert from interrupt context, * and the serial logging will blow past the delay to wait for the interrupt * so don't print in this context. */ signal_callback_counter++; } static void test_signal_thread_call(void) { thread_call_t call; call = thread_call_allocate_with_options(&test_signal_callback, (thread_call_param_t)TEST_ARG1, THREAD_CALL_PRIORITY_HIGH, THREAD_CALL_OPTIONS_ONCE|THREAD_CALL_OPTIONS_SIGNAL); thread_call_param_t arg2_param = (thread_call_param_t)TEST_ARG2; uint64_t deadline; boolean_t canceled, pending, freed; clock_interval_to_deadline(10, NSEC_PER_SEC, &deadline); pending = thread_call_enter1_delayed(call, arg2_param, deadline); T_ASSERT_EQ_INT(pending, FALSE, "call should not be pending"); canceled = thread_call_cancel(call); T_ASSERT_EQ_INT(canceled, TRUE, "thread_call_cancel should succeed"); clock_interval_to_deadline(10, NSEC_PER_MSEC, &deadline); pending = thread_call_enter1_delayed(call, arg2_param, deadline); T_ASSERT_EQ_INT(pending, FALSE, "call should not be pending"); /* sleep for 50ms to let the interrupt fire */ delay_for_interval(50, NSEC_PER_MSEC); T_ASSERT_EQ_INT(signal_callback_counter, 1, "callback fired"); canceled = thread_call_cancel(call); T_ASSERT_EQ_INT(canceled, FALSE, "thread_call_cancel should not succeed"); freed = thread_call_free(call); T_ASSERT_EQ_INT(freed, TRUE, "thread_call_free should succeed"); } kern_return_t test_thread_call(void) { test_once_thread_call(); test_signal_thread_call(); return KERN_SUCCESS; } |