📄 vm_thread_win32.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.//*/#if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE)#ifndef _WIN32_WCE#include <process.h>#endif /* _WIN32_WCE */#include <stdlib.h>#include "vm_thread.h"#include "vm_mutex.h"/* set the thread handler an invalid value */void vm_thread_set_invalid(vm_thread *thread){ /* check error(s) */ if (NULL == thread) return; thread->handle = NULL; thread->i_wait_count = 0; 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){ int iRes = 1; /* check error(s) */ if (NULL == thread) return 0; vm_mutex_lock(&thread->access_mut); if (NULL != thread->handle) { if (WAIT_OBJECT_0 == WaitForSingleObject(thread->handle, 0)) { iRes = 0; if (0 == thread->i_wait_count) { CloseHandle(thread->handle); thread->handle = NULL; } } } if (NULL == thread->handle) iRes = 0; vm_mutex_unlock(&(thread->access_mut)); return iRes;} /* int vm_thread_is_valid(vm_thread *thread) *//* create a thread. * return NULL if failed to create the thread */int vm_thread_create(vm_thread *thread, vm_thread_callback func, void *arg){ int i_res = 1; /* check error(s) */ if ((NULL == thread) || (NULL == func)) return 0; if ((!vm_mutex_is_valid(&thread->access_mut)) && (VM_OK != vm_mutex_init(&thread->access_mut))) i_res = 0; if (i_res) { if (NULL != thread->handle) vm_thread_wait(thread); vm_mutex_lock(&thread->access_mut); thread->handle = (HANDLE)#ifndef _WIN32_WCE _beginthreadex(0, 0, (unsigned (__stdcall *)(void*))func, arg, 0, 0);#else /* _WIN32_WCE */ CreateThread(0, 0, (LPTHREAD_START_ROUTINE) func, arg, 0, 0);#endif /* _WIN32_WCE */ i_res = (int) ((thread->handle) ? (1) : (0)); vm_mutex_unlock(&thread->access_mut); } return i_res;} /* int vm_thread_create(vm_thread *thread, vm_thread_callback func, void *arg) *//* set thread priority. return 1 if success */int vm_thread_set_priority(vm_thread *thread, vm_thread_priority priority){ int i_res = 0; /* check error(s) */ if (NULL == thread) return 0; if (NULL != thread->handle) { vm_mutex_lock(&thread->access_mut); switch (priority) { case VM_THREAD_PRIORITY_HIGHEST: i_res = SetThreadPriority(thread->handle, THREAD_PRIORITY_HIGHEST); break; case VM_THREAD_PRIORITY_HIGH: i_res = SetThreadPriority(thread->handle, THREAD_PRIORITY_ABOVE_NORMAL); break; case VM_THREAD_PRIORITY_NORMAL: i_res = SetThreadPriority(thread->handle, THREAD_PRIORITY_NORMAL); break; case VM_THREAD_PRIORITY_LOW: i_res = SetThreadPriority(thread->handle, THREAD_PRIORITY_BELOW_NORMAL); break; case VM_THREAD_PRIORITY_LOWEST: i_res = SetThreadPriority(thread->handle, THREAD_PRIORITY_LOWEST); break; default: i_res = 0; break; } 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; vm_mutex_lock(&thread->access_mut); if (thread->handle) { thread->i_wait_count++; vm_mutex_unlock(&thread->access_mut); WaitForSingleObject(thread->handle, INFINITE); vm_mutex_lock(&thread->access_mut); thread->i_wait_count--; if (0 == thread->i_wait_count) { CloseHandle(thread->handle); thread->handle = NULL; } } 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_mutex_destroy(&thread->access_mut);} /* void vm_thread_close(vm_thread *thread) */#endif /* defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -