From: Diego Nieto Cid <[email protected]>
---
tests/test-task.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 106 insertions(+)
diff --git a/tests/test-task.c b/tests/test-task.c
index cbc75e23..4be9c193 100644
--- a/tests/test-task.c
+++ b/tests/test-task.c
@@ -27,6 +27,7 @@
#include <gnumach.user.h>
#include <mach.user.h>
+#include <mach_host.user.h>
void test_task()
@@ -160,6 +161,110 @@ int test_errors()
ASSERT(err == MACH_SEND_INVALID_DEST, "task DEAD");
}
+void test_priority()
+{
+/* XXX cannot include <kern/sched.h> */
+#define BASEPRI_USER 25
+ kern_return_t err;
+ struct task_basic_info tk_binfo;
+ struct thread_sched_info th_sinfo;
+ int count;
+ thread_t new_th;
+
+ /* Get current task priority */
+ count = TASK_BASIC_INFO_COUNT;
+ err = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&tk_binfo,
&count);
+ ASSERT_RET(err, "TASK_BASIC_INFO failed!");
+ printf("initial base priority is %d\n", tk_binfo.base_priority);
+ ASSERT(tk_binfo.base_priority == BASEPRI_USER, "(task) Unexpected initial
base priority");
+
+ err = task_priority(mach_task_self(), 5, 0);
+ ASSERT_RET(err, "task_priority (5, 0) failed");
+
+ err = task_priority(mach_task_self(), 5, 1);
+ ASSERT(err != KERN_SUCCESS, "task_priority (5, 1) succeeded!");
+
+ /* Get current task priority */
+ count = TASK_BASIC_INFO_COUNT;
+ err = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&tk_binfo,
&count);
+ ASSERT_RET(err, "TASK_BASIC_INFO failed!");
+ ASSERT(tk_binfo.base_priority == 5,
+ "(task) previous call to task_priority didn't set base priority");
+
+ /* Restore base priority */
+ err = task_priority(mach_task_self(), BASEPRI_USER, 0);
+ ASSERT_RET(err, "task_priority (BASEPRI_USER, 0) failed");
+
+ /* Get current thread priority */
+ count = THREAD_SCHED_INFO_COUNT;
+ err = thread_info(mach_thread_self(), THREAD_SCHED_INFO,
(thread_info_t)&th_sinfo, &count);
+ ASSERT_RET(err, "THREAD_SCHED_INFO failed!");
+ ASSERT(th_sinfo.base_priority == BASEPRI_USER, "(thread) Unexpected initial
base priority");
+ ASSERT(th_sinfo.max_priority == BASEPRI_USER, "(thread) Unexpected initial
max priority");
+
+ err = task_priority_override(mach_host_self(), mach_task_self(), 5);
+ ASSERT(err == KERN_INVALID_ARGUMENT,
+ "task_priority_override must require the host privileged port");
+
+ err = task_priority_override(host_priv(), mach_task_self(), 5);
+ ASSERT_RET(err, "task_priority_override (5) failed");
+
+ /* Get updated task priority */
+ count = TASK_BASIC_INFO_COUNT;
+ err = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&tk_binfo,
&count);
+ ASSERT_RET(err, "TASK_BASIC_INFO failed!");
+ ASSERT(tk_binfo.base_priority == 5, "Priority not updated");
+
+ /* Get updated thread priority */
+ count = THREAD_SCHED_INFO_COUNT;
+ err = thread_info(mach_thread_self(), THREAD_SCHED_INFO,
(thread_info_t)&th_sinfo, &count);
+ ASSERT_RET(err, "THREAD_SCHED_INFO failed!");
+ printf("(thread) updated base priority is %d\n", th_sinfo.base_priority);
+ ASSERT(th_sinfo.base_priority == 5, "(thread) Unexpected updated base
priority");
+ ASSERT(th_sinfo.max_priority == 5, "(thread) Unexpected updated max
priority");
+
+ err = thread_create(mach_task_self(), &new_th);
+ ASSERT_RET(err, "thread_create failed");
+
+ /* Get new thread priority */
+ count = THREAD_SCHED_INFO_COUNT;
+ err = thread_info(new_th, THREAD_SCHED_INFO, (thread_info_t)&th_sinfo,
&count);
+ ASSERT_RET(err, "THREAD_SCHED_INFO failed!");
+ printf("(new thread) initial base priority is %d\n", th_sinfo.base_priority);
+ ASSERT(th_sinfo.base_priority == 5, "(new thread) Unexpected initial base
priority");
+ ASSERT(th_sinfo.max_priority == 5, "(new thread) Unexpected initial max
priority");
+
+ /* Restore priorities */
+ err = task_priority_override(host_priv(), mach_task_self(), BASEPRI_USER);
+ ASSERT_RET(err, "task_priority_override (BASEPRI_USER) failed");
+
+ /* Check new thread priority was updated */
+ count = THREAD_SCHED_INFO_COUNT;
+ err = thread_info(new_th, THREAD_SCHED_INFO, (thread_info_t)&th_sinfo,
&count);
+ ASSERT_RET(err, "THREAD_SCHED_INFO failed!");
+ printf("(new thread) updated base priority is %d\n", th_sinfo.base_priority);
+ ASSERT(th_sinfo.base_priority == BASEPRI_USER, "(new thread) Unexpected
updated base priority");
+ /*
+ * Here we still expect the updated max priority. The new priority set above
+ * is bigger than the previous one therefore the max_priority of the thread
+ * is not adjusted.
+ */
+ ASSERT(th_sinfo.max_priority == 5, "(new thread) Unexpected updated max
priority");
+
+ err = thread_priority(new_th, BASEPRI_USER, TRUE);
+ ASSERT_RET(err, "thread_priority failed!");
+
+ /* Check new thread's max priority was updated */
+ count = THREAD_SCHED_INFO_COUNT;
+ err = thread_info(new_th, THREAD_SCHED_INFO, (thread_info_t)&th_sinfo,
&count);
+ ASSERT_RET(err, "THREAD_SCHED_INFO failed!");
+ printf("(new thread) updated max priority is %d\n", th_sinfo.max_priority);
+ ASSERT(th_sinfo.max_priority == BASEPRI_USER, "(new thread) Unexpected
updated base priority");
+
+ err = thread_terminate(new_th);
+ ASSERT_RET(err, "thread_terminate failed");
+#undef BASEPRI_USER
+}
int main(int argc, char *argv[], int envc, char *envp[])
{
@@ -167,5 +272,6 @@ int main(int argc, char *argv[], int envc, char *envp[])
test_task_threads();
test_new_task();
test_errors();
+ test_priority();
return 0;
}
--
2.53.0