xref: /libuv/test/test-thread-priority.c (revision e135dfe1)
1 /* Copyright libuv contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #include "uv.h"
23 #include "task.h"
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h> /* memset */
28 
29 #ifdef __POSIX__
30 #include <pthread.h>
31 #include <errno.h>
32 #endif
33 
34 #ifdef _WIN32
35 #include <windows.h>
36 #else
37 #include <unistd.h>
38 #endif
39 
40 uv_sem_t sem;
41 
simple_task(void * args)42 static void simple_task(void *args) {
43   uv_sem_wait(&sem);
44   printf("in simple_task\n");
45 }
46 
TEST_IMPL(thread_priority)47 TEST_IMPL(thread_priority) {
48   int priority;
49 #ifndef _WIN32
50   int min;
51   int max;
52   int policy;
53   struct sched_param param;
54 #endif
55   uv_thread_t task_id;
56 
57   /* Verify that passing a NULL pointer returns UV_EINVAL. */
58   ASSERT_EQ(UV_EINVAL, uv_thread_getpriority(0, NULL));
59   ASSERT_OK(uv_sem_init(&sem, 1));
60   uv_sem_wait(&sem);
61   ASSERT_OK(uv_thread_create(&task_id, simple_task, NULL));
62   ASSERT_OK(uv_thread_getpriority(task_id, &priority));
63 
64 #ifdef _WIN32
65   ASSERT_EQ(priority, THREAD_PRIORITY_NORMAL);
66 #else
67   ASSERT_OK(pthread_getschedparam(task_id, &policy, &param));
68 #ifdef __PASE__
69   min = 1;
70   max = 127;
71 #else
72   min = sched_get_priority_min(policy);
73   max = sched_get_priority_max(policy);
74 #endif
75   ASSERT(priority >= min && priority <= max);
76 #endif
77 
78   ASSERT_OK(uv_thread_setpriority(task_id, UV_THREAD_PRIORITY_LOWEST));
79   ASSERT_OK(uv_thread_getpriority(task_id, &priority));
80 
81 #ifdef _WIN32
82   ASSERT_EQ(priority, THREAD_PRIORITY_LOWEST);
83 #else
84   ASSERT_EQ(priority, min);
85 #endif
86 
87 /**
88  * test set nice value for the calling thread with default schedule policy
89 */
90 #ifdef __linux__
91   ASSERT_OK(uv_thread_getpriority(pthread_self(), &priority));
92   ASSERT_EQ(priority, 0);
93   ASSERT_OK(uv_thread_setpriority(pthread_self(), UV_THREAD_PRIORITY_LOWEST));
94   ASSERT_OK(uv_thread_getpriority(pthread_self(), &priority));
95   ASSERT_EQ(priority, (0 - UV_THREAD_PRIORITY_LOWEST * 2));
96 #endif
97 
98   uv_sem_post(&sem);
99 
100   ASSERT_OK(uv_thread_join(&task_id));
101 
102   uv_sem_destroy(&sem);
103 
104   return 0;
105 }