📄 vm_thread_linux32.c
字号:
/*//// INTEL CORPORATION PROPRIETARY INFORMATION// This software is supplied under the terms of a license agreement or// nondisclosure agreement with Intel Corporation and may not be copied// or disclosed except in accordance with the terms of that agreement.// Copyright(c) 2003-2005 Intel Corporation. All Rights Reserved.//*/#ifdef LINUX32#include <unistd.h>#include <sys/time.h>#include <sched.h>#include "vm_thread.h"#include "vm_event.h"#include "vm_mutex.h"staticvoid *vm_thread_proc(void *pv_params){ vm_thread *p_thread = (vm_thread *) pv_params; /* check error(s) */ if (NULL == pv_params) return ((void *) -1); p_thread->p_thread_func(p_thread->p_arg); vm_event_signal(&p_thread->exit_event); return ((void *) 1);} /* void *vm_thread_proc(void *pv_params) *//* set the thread handler an invalid value */void vm_thread_set_invalid(vm_thread *thread){ /* check error(s) */ if (NULL == thread) return; thread->is_valid = 0; thread->i_wait_count = 0; vm_event_set_invalid(&thread->exit_event); vm_mutex_set_invalid(&thread->access_mut);} /* void vm_thread_set_invalid(vm_thread *thread) *//* verify if the thread handler is valid */int vm_thread_is_valid(vm_thread *thread){ /* check error(s) */ if (NULL == thread) return 0; if (thread->is_valid) { vm_mutex_lock(&thread->access_mut); if (VM_OK == vm_event_timed_wait(&thread->exit_event, 0)) { vm_mutex_unlock(&thread->access_mut); vm_thread_wait(thread); } else vm_mutex_unlock(&thread->access_mut); } return thread->is_valid;} /* int vm_thread_is_valid(vm_thread *thread) *//* create a thread. return 1 if success */int vm_thread_create(vm_thread *thread, unsigned int (*vm_thread_func)(void *), void *arg){ int i_res = 1; pthread_attr_t attr; /* check error(s) */ if ((NULL == thread) || (NULL == vm_thread_func)) return 0; if (0 != i_res) { if (VM_OK != vm_event_init(&thread->exit_event, 1, 0)) i_res = 0; } if ((0 != i_res) && (VM_OK != vm_mutex_init(&thread->access_mut))) i_res = 0; if (0 != i_res) { vm_mutex_lock(&thread->access_mut); thread->p_thread_func = vm_thread_func; thread->p_arg = arg; pthread_attr_init(&attr); pthread_attr_setschedpolicy(&attr, geteuid() ? SCHED_OTHER : SCHED_RR); thread->is_valid =! pthread_create(&thread->handle, &attr, vm_thread_proc, (void*)thread); i_res = (thread->is_valid) ? 1 : 0; vm_mutex_unlock(&thread->access_mut); pthread_attr_destroy(&attr); } vm_thread_set_priority(thread, VM_THREAD_PRIORITY_LOWEST); return i_res;} /* int vm_thread_create(vm_thread *thread, *//* set thread priority, return 1 if successful */int vm_thread_set_priority(vm_thread *thread, vm_thread_priority priority){ int i_res = 1; int policy, pmin, pmax, pmean; struct sched_param param; /* check error(s) */ if (NULL == thread) return 0; if (thread->is_valid) { vm_mutex_lock(&thread->access_mut); pthread_getschedparam(thread->handle,&policy,¶m); pmin = sched_get_priority_min(policy); pmax = sched_get_priority_max(policy); pmean = (pmin + pmax) / 2; switch (priority) { case VM_THREAD_PRIORITY_HIGHEST: param.sched_priority = pmax; break; case VM_THREAD_PRIORITY_LOWEST: param.sched_priority = pmin; break; case VM_THREAD_PRIORITY_NORMAL: param.sched_priority = pmean; break; case VM_THREAD_PRIORITY_HIGH: param.sched_priority = (pmax + pmean) / 2; break; case VM_THREAD_PRIORITY_LOW: param.sched_priority = (pmin + pmean) / 2; break; default: i_res = 0; break; } if (i_res) i_res = !pthread_setschedparam(thread->handle, policy, ¶m); vm_mutex_unlock(&thread->access_mut); } return i_res;} /* int vm_thread_set_priority(vm_thread *thread, vm_thread_priority priority) *//* wait until a thread exists */void vm_thread_wait(vm_thread *thread){ /* check error(s) */ if (NULL == thread) return; if (thread->is_valid) { vm_mutex_lock(&thread->access_mut); thread->i_wait_count++; vm_mutex_unlock(&thread->access_mut); vm_event_wait(&thread->exit_event); vm_mutex_lock(&thread->access_mut); thread->i_wait_count--; if (0 == thread->i_wait_count) { pthread_join(thread->handle, NULL); thread->is_valid = 0; } vm_mutex_unlock(&thread->access_mut); }} /* void vm_thread_wait(vm_thread *thread) *//* close thread after all */void vm_thread_close(vm_thread *thread){ /* check error(s) */ if (NULL == thread) return; vm_thread_wait(thread); vm_event_destroy(&thread->exit_event); vm_mutex_destroy(&thread->access_mut);} /* void vm_thread_close(vm_thread *thread) */#endif /* LINUX32 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -