📄 posixtimer.c
字号:
/* posixTimer.c - posix timer demo *//* Copyright 1984-1997 Wind River Systems, Inc. *//*modification history--------------------01a,07nov97,mm added modification history and copyright*//* includes */#include "vxWorks.h"#include "time.h"#include "stdio.h"#include "string.h"#include "taskLib.h"#include "semLib.h"#include "msgQLib.h"/* defines */#define LONGTIME 362#define TIMER_MSG_QUEUE_LENGTH 20timer_t timerId; SEM_ID semSync;MSG_Q_ID timerQueueId;struct timerMsg{ int timerId;};struct itimerspec time_value;/* function prototype */void setup();void alarmHandle (timer_t ti, int arg);void timerDemo(){ timerQueueId=msgQCreate(TIMER_MSG_QUEUE_LENGTH,sizeof(struct timerMsg),MSG_Q_FIFO); semSync = semBCreate (SEM_Q_FIFO, SEM_EMPTY); /* Spawn the demoTask */ if (taskSpawn ("tPosicTimerTask",120, 0, 4096, (FUNCPTR)setup, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) == ERROR) { printf("posixTimerStartDemo: Error in spawning demoTask"); } }void alarmHandle (timer_t ti, int arg){ struct timerMsg msgItem; msgItem.timerId=1; msgQSend(timerQueueId,(char *)&msgItem,sizeof(struct timerMsg), WAIT_FOREVER,MSG_PRI_NORMAL); }void setup(){ int i; struct timerMsg msg_item; /* create a new timer and put its id in timerId */ printf("hahahahh\n"); timer_create (CLOCK_REALTIME, NULL, &timerId); /* When the timer goes off we want to call alarmHandle */ timer_connect (timerId,(VOIDFUNCPTR) alarmHandle,0); /* initialise time-value */ bzero ((char *) &time_value, sizeof (struct itimerspec)); /* Timer goes off after .5 secs */ time_value.it_value.tv_sec =1; for(i=0;i<1000000;i++) { /* arm timer */ timer_settime(timerId, 0, &time_value, NULL); if(msgQReceive(timerQueueId,(char *)&msg_item,sizeof(struct timerMsg), WAIT_FOREVER)) { if(msg_item.timerId==1) { printf ("Task woken up by alarm!\n"); } /* * When the timer goes off it sends a signal to the task that armed it. * The signal handler alarmHandle will be called in the context of this task. * As a result of the signal taskDelay will return with ERROR */ if(!(i%4)) { printf("Cancel the timer!\n"); timer_cancel (timerId); } } else { printf("hello\n"); } } /* Clean up any resources used by the timer */ /* timer_delete (timerId);*/}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -