thread_windows.c

来自「一个很好用的解析」· C语言 代码 · 共 168 行

C
168
字号
#include <process.h>#include <windows.h>#include "xmlrpc_config.h"#include "mallocvar.h"#include "xmlrpc-c/string_int.h"#include "xmlrpc-c/abyss.h"#include "thread.h"struct abyss_thread {    HANDLE          handle;    void *          userHandle;    TThreadDoneFn * threadDone;};#define  THREAD_STACK_SIZE (16*1024L)typedef uint32_t (WINAPI WinThreadProc)(void *);voidThreadCreate(TThread **      const threadPP,             void *          const userHandle,             TThreadProc   * const func,             TThreadDoneFn * const threadDone,             abyss_bool      const useSigchld,             const char **   const errorP) {    DWORD z;    TThread * threadP;    MALLOCVAR(threadP);    if (threadP == NULL)        xmlrpc_asprintf(errorP,                        "Can't allocate memory for thread descriptor.");    else {        threadP->userHandle = userHandle;        threadP->threadDone = threadDone;        threadP->handle = (HANDLE)_beginthreadex(NULL, THREAD_STACK_SIZE, func,                                         userHandle,                                         CREATE_SUSPENDED, &z);        if (threadP->handle == NULL)            xmlrpc_asprintf(errorP, "_beginthreadex() failed.");        else {            *errorP = NULL;            *threadPP = threadP;        }        if (*errorP)            free(threadP);    }}abyss_boolThreadRun(TThread * const threadP) {    return (ResumeThread(threadP->handle) != 0xFFFFFFFF);}abyss_boolThreadStop(TThread * const threadP) {    return (SuspendThread(threadP->handle) != 0xFFFFFFFF);}abyss_boolThreadKill(TThread * const threadP) {    return (TerminateThread(threadP->handle, 0) != 0);}voidThreadWaitAndRelease(TThread * const threadP) {    ThreadRelease(threadP);    if (threadP->threadDone)        threadP->threadDone(threadP->userHandle);    free(threadP);}voidThreadExit(int const retValue) {    _endthreadex(retValue);}voidThreadRelease(TThread * const threadP) {    CloseHandle(threadP->handle);}abyss_boolThreadForks(void) {    return FALSE;}voidThreadUpdateStatus(TThread * const threadP ATTR_UNUSED) {    /* Threads keep their own statuses up to date, so there's nothing       to do here.    */}abyss_boolMutexCreate(TMutex * const mutexP) {    *mutexP = CreateMutex(NULL, FALSE, NULL);    return (*mutexP != NULL);}abyss_boolMutexLock(TMutex * const mutexP) {    return (WaitForSingleObject(*mutexP, INFINITE) != WAIT_TIMEOUT);}abyss_boolMutexUnlock(TMutex * const mutexP) {    return ReleaseMutex(*mutexP);}abyss_boolMutexTryLock(TMutex * const mutexP) {    return (WaitForSingleObject(*mutexP, 0) != WAIT_TIMEOUT);}voidMutexFree(TMutex * const mutexP) {    CloseHandle(*mutexP);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?