zoukankan      html  css  js  c++  java
  • dm8148 开发之---互斥量、条件量、枷锁、互斥枷锁

    int OSA_semCreate(OSA_SemHndl *hndl, Uint32 maxCount, Uint32 initVal)
    {
    pthread_mutexattr_t mutex_attr;
    pthread_condattr_t cond_attr;
    int status=OSA_SOK;

    status |= pthread_mutexattr_init(&mutex_attr);
    status |= pthread_condattr_init(&cond_attr);

    status |= pthread_mutex_init(&hndl->lock, &mutex_attr);
    status |= pthread_cond_init(&hndl->cond, &cond_attr);

    hndl->count = initVal;
    hndl->maxCount = maxCount;

    if(hndl->maxCount==0)
    hndl->maxCount=1;

    if(hndl->count>hndl->maxCount)
    hndl->count = hndl->maxCount;

    if(status!=OSA_SOK)
    OSA_ERROR("OSA_semCreate() = %d ", status);

    pthread_condattr_destroy(&cond_attr);
    pthread_mutexattr_destroy(&mutex_attr);

    return status;
    }


    int OSA_semWait(OSA_SemHndl *hndl, Uint32 timeout)
    {
    int status = OSA_EFAIL;

    pthread_mutex_lock(&hndl->lock);

    while(1) {
    if(hndl->count > 0) {
    hndl->count--;
    status = OSA_SOK;
    break;
    } else {
    if(timeout==OSA_TIMEOUT_NONE)
    break;

    pthread_cond_wait(&hndl->cond, &hndl->lock);
    }
    }

    pthread_mutex_unlock(&hndl->lock);

    return status;
    }

    int OSA_semSignal(OSA_SemHndl *hndl)
    {
    int status = OSA_SOK;

    pthread_mutex_lock(&hndl->lock);

    if(hndl->count<hndl->maxCount) {
    hndl->count++;
    status |= pthread_cond_signal(&hndl->cond);
    }

    pthread_mutex_unlock(&hndl->lock);

    return status;
    }

    int OSA_semDelete(OSA_SemHndl *hndl)
    {
    pthread_cond_destroy(&hndl->cond);
    pthread_mutex_destroy(&hndl->lock);

    return OSA_SOK;
    }

    void OSA_waitMsecs(Uint32 msecs)
    {
    #if 1
    struct timespec delayTime, elaspedTime;

    delayTime.tv_sec = msecs/1000;
    delayTime.tv_nsec = (msecs%1000)*1000000;

    nanosleep(&delayTime, &elaspedTime);
    #else
    usleep(msecs*1000);
    #endif
    }

    http://www.cnblogs.com/mywolrd/archive/2009/02/05/1930707.html#ConVarOverview

    http://blog.csdn.net/npuweiwei/article/details/8661829

  • 相关阅读:
    centos7 安装mysql
    Nginx安装及配置详解
    nginx安装
    JSON Web Token
    优先队列
    小程序遮罩层禁止页面滚动(遮罩层内部可以滚动)
    H5中接入微信支付
    如何使用less预编译
    在methods中使用filter
    根据当前时间获取上一个月的时间
  • 原文地址:https://www.cnblogs.com/pengkunfan/p/3884950.html
Copyright © 2011-2022 走看看