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