获取无效参数错误(22)

时间:2012-06-13 06:53:30

标签: c++ linux arm

我在x86-64机器上编写Linux代码。它在x86-64上工作得很好,但我将相同的代码移植到基于ARM的设备上,现在我收到错误:定时器代码的参数无效。以下是我的计时器代码:

/**
* FUNCTION NAME : startRetryCount
* @brief start the retry count
* @param isAuth: if true start retry for Auth message else for Normal message
* @return true if success and false if not
*/
bool startRetryCount(bool isAuth)
{
    m_RetryAuth = isAuth;
    stopRetryCount();

    struct sigevent sigev;
    struct itimerspec itval;
    struct itimerspec oitval;
    struct sigaction sigact;

    if(sigemptyset(&sigact.sa_mask) == -1)
    {
        printLog(LOG_ERROR,"[%s:%d#%s] <<<< sigemptyset >>>> : %s\n",
                            __FILE__,__LINE__,__func__,strerror(errno));
    }

    sigact.sa_flags = SA_SIGINFO;
    sigact.sa_sigaction = signalHandler;

    m_RetryCount = ConfigureManager_SingleTon::getInstance()->getRetryCount();
    printLog(LOG_INFO,"**Retry Count Started**\n"); 
    printLog(LOG_INFO,"Number of Retry Count:%d\n",m_RetryCount);
    printLog(LOG_INFO,"Retry time is:%d\n",
            ConfigureManager_SingleTon::getInstance()->getRetryTime());

    // set up sigaction to catch signal
    if (sigaction(SIGTIMER, &sigact, NULL) == -1)
    {
        printLog(LOG_ERROR,"[%s:%d#%s] <<<< time_settime >>>> : %s\n",
                            __FILE__,__LINE__,__func__,strerror(errno));
        return false;
    }

    //Create the POSIX timer to generate signo
    sigev.sigev_notify = SIGEV_SIGNAL;
    sigev.sigev_signo = SIGRTMAX;
    sigev.sigev_value.sival_int = RETRY_ID;

    if (timer_create(CLOCK_REALTIME, &sigev, &m_RetryTimerId) == 0)
     {
        itval.it_value.tv_sec = ConfigureManager_SingleTon::getInstance()->getRetryTime();
        itval.it_value.tv_nsec = 0L;
        itval.it_interval.tv_sec = itval.it_value.tv_sec;
        itval.it_interval.tv_nsec = itval.it_value.tv_nsec;

        if (timer_settime(m_RetryTimerId, 0, &itval, &oitval) != 0)
        {
            Utility_SingleTon::printLog(LOG_ERROR,"[%s:%d#%s] <<<< time_settime >>>> %s\n",
                                __FILE__, __LINE__,__func__,strerror( errno ));
            return false;
        }
    }
    else
    {
        printLog(LOG_ERROR,"[%s:%d#%s] <<<< timer_create >>>> %s",
                                __FILE__, __LINE__,__func__,strerror( errno ));
        return false;
    }
    return true;
}

/**
* FUNCTION NAME : stopRetryCount
* @brief stop the retry count
*/
voidstopRetryCount()
{
    if(m_RetryTimerId != NULL) 
    {
        if(timer_delete(m_RetryTimerId) != 0)
        {
            printLog(LOG_ERROR,"[%s:%d#%s] Timer delete error [%d]=%s\n",__FILE__, __LINE__,__func__,errno,strerror(errno));
        }
        m_RetryCount = 0;
        m_RetryTimerId = NULL;
        printLog(LOG_INFO,"Retry Timer Stopped!\n");
    }
}

错误的打印日志是:

[src/manager.cpp:686#stopRetryCount] Timer delete error [22]=Invalid argument

我已经尝试了很多东西来找到困扰基于ARM的设备,但遗憾的是我无法解决问题。任何帮助都将受到高度赞赏。

编辑:

为了模拟我的情况,我已经创建了示例程序,现在我开始知道在错误的SAM-9处理器上定时器和线程无法正常工作,如果我在主线程中启动计时器并且如果我试图停止该计时器通过子线程,然后它给出了该错误。这是sample.cpp文件源:

#include "devicetimer.h"


int DeviceTimer::counter = 0;

timer_t DeviceTimer::testTimer1Id = 0;

void DeviceTimer::timer1Stop()
{

    if(testTimer1Id != 0)
    {
        if(timer_delete(testTimer1Id) == -1)
        {
            printf("Timer Delete Error\nError Number = %d\nError Message = %s\n",errno,strerror(errno));
            exit(1);
        }
        else
        {
            printf("Timer Delete Successfully\n");
        }       
    }
}

void DeviceTimer::signalHandler(int signo, siginfo_t* info, void* context)
{
    if (signo == SIGTIMER)
    {
        printf("counter %d\n\n",++counter);
    }
}

int DeviceTimer::timer1start()
{
    struct sigevent sigev; //signal event struct
    struct itimerspec itval;
    struct itimerspec oitval;
    struct sigaction sigact;


    sigemptyset(&sigact.sa_mask);
    sigact.sa_flags = SA_SIGINFO;
    sigact.sa_sigaction = signalHandler;

    // set up sigaction to catch signal
    if (sigaction(SIGTIMER, &sigact, NULL) == -1)
    {
        printf("time_settime error \n");
        return -1;
    }

    //Create the POSIX timer to generate signo
    sigev.sigev_notify = SIGEV_SIGNAL;
    sigev.sigev_signo = SIGTIMER;
    sigev.sigev_value.sival_int = 2;

    if (timer_create(CLOCK_REALTIME, &sigev, &testTimer1Id) == 0)
     {
        itval.it_value.tv_sec = 1;
        itval.it_value.tv_nsec = 0L;
        itval.it_interval.tv_sec = itval.it_value.tv_sec;
        itval.it_interval.tv_nsec = itval.it_value.tv_nsec;

        if (timer_settime(testTimer1Id, 0, &itval, &oitval) != 0)
        {
            printf("Error in set time \n");
            return -2;
        }
    }
    else
    {
        printf("Error in creating timer \n");
        return -3;
    }
    return 0;
}


void* DeviceTimer::threadFunction(void* data)
{
    DeviceTimer *deviceTimer = (DeviceTimer *) data;

    while(1)
    {
        printf(".\n");
        usleep(100000);
        if(counter > 25)
        {
            deviceTimer->timer1Stop();
            break;
        }
    }   
    return NULL;
}


void DeviceTimer::startThread()
{
    if(pthread_create(&m_Thread, NULL, DeviceTimer::threadFunction, this) != 0)
    {
        printf("Error in creating Thread\n");
        exit(1);
    }
}

int main()
{
    DeviceTimer deviceTimer;

    deviceTimer.timer1start();
    deviceTimer.startThread();

    while(1)
    {
    }

    return 0;   
}

以下是输出:

counter 26
Timer Delete Error
Error Number = 22
Error Message = Invalid argument

如果有人遇到同样的问题,请帮我解决这个问题。

谢谢&amp; BR, Yuvi

1 个答案:

答案 0 :(得分:1)

确保您的计时器ID是正确的类型(timer_t)。如果您不小心使用其他类型,可能会发生这类错误,因为timer_delete()无法识别该ID。