c++ - 获取无效参数错误 (22)

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

编辑:

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

最佳答案

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

关于c++ - 获取无效参数错误 (22),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11009617/

相关文章:

linux - 如何在命令行中合并图像?

linux - 有没有办法使用 perl 在我的 linux 中找到有效语言环境列表?

php - 64 位 Linux/Ubuntu 和 openssl 问题(无法读取符号 : Bad value)

linux - 是否可以将超过七个参数传递给 arm linux 中的系统调用?

c - 未定义对 _write 的引用,该引用在库中实现

c++ - 避免在增强精神语义 Action 中构建模板

c++ - 如果 "make -j2"和 "make"可以加速制作过程并解决所有与制作序列相关的问题?

c++ - Lambda 和 std::function

c - 在 core_cm4.h 上,为什么会有类似 ((uint32_t)(int32_t)IRQn) 的转换?

c# - 从 C# 调用 C