1 | /// ----------------------------------------*- mode: C++; -*--
|
---|
2 | /// @file testqueue.c
|
---|
3 | /// Testing fastqueue.
|
---|
4 | /// ----------------------------------------------------------
|
---|
5 | /// $Id: testqueue.c 2872 2008-02-18 10:58:03Z bless $
|
---|
6 | /// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/fastqueue/testqueue.c $
|
---|
7 | // ===========================================================
|
---|
8 | //
|
---|
9 | // Copyright (C) 2005-2007, all rights reserved by
|
---|
10 | // - Institute of Telematics, Universitaet Karlsruhe (TH)
|
---|
11 | //
|
---|
12 | // More information and contact:
|
---|
13 | // https://projekte.tm.uka.de/trac/NSIS
|
---|
14 | //
|
---|
15 | // This program is free software; you can redistribute it and/or modify
|
---|
16 | // it under the terms of the GNU General Public License as published by
|
---|
17 | // the Free Software Foundation; version 2 of the License
|
---|
18 | //
|
---|
19 | // This program is distributed in the hope that it will be useful,
|
---|
20 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
22 | // GNU General Public License for more details.
|
---|
23 | //
|
---|
24 | // You should have received a copy of the GNU General Public License along
|
---|
25 | // with this program; if not, write to the Free Software Foundation, Inc.,
|
---|
26 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
---|
27 | //
|
---|
28 | // ===========================================================
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * @ingroup fastqueue
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <stdlib.h>
|
---|
36 | #include <stdio.h>
|
---|
37 | #include <pthread.h> /* Headers for POSIX-Threads */
|
---|
38 | #include <time.h> /* needed for getting Timestamps */
|
---|
39 |
|
---|
40 | #include "fastqueue.h"
|
---|
41 |
|
---|
42 | #define error_check(status,string) \
|
---|
43 | if (status==-1) perror(string);
|
---|
44 |
|
---|
45 | #define MAXELEMENTS 10000
|
---|
46 |
|
---|
47 | /*** global variables ***/
|
---|
48 | pthread_t producer_thread, /* Thread Objects (sim. to TCB) */
|
---|
49 | consumer_thread;
|
---|
50 |
|
---|
51 | float queuetime, porttime;
|
---|
52 |
|
---|
53 |
|
---|
54 | /** both queues must be created before monitor tasks are started **/
|
---|
55 | queue_t *consumer_cmdq; /** queue for consumertask **/
|
---|
56 | struct timespec ts_start,ts_end;
|
---|
57 | int status; /* Hold status from pthread_ calls */
|
---|
58 |
|
---|
59 | void *producertask(void * argp)
|
---|
60 | {
|
---|
61 | long i;
|
---|
62 | fprintf(stderr,"QUEUETEST started. Please wait.\n");
|
---|
63 | clock_gettime(CLOCK_REALTIME,&ts_start);
|
---|
64 | for (i=1; i<=MAXELEMENTS; i++)
|
---|
65 | enqueue_element_signal(consumer_cmdq, (void *) i);
|
---|
66 |
|
---|
67 | return NULL;
|
---|
68 | }
|
---|
69 |
|
---|
70 | void *consumertask(void * argp)
|
---|
71 | {
|
---|
72 | long j;
|
---|
73 | /** test queue **/
|
---|
74 | while ((j= (int) dequeue_element_wait(consumer_cmdq))<MAXELEMENTS);
|
---|
75 |
|
---|
76 | clock_gettime(CLOCK_REALTIME,&ts_end);
|
---|
77 | queuetime= ts_end.tv_sec-ts_start.tv_sec +
|
---|
78 | (ts_end.tv_nsec-ts_start.tv_nsec)*1E-9;
|
---|
79 | fprintf(stderr,"QUEUETEST stopped (%d elements): %gs\n",MAXELEMENTS,queuetime);
|
---|
80 | return NULL;
|
---|
81 | }
|
---|
82 |
|
---|
83 | int
|
---|
84 | main(int argc, char **argv)
|
---|
85 | {
|
---|
86 | void *exit_value; /* for pthread_join */
|
---|
87 |
|
---|
88 | fprintf(stderr,"<program start>\n");
|
---|
89 | /** create a queue **/
|
---|
90 | if ((consumer_cmdq= create_queue("testqueue"))==NULL) exit(1);
|
---|
91 |
|
---|
92 | /** Start Threads **/
|
---|
93 | status= pthread_create(&consumer_thread,
|
---|
94 | NULL,
|
---|
95 | consumertask,
|
---|
96 | NULL);
|
---|
97 | error_check(status,"fatal: cannot create consumertask");
|
---|
98 |
|
---|
99 | status= pthread_create(&producer_thread,
|
---|
100 | NULL,
|
---|
101 | producertask,
|
---|
102 | NULL);
|
---|
103 | error_check(status,"fatal: cannot create producertask");
|
---|
104 |
|
---|
105 | /** wait for threads to end **/
|
---|
106 | status= pthread_join(producer_thread, &exit_value);
|
---|
107 | error_check(status,"pthread_join");
|
---|
108 |
|
---|
109 | status= pthread_join(consumer_thread, &exit_value);
|
---|
110 | error_check(status,"pthread_join");
|
---|
111 |
|
---|
112 | /** destroy all queues **/
|
---|
113 | status= destroy_queue(consumer_cmdq);
|
---|
114 | error_check(status,"destroying consumer queue");
|
---|
115 |
|
---|
116 | fprintf(stderr,"<program exited normally>\n");
|
---|
117 | return 0;
|
---|
118 | }
|
---|
119 | /**** end of source ****/
|
---|
120 |
|
---|
121 | //@}
|
---|