OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [compat/] [posix/] [current/] [tests/] [pmqueue2.c] - Blame information for rev 810

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
/*========================================================================
2
//
3
//      pmqueue2.c
4
//
5
//      POSIX Message queues tests - mq_notify
6
//
7
//========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under
14
// the terms of the GNU General Public License as published by the Free
15
// Software Foundation; either version 2 or (at your option) any later
16
// version.
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21
// for more details.
22
//
23
// You should have received a copy of the GNU General Public License
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26
//
27
// As a special exception, if other files instantiate templates or use
28
// macros or inline functions from this file, or you compile this file
29
// and link it with other works to produce a work based on this file,
30
// this file does not by itself cause the resulting work to be covered by
31
// the GNU General Public License. However the source code for this file
32
// must still be made available in accordance with section (3) of the GNU
33
// General Public License v2.
34
//
35
// This exception does not invalidate any other reasons why a work based
36
// on this file might be covered by the GNU General Public License.
37
// -------------------------------------------
38
// ####ECOSGPLCOPYRIGHTEND####
39
//========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):     jlarmour
43
// Contributors:
44
// Date:          2000-05-18
45
// Purpose:       This file provides tests for POSIX mqueue mq_notify
46
// Description:
47
// Usage:
48
//
49
//####DESCRIPTIONEND####
50
//
51
//======================================================================
52
*/
53
 
54
/* CONFIGURATION */
55
 
56
#include <pkgconf/posix.h>
57
 
58
#ifndef CYGPKG_POSIX_MQUEUES
59
# define NA_MSG "Message queues not configured"
60
#elif !defined(CYGPKG_POSIX_SIGNALS)
61
# define NA_MSG "No POSIX signals configured"
62
#endif
63
 
64
#ifdef NA_MSG
65
#include <cyg/infra/testcase.h>      // test API
66
void
67
cyg_user_start(void)
68
{
69
    CYG_TEST_INIT();
70
    CYG_TEST_NA( NA_MSG );
71
}
72
 
73
#else
74
 
75
/* INCLUDES */
76
 
77
#include <fcntl.h>                   // O_*
78
#include <errno.h>                   // errno
79
#include <sys/stat.h>                // file modes
80
#include <mqueue.h>                  // Mqueue Header
81
#include <cyg/infra/testcase.h>      // test API
82
#include <signal.h>                  // signals
83
 
84
/* GLOBALS */
85
sig_atomic_t signals=0;
86
char buf[20];
87
unsigned int prio;
88
 
89
 
90
/* FUNCTIONS */
91
 
92
static int
93
my_memcmp(const void *m1, const void *m2, size_t n)
94
{
95
    char *s1 = (char *)m1;
96
    char *s2 = (char *)m2;
97
 
98
    while (n--) {
99
        if (*s1 != *s2)
100
            return *s1 - *s2;
101
        s1++;
102
        s2++;
103
    }
104
    return 0;
105
} // my_memcmp()
106
 
107
static char *
108
my_strcpy(char *s1, const char *s2)
109
{
110
    char *s = s1;
111
    while (*s2) {
112
        *s1++ = *s2++;
113
    }
114
    return s;
115
} // my_strcpy()
116
 
117
static size_t
118
my_strlen(const char *s)
119
{
120
    const char *start = s;
121
    while (*s)
122
        s++;
123
    return (s - start);
124
} // my_strcpy()
125
 
126
 
127
 
128
//************************************************************************
129
 
130
static void
131
sigusr1_handler( int signo, siginfo_t *info, void *context )
132
{
133
    ssize_t recvlen;
134
    char mybuf[20];
135
    unsigned int myprio;
136
    mqd_t *q = (mqd_t *)info->si_value.sival_ptr;
137
 
138
    CYG_TEST_PASS_FAIL( SIGUSR1 == signo, "correct signal number #1" );
139
    CYG_TEST_PASS_FAIL( SIGUSR1 == info->si_signo, "correct signal number #2" );
140
    CYG_TEST_PASS_FAIL( SI_MESGQ == info->si_code, "correct signal code" );
141
 
142
    signals++;
143
 
144
    // retrieve message and compare with buf
145
    recvlen = mq_receive( *q, mybuf, sizeof(mybuf), &myprio );
146
    CYG_TEST_PASS_FAIL( recvlen == my_strlen(buf),
147
                        "receive message length" );
148
    CYG_TEST_PASS_FAIL( 0 == my_memcmp( buf, mybuf, my_strlen(buf)),
149
                        "received message data intact" );
150
    CYG_TEST_PASS_FAIL( prio == myprio,
151
                        "received at correct priority" );
152
}
153
 
154
//************************************************************************
155
 
156
int
157
main(void)
158
{
159
    mqd_t q1;
160
    struct mq_attr attr;
161
    mode_t mode;
162
    int err;
163
    ssize_t recvlen;
164
    char mybuf[20];
165
    unsigned int myprio;
166
    struct sigevent ev;
167
    struct sigaction act;
168
 
169
    CYG_TEST_INIT();
170
    CYG_TEST_INFO( "Starting POSIX message test 2" );
171
 
172
#if 0
173
    if ( 0 != pthread_create( &thr, NULL, &thread, NULL ) ) {
174
        CYG_TEST_FAIL_FINISH( "Couldn't create a helper thread" );
175
    }
176
#endif
177
 
178
    attr.mq_flags = 0;
179
    attr.mq_maxmsg = 4;
180
    attr.mq_msgsize = 20;
181
    mode = S_IRWXU|S_IRWXG|S_IRWXO; // rwx for all
182
 
183
    q1 = mq_open( "/mq1", O_CREAT|O_NONBLOCK|O_RDWR, mode, &attr );
184
    CYG_TEST_PASS_FAIL( q1 != (mqd_t)-1, "simple mq_open (write only)" );
185
 
186
    err = mq_getattr( q1, &attr );
187
    CYG_TEST_PASS_FAIL( 0 == err, "simple mq_getattr" );
188
    CYG_TEST_PASS_FAIL( (4 == attr.mq_maxmsg) &&
189
                        (20 == attr.mq_msgsize) &&
190
                        (O_NONBLOCK == (attr.mq_flags & O_NONBLOCK)) &&
191
                        (O_RDWR == (attr.mq_flags & O_RDWR)) &&
192
                        (0 == attr.mq_curmsgs ), "getattr attributes correct" );
193
 
194
 
195
    act.sa_sigaction = &sigusr1_handler;
196
    sigfillset( &act.sa_mask ); // enable all signals
197
    act.sa_flags = SA_SIGINFO;
198
 
199
    if ( 0 != sigaction( SIGUSR1, &act, NULL ) ) {
200
        CYG_TEST_FAIL_FINISH( "Couldn't register signal handler" );
201
    }
202
 
203
    ev.sigev_notify = SIGEV_SIGNAL;
204
    ev.sigev_signo = SIGUSR1;
205
    ev.sigev_value.sival_ptr = (void *)&q1;
206
 
207
    err = mq_notify( q1, &ev );
208
    CYG_TEST_PASS_FAIL( 0 == err, "simple mq_notify" );
209
 
210
    my_strcpy( buf, "Vik is the best" );
211
    prio = 7;
212
    err = mq_send( q1, buf, my_strlen(buf), prio );
213
 
214
    CYG_TEST_PASS_FAIL( 0 == err, "mq_send #1" );
215
 
216
    CYG_TEST_PASS_FAIL( 1 == signals, "got notification" );
217
 
218
    my_strcpy( buf, "Scrummy Vik" );
219
    prio = 6;
220
    err = mq_send( q1, buf, my_strlen(buf), prio );
221
    CYG_TEST_PASS_FAIL( 0 == err, "mq_send #2" );
222
 
223
    CYG_TEST_PASS_FAIL( 1 == signals, "correctly didn't get notification" );
224
 
225
    recvlen = mq_receive( q1, mybuf, sizeof(mybuf), &myprio );
226
    CYG_TEST_PASS_FAIL( recvlen == my_strlen(buf),
227
                        "receive message length" );
228
    CYG_TEST_PASS_FAIL( 0 == my_memcmp( buf, mybuf, my_strlen(buf)),
229
                        "received message data intact" );
230
    CYG_TEST_PASS_FAIL( prio == myprio,
231
                        "received at correct priority" );
232
 
233
    err = mq_notify( q1, &ev );
234
    CYG_TEST_PASS_FAIL( 0 == err, "mq_notify #2" );
235
 
236
    err = mq_notify( q1, &ev );
237
    CYG_TEST_PASS_FAIL( -1 == err, "second mq_notify returns error" );
238
    CYG_TEST_PASS_FAIL( EBUSY == errno,
239
                        "errno correct for second mq_notify error" );
240
 
241
    err = mq_notify( q1, NULL );
242
    CYG_TEST_PASS_FAIL( 0 == err, "clear notification" );
243
 
244
    my_strcpy( buf, "Vik is k3wl" );
245
    prio = 8;
246
    err = mq_send( q1, buf, my_strlen(buf), prio );
247
 
248
    CYG_TEST_PASS_FAIL( 0 == err, "mq_send #2" );
249
 
250
    CYG_TEST_PASS_FAIL( 1 == signals, "correctly didn't get notification #2" );
251
 
252
    recvlen = mq_receive( q1, mybuf, sizeof(mybuf), &myprio );
253
    CYG_TEST_PASS_FAIL( recvlen == my_strlen(buf),
254
                        "receive message length" );
255
    CYG_TEST_PASS_FAIL( 0 == my_memcmp( buf, mybuf, my_strlen(buf)),
256
                        "received message data intact" );
257
    CYG_TEST_PASS_FAIL( prio == myprio,
258
                        "received at correct priority" );
259
 
260
    err = mq_close( q1 );
261
    CYG_TEST_PASS_FAIL( 0 == err, "mq_close" );
262
 
263
    err = mq_unlink( "/mq1" );
264
    CYG_TEST_PASS_FAIL( 0 == err, "mq_unlink" );
265
 
266
    CYG_TEST_EXIT("POSIX message test 2");
267
 
268
    return 0;
269
} // main()
270
 
271
//------------------------------------------------------------------------
272
 
273
#endif
274
 
275
/* EOF pmqueue2.c */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.