Subversion Repositories shark

Rev

Rev 1086 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1085 pj 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/**
20
 ------------
21
 CVS :        $Id: ptest6.c,v 1.1.1.1 2002-09-02 09:37:47 pj Exp $
22
 
23
 File:        $File$
24
 Revision:    $Revision: 1.1.1.1 $
25
 Last update: $Date: 2002-09-02 09:37:47 $
26
 ------------
27
 
28
 Posix test 6:
29
 
30
   message queues
31
 
32
   main thread:
33
     set a sigevent to sigev_thread on a mailbox (that creates thread 2)
34
     creates thread 1
35
     waits t=3.5 sec.
36
     pthread_cancel(T4)
37
 
38
   thread 1:
39
     send a msg to the mailbox (the event fires and thread 2 is created)
40
 
41
   thread 2:
42
     receive the msg sent by thread 1
43
     set the event to a signal
44
     creates thread 3 and 4
45
     waits t = 1 sec
46
     send another msg
47
 
48
   thread 3:
49
     receive the msg sent by 2 (it blocks!)
50
     waits t = 2 sec
51
     send 5 msgs (with different priorities!!!
52
 
53
   thread 4:
54
     receives 5 msgs every 0.5 sec.
55
     then receive another message that never will arrive...
56
 
57
 non standard function used:
58
   cprintf
59
   sys_gettime
60
   keyboard stuffs
61
   sys_end
62
 
63
**/
64
 
65
/*
66
 * Copyright (C) 2000 Paolo Gai
67
 *
68
 * This program is free software; you can redistribute it and/or modify
69
 * it under the terms of the GNU General Public License as published by
70
 * the Free Software Foundation; either version 2 of the License, or
71
 * (at your option) any later version.
72
 *
73
 * This program is distributed in the hope that it will be useful,
74
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
75
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
76
 * GNU General Public License for more details.
77
 *
78
 * You should have received a copy of the GNU General Public License
79
 * along with this program; if not, write to the Free Software
80
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
81
 *
82
 */
83
 
84
#include <sys/types.h>
85
#include <pthread.h>
86
#include <mqueue.h>
87
 
88
#include <kernel/kern.h>
89
#include <drivers/keyb.h>
90
 
91
struct sigevent ev25, evtask;
92
pthread_attr_t task_attr;
93
struct sched_param task_param;
94
mqd_t mq;
95
pthread_t T1,T2,T3,T4,T5;
96
 
97
#define MESSAGE_LENGTH 100
98
 
99
void *t1(void *arg)
100
{
101
  cprintf("T1: started, sending a message\n");
102
  if (mq_send(mq,"Donald Duck",12,1))
103
  { cprintf("T1: mq_send returns errno %d\n",errno); return 0; }
104
 
105
  cprintf("T1: ending...\n");
106
  return 0;
107
}
108
 
109
void *t4(void *arg);
110
void *t3(void *arg);
111
 
112
void t2(union sigval value)
113
{
114
  ssize_t x;
115
  char buf[MESSAGE_LENGTH];
116
  int prio;
117
 
118
  cprintf("T2: value = %d, receiving a message\n", value.sival_int);
119
 
120
  x = mq_receive(mq,buf,MESSAGE_LENGTH,&prio);
121
 
122
  cprintf("T2: received message: length=%ld, prio=%d, text=%s, notify...\n",
123
              x,prio,buf);
124
 
125
  if (mq_notify(mq, &ev25))
126
  { cprintf("T2: mq_notify returns errno %d\n",errno); sys_end(); }
127
 
128
  cprintf("T2: waiting t = 1 sec.\n");
129
  while (sys_gettime(NULL)<1000000);
130
  cprintf("T2: 1 sec. reached, sending another message and creating T3 and T4, \n");
131
 
132
  if (mq_send(mq,"Mickey Mouse",13,1))
133
  { cprintf("T2: mq_send returns errno %d\n",errno); sys_end(); }
134
 
135
  pthread_create(&T3, NULL, t3, NULL);
136
  pthread_create(&T4, NULL, t4, NULL);
137
 
138
  cprintf("T2: ending...\n");
139
}
140
 
141
void *t3(void *arg)
142
{
143
  ssize_t x;
144
  char buf[MESSAGE_LENGTH];
145
  int prio;
146
 
147
  cprintf("T3: waiting a message...\n");
148
 
149
  x = mq_receive(mq,buf,MESSAGE_LENGTH,&prio);
150
 
151
  // mickey mouse
152
  cprintf("T3: received message: length=%ld, prio=%d, text=%s\n",
153
              x,prio,buf);
154
 
155
  cprintf("T3: waiting t = 1.5 sec.\n");
156
  while (sys_gettime(NULL)<1500000);
157
  cprintf("T3: 2 sec. reached, sending 5 messages\n");
158
 
159
  if (mq_send(mq,"Goofy",6,1))
160
  { cprintf("T3: mq_send1 returns errno %d\n",errno); sys_end(); }
161
  cprintf("");
162
 
163
  if (mq_send(mq,"Minnie",7,1))
164
  { cprintf("T3: mq_send2 returns errno %d\n",errno); sys_end(); }
165
  cprintf("");
166
 
167
  if (mq_send(mq,"Pluto",6,2))  // NB: different priority!!!
168
  { cprintf("T3: mq_send3 returns errno %d\n",errno); sys_end(); }
169
  cprintf("");
170
 
171
  if (mq_send(mq,"Rocker Duck",12,2))  // NB: different priority!!!
172
  { cprintf("T3: mq_send4 returns errno %d\n",errno); sys_end(); }
173
  cprintf("");
174
 
175
  if (mq_send(mq,"Oncle Scroodge",15,2))  // NB: different priority!!!
176
  { cprintf("T3: mq_send5 returns errno %d\n",errno); sys_end(); }
177
  cprintf("");
178
 
179
  cprintf("T3: ending...\n");
180
 
181
  return 0;
182
}
183
 
184
void t4exit(void *arg)
185
{
186
  cprintf("T4: AAAARRRRGGGHHH!!! killed by someone...\n");
187
}
188
 
189
void *t4(void *arg)
190
{
191
  ssize_t x;
192
  char buf[MESSAGE_LENGTH];
193
  int prio;
194
 
195
  cprintf("T4: waiting t = 2.2 sec.\n");
196
 
197
  while (sys_gettime(NULL)<2200000);
198
  x = mq_receive(mq,buf,MESSAGE_LENGTH,&prio);
199
  cprintf("T4: received message: length=%ld, prio=%d, text=%s\n",x,prio,buf);
200
 
201
  while (sys_gettime(NULL)<2400000);
202
  x = mq_receive(mq,buf,MESSAGE_LENGTH,&prio);
203
  cprintf("T4: received message: length=%ld, prio=%d, text=%s\n",x,prio,buf);
204
  while (sys_gettime(NULL)<2600000);
205
 
206
  x = mq_receive(mq,buf,MESSAGE_LENGTH,&prio);
207
  cprintf("T4: received message: length=%ld, prio=%d, text=%s\n",x,prio,buf);
208
  while (sys_gettime(NULL)<2800000);
209
 
210
  x = mq_receive(mq,buf,MESSAGE_LENGTH,&prio);
211
  cprintf("T4: received message: length=%ld, prio=%d, text=%s\n",x,prio,buf);
212
  while (sys_gettime(NULL)<3000000);
213
 
214
  x = mq_receive(mq,buf,MESSAGE_LENGTH,&prio);
215
  cprintf("T4: received message: length=%ld, prio=%d, text=%s\n",x,prio,buf);
216
 
217
  pthread_cleanup_push(t4exit,NULL);
218
  x = mq_receive(mq,buf,MESSAGE_LENGTH,&prio);
219
  cprintf("T4: received message: length=%ld, prio=%d, text=%s\n",x,prio,buf);
220
  pthread_cleanup_pop(0);
221
 
222
  return 0;
223
}
224
 
225
void signal_handler(int signo, siginfo_t *info, void *extra)
226
{
227
  cprintf("Signal %d code=%s value=%d task=%d time=%ldusec\n",
228
              info->si_signo,
229
              (info->si_code == SI_TIMER) ? "Timer" : "Other",
230
              info->si_value.sival_int,
231
              info->si_task,
232
              sys_gettime(NULL));
233
}
234
 
235
void fine(KEY_EVT *e)
236
{
237
  sys_end();
238
}
239
 
240
int main(int argc, char **argv)
241
{
242
//  int err;
243
  struct sigaction sig_act;
244
  struct mq_attr attr;
245
 
246
    KEY_EVT emerg;
247
    //keyb_set_map(itaMap);
248
    emerg.ascii = 'x';
249
    emerg.scan = KEY_X;
250
    emerg.flag = ALTL_BIT;
251
    keyb_hook(emerg,fine);
252
 
253
  sig_act.sa_sigaction = (void *) signal_handler;
254
  sig_act.sa_flags = SA_SIGINFO;
255
  sigemptyset(&sig_act.sa_mask);
256
  sigaction(25, &sig_act, NULL);
257
 
258
  // set ev25, evtask
259
  ev25.sigev_notify           = SIGEV_SIGNAL;
260
  ev25.sigev_signo            = 25;
261
  ev25.sigev_value.sival_int  = 555;
262
 
263
  evtask.sigev_notify            = SIGEV_THREAD;
264
  evtask.sigev_value.sival_int   = 777;
265
  evtask.sigev_notify_function   = t2;
266
  evtask.sigev_notify_attributes = &task_attr;
267
 
268
  // set pthread attributes
269
  pthread_attr_init(&task_attr);
270
  pthread_attr_setdetachstate(&task_attr, PTHREAD_CREATE_DETACHED);
271
  pthread_attr_setschedpolicy(&task_attr, SCHED_FIFO);
272
  task_param.sched_priority = 10;
273
  pthread_attr_setschedparam(&task_attr, &task_param);
274
 
275
  // set mqueue attributes
276
  attr.mq_flags   = 0;
277
  attr.mq_maxmsg  = 3;
278
  attr.mq_msgsize = MESSAGE_LENGTH;
279
 
280
  // create the message queue
281
  if ((mq = mq_open("mq", O_CREAT|O_RDWR, 0, &attr)) == -1)
282
  { cprintf("main: mq_open returns errno %d\n",errno); return 0; }
283
 
284
  if (mq_notify(mq, &evtask))
285
  { cprintf("main: mq_notify returns errno %d\n",errno); return 0; }
286
 
287
  cprintf("main: created mq, creating T1...\n");
288
 
289
  pthread_create(&T1, NULL, t1, NULL);
290
 
291
  cprintf("main: waiting t= 3.5 sec., then kill T4...\n");
292
 
293
  while (sys_gettime(NULL)<3500000);
294
 
295
  pthread_cancel(T4);
296
 
297
  cprintf("main: ending...\n");
298
 
299
  return 0;
300
}