1

我正在 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 的设备的问题,但不幸的是我无法解决这些问题。任何帮助将不胜感激。

编辑:

为了模拟我的情况,我创建了示例程序,现在我知道在 misral 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

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

谢谢和BR,尤维

4

1 回答 1

1

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

于 2012-06-13T09:29:22.437 回答