-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer_code.c
128 lines (104 loc) · 4.65 KB
/
timer_code.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
scriptname: timer_code.c
author: Kurt Stewart
purpose: assignment 3 pt2
description:
adapted from example code from timer_create() qnx documentation the following code has two threads
that wait on monotonic timer interrupts
*/
#include <stdio.h>
#include <time.h>
#include <sys/netmgr.h>
#include <sys/neutrino.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#define MY_PULSE_CODE _PULSE_CODE_MINAVAIL
#define Other _PULSE_CODE_MAXAVAIL
static void *myfunction( void *args );
static void *myfunction2( void *args );
typedef union {
struct _pulse pulse;
/* your other message structures would go
here too */
} my_message_t;
int chid; //chan
int rcvid; // receive id
my_message_t msg; // message object
int chid2; //chan
int rcvid2; // receive id
my_message_t msg2; // message object
main()
{
struct sigevent event; //create signal event struct
struct itimerspec itime; //create timer struct
timer_t timer_id; //timer object
int result;
// Create Channel
chid = ChannelCreate(0);
SIGEV_INTR_INIT(&event); //Add interrupt ability to the signal event
InterruptAttachEvent(10,&event,0); // attach the IRQ10 with the signal event interrupt with no flag
//event.sigev_intr_init()
event.sigev_notify = SIGEV_PULSE; //the notification of the signal event is pulse type
event.sigev_coid = ConnectAttach(ND_LOCAL_NODE, 0,chid,_NTO_SIDE_CHANNEL, 0); //the channel on which the signal will be listening is the created channel
event.sigev_priority = getprio(0); //priority of 0
event.sigev_code = MY_PULSE_CODE; //the code that executes upon the signal event is MY_PULSE_CODE
timer_create(CLOCK_MONOTONIC, &event, &timer_id); //create a monotonic timer which signals the event of timerid
itime.it_value.tv_sec = 1;
/* 500 million nsecs = .5 secs */
itime.it_value.tv_nsec = 500000000;
itime.it_interval.tv_sec = 10;
/* 500 million nsecs = .5 secs */
itime.it_interval.tv_nsec = 500000000;
timer_settime(timer_id, 0, &itime, NULL);
pthread_t tid;
result = pthread_create( &tid, NULL, myfunction, NULL );
//pthread_setschedprio( tid,9 );
//----------------------------------------------------------------------------------------------------
struct sigevent event2; //create signal event struct
struct itimerspec itime2; //create timer struct
timer_t timer_id2; //timer object
int result2;
// Create Channel
chid2 = ChannelCreate(0);
SIGEV_INTR_INIT(&event2); //Add interrupt ability to the signal event
InterruptAttachEvent(9,&event2,0); // attach the IRQ10 with the signal event interrupt with no flag
//event.sigev_intr_init()
event2.sigev_notify = SIGEV_PULSE; //the notification of the signal event is pulse type
event2.sigev_coid = ConnectAttach(ND_LOCAL_NODE, 0,chid2,_NTO_SIDE_CHANNEL, 0); //the channel on which the signal will be listening is the created channel
event2.sigev_priority = getprio(0); //priority of 0
event2.sigev_code = MY_PULSE_CODE; //the code that executes upon the signal event is MY_PULSE_CODE
timer_create(CLOCK_MONOTONIC, &event2, &timer_id2); //create a monotonic timer which signals the event of timerid
itime2.it_value.tv_sec = 1;
/* 500 million nsecs = .5 secs */
itime2.it_value.tv_nsec = 500000000;
itime2.it_interval.tv_sec = 1;
/* 500 million nsecs = .5 secs */
itime2.it_interval.tv_nsec = 500000000;
timer_settime(timer_id2, 0, &itime2, NULL);
pthread_t tid2;
result2 = pthread_create( &tid2, NULL, myfunction2, NULL );
//pthread_setschedprio( tid2,9 );
//loop forever
while(1){}
}//end of main
static void *myfunction( void *args ){
while(1){
rcvid = MsgReceive(chid, &msg, sizeof(msg), NULL); //wait for message on the channel
if (rcvid == 0) { /* we got a pulse */
if (msg.pulse.code == MY_PULSE_CODE) {
printf("we got a pulse from our timer\n");
} /* else other pulses ... */
} /* else other messages ... */
}
}
static void *myfunction2( void *args ){
while(1){
rcvid2 = MsgReceive(chid2, &msg2, sizeof(msg2), NULL); //wait for message on the channel
if (rcvid2 == 0) { /* we got a pulse */
if (msg2.pulse.code == MY_PULSE_CODE) {
printf("we got a pulse from our second timer\n");
} /* else other pulses ... */
} /* else other messages ... */
}
}