⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils.cpp

📁 采用windriver提供的API开发的D12应用程序源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* Copyright (C) Jungo 2002 - 2006 */

#include "stdafx.h"
#include "utils.h"
#if !defined(WIN16)

#include "windrvr.h"
#if defined(WIN32) && !defined(WINCE)
    #include <process.h>
#endif
#if defined(UNIX) && !defined(VXWORKS)
    #include <pthread.h>
    #include <sys/time.h>
    #include <unistd.h>
    #include <errno.h>
#endif

#if !defined (__KERNEL__)
    #include <stdarg.h>
    #include <stdio.h>
    #if !defined(VXWORKS)
        #include <malloc.h>
    #endif
#else
    #if defined(LINUX)
        #define sprintf LINUX_sprintf
    #endif
#endif
#if defined(BCPP)
    #include <conio.h>
#endif

#if defined(LINUX)
typedef struct {
    pthread_cond_t cond;
    pthread_mutex_t mutex;
    BOOL signaled;
} wd_linux_event_t;
#endif

#if defined(__KERNEL__) && defined(WIN32)
    #pragma warning( disable :4013 4100)
#endif

#if !(defined(WIN32) || defined(WIN16)) || defined(WINCE) || defined(_MT) 
// threads functions
typedef struct
{
    HANDLER_FUNC func;
    void *data;
} thread_struct_t;

#if defined(WIN32) && !defined (WINCE)
    static unsigned int DLLCALLCONV thread_handler(void *data)
#elif defined(WINCE)
    static unsigned long DLLCALLCONV thread_handler(void *data)
#else
    static void *thread_handler(void *data)
#endif
{
    thread_struct_t *t = (thread_struct_t *)data;
#if defined(WINCE)
    SetProcPermissions(0xFFFF);
#endif
    t->func(t->data);
    free(t);
    return 0;
}

DWORD DLLCALLCONV ThreadStart(HANDLE *phThread, HANDLER_FUNC pFunc, void *pData)
{
    #if defined(WIN32) || defined(WINCE)
    DWORD dwTmp;
    #endif
    thread_struct_t *t = (thread_struct_t *)malloc(sizeof(thread_struct_t));
    void *ret = NULL;

    t->func = pFunc;
    t->data = pData;
    #if defined(WIN32) && !defined(WINCE)
        ret = (void *)_beginthreadex(NULL, 0x1000, thread_handler,
            (void *)t, 0, (unsigned int *)&dwTmp);
    #elif defined(WINCE)
        ret = (void *)CreateThread(NULL, 0x1000, thread_handler,
            (void *)t, 0, (unsigned long *)&dwTmp);
    #elif defined(VXWORKS)
    {
        int priority, std, task_id = ERROR;
        if (taskPriorityGet(taskIdSelf(), &priority)!=ERROR)
        {
            task_id = taskSpawn(NULL, priority, 0, 0x4000,
                (FUNCPTR)thread_handler, (int)t, 0, 0, 0, 0, 0,
                0, 0, 0, 0);
        }
        if (task_id!=ERROR)
        {
            std = ioTaskStdGet(0, 1);
            ioTaskStdSet(task_id, 0, std);
            ioTaskStdSet(task_id, 1, std);
            ioTaskStdSet(task_id, 2, std);
            ret = (void *)task_id;
        }
    }
    #elif defined(UNIX)
    {
        int err = 0;
        ret = malloc(sizeof(pthread_t));
        if (ret)
            err = pthread_create((pthread_t *)ret, NULL, thread_handler, (PVOID)t);
        if (err)
        {
            free(ret);
            ret = NULL;
        }
    }
    #endif
    *phThread = ret;
    if (!ret)
    {
        free(t);
        return WD_INSUFFICIENT_RESOURCES;
    }
    return WD_STATUS_SUCCESS;
}


#if defined(VXWORKS)
    #define WAIT_FOR_EVER 0
    static void vxTask_wait(int taskId, int waitTime)
    {
        SEM_ID waitSem;

        if (waitTime==WAIT_FOR_EVER)
        {
            /* Loop while task is still alive */
            while (taskIdVerify(taskId)==OK)
                taskDelay(3);
        }
        else
        {
            /* create a dummy semaphore and try to take it for the specified
             * time. */
            waitSem = semBCreate(SEM_Q_PRIORITY, SEM_EMPTY);
            semTake(waitSem, waitTime);
            semDelete(waitSem);
        }
    }
#endif

void DLLCALLCONV ThreadWait(HANDLE hThread)
{
    #if defined(WIN32)
        WaitForSingleObject(hThread, INFINITE);
        CloseHandle(hThread);
    #elif defined(VXWORKS)
        vxTask_wait((int)hThread, WAIT_FOR_EVER);
    #elif defined(UNIX)
        pthread_join(*((pthread_t *)hThread), NULL);
        free(hThread);
    #endif
}

// For backward compatability
void DLLCALLCONV ThreadStop(HANDLE hThread)
{
    ThreadWait(hThread);
}
// end of threads functions

#endif // defined(_MT)

// Synchronization objects

// Auto-reset events
DWORD DLLCALLCONV OsEventCreate(HANDLE *phOsEvent)
{
#if defined(__KERNEL__)
    return WD_NOT_IMPLEMENTED;
#else
#if defined(WIN32)
    *phOsEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    return *phOsEvent ? WD_STATUS_SUCCESS : WD_INSUFFICIENT_RESOURCES;
#elif defined(LINUX)
    wd_linux_event_t *linux_event = (wd_linux_event_t *)malloc(
        sizeof(wd_linux_event_t));
    if (!linux_event)
        return WD_INSUFFICIENT_RESOURCES;
    memset(linux_event, 0, sizeof(wd_linux_event_t));
    pthread_cond_init(&linux_event->cond, NULL); 
    pthread_mutex_init(&linux_event->mutex, NULL);
    *phOsEvent = linux_event;
    return WD_STATUS_SUCCESS;
#else
    return WD_NOT_IMPLEMENTED;
#endif
#endif
}

DWORD DLLCALLCONV OsEventReset(HANDLE hOsEvent)
{
    DWORD dwStatus = OsEventWait(hOsEvent, 0);
    return (dwStatus==WD_TIME_OUT_EXPIRED) ? WD_STATUS_SUCCESS : dwStatus;
}

void DLLCALLCONV OsEventClose(HANDLE hOsEvent)
{
#if defined(__KERNEL__)
#else
#if defined(WIN32)
    if (hOsEvent)
        CloseHandle(hOsEvent);
#elif defined(LINUX)
    wd_linux_event_t *linux_event = (wd_linux_event_t *)hOsEvent;
    pthread_cond_destroy(&linux_event->cond); 
    pthread_mutex_destroy(&linux_event->mutex);
    free(linux_event);
#endif
#endif
}

DWORD DLLCALLCONV OsEventWait(HANDLE hOsEvent, DWORD dwSecTimeout)
{
#if defined(__KERNEL__)
    return WD_STATUS_SUCCESS;
#else
    DWORD rc = WD_STATUS_SUCCESS;
#if defined(WIN32)
    rc = WaitForSingleObject(hOsEvent, 
        (dwSecTimeout == INFINITE) ? INFINITE : dwSecTimeout * 1000);
    switch (rc) 
    {
        case WAIT_OBJECT_0:
            rc = WD_STATUS_SUCCESS;
            break;
        case WAIT_TIMEOUT:
            rc = WD_TIME_OUT_EXPIRED;
            break;
        default:
            rc = WD_SYSTEM_INTERNAL_ERROR;
            break;
    }
#elif defined(LINUX)
    struct timeval now;
    struct timespec timeout;
    wd_linux_event_t *linux_event = (wd_linux_event_t *)hOsEvent;

    pthread_mutex_lock(&linux_event->mutex);
    if (!linux_event->signaled)
    {
        if (dwSecTimeout == INFINITE)
            rc = pthread_cond_wait(&linux_event->cond, &linux_event->mutex);
        else
        {
            gettimeofday(&now, NULL);
            timeout.tv_sec = now.tv_sec + dwSecTimeout;
            timeout.tv_nsec = now.tv_usec * 1000;

            rc = pthread_cond_timedwait(&linux_event->cond, &linux_event->mutex, &timeout);
        }
    }
    linux_event->signaled = FALSE;
    pthread_mutex_unlock(&linux_event->mutex);
    rc = (rc==ETIMEDOUT ?  WD_TIME_OUT_EXPIRED : WD_STATUS_SUCCESS);
#endif
    return rc;
#endif
}

DWORD DLLCALLCONV OsEventSignal(HANDLE hOsEvent)
{
#if defined(__KERNEL__)
    return WD_STATUS_SUCCESS;
#else
#if defined(WIN32)
    if (!SetEvent(hOsEvent))
        return WD_SYSTEM_INTERNAL_ERROR;
#elif defined(LINUX)
    wd_linux_event_t *linux_event = (wd_linux_event_t *)hOsEvent;
    pthread_mutex_lock(&linux_event->mutex);
    linux_event->signaled = TRUE;
    pthread_cond_signal(&linux_event->cond);
    pthread_mutex_unlock(&linux_event->mutex);
#endif
    return WD_STATUS_SUCCESS;
#endif
}

DWORD DLLCALLCONV OsMutexCreate(HANDLE *phOsMutex)
{
#if defined(__KERNEL__)
    return WD_NOT_IMPLEMENTED;
#else
#if defined(WIN32)
    *phOsMutex = CreateMutex(NULL, FALSE, NULL);
    return *phOsMutex ? WD_STATUS_SUCCESS : WD_INSUFFICIENT_RESOURCES;
#elif defined(LINUX)
    pthread_mutex_t *linux_mutex = (pthread_mutex_t *)malloc(
        sizeof(pthread_mutex_t));
    if (!linux_mutex)
        return WD_INSUFFICIENT_RESOURCES;
    memset(linux_mutex, 0, sizeof(pthread_mutex_t));
    pthread_mutex_init(linux_mutex, NULL);
    *phOsMutex = linux_mutex;
    return WD_STATUS_SUCCESS;
#else

⌨️ 快捷键说明

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