source: rtems/testsuites/psxtests/psxmsgq04/init.c @ 9a4eca5

5
Last change on this file since 9a4eca5 was c8982e5, checked in by Sebastian Huber <sebastian.huber@…>, on 04/26/16 at 19:20:31

posix: Simplify message queues

The mq_open() function returns a descriptor to a POSIX message queue
object identified by a name. This is similar to sem_open(). In
contrast to the POSIX semaphore the POSIX message queues use a separate
object for the descriptor. This extra object is superfluous, since the
object identifier can be used directly for this purpose, just like for
the semaphores.

Update #2702.
Update #2555.

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2012.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.org/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <pmacros.h>
15#include <unistd.h>
16#include <errno.h>
17#include <tmacros.h>
18
19#include <fcntl.h>           /* For O_* constants */
20#include <sys/stat.h>        /* For mode constants */
21#include <mqueue.h>
22
23#include "test_support.h"
24
25const char rtems_test_name[] = "PSXMSGQ 4";
26
27/* forward declarations to avoid warnings */
28void *POSIX_Init(void *argument);
29
30void *POSIX_Init(
31  void *argument
32)
33{
34  struct mq_attr          attr;
35  mqd_t                   Queue, second_Queue;
36  int                     sc;
37  Heap_Information_block  start;
38  size_t                  to_alloc;
39  void                   *alloced;
40  bool                    sb;
41  const char             *name;
42
43  TEST_BEGIN();
44
45  attr.mq_maxmsg = 1;
46  attr.mq_msgsize = sizeof(int);
47
48  puts( "Init - Open message queue instance 1" );
49  Queue = mq_open( "Queue", O_CREAT | O_RDWR, 0x777, &attr );
50  if ( Queue == (-1) )
51    perror( "mq_open failed" );
52  rtems_test_assert( Queue != (-1) );
53
54  puts( "Init - Open message queue instance 2 - FAIL - ENFILE " );
55  second_Queue = mq_open( "Queue2", O_CREAT | O_RDWR, 0x777, &attr );
56  if ( second_Queue != (-1) )
57    puts( "mq_open did not failed" );
58  rtems_test_assert( second_Queue == (-1) );
59  rtems_test_assert( errno == ENFILE );
60
61  puts( "Init - Unlink message queue instance 1" );
62  sc = mq_unlink( "Queue" );
63  if ( sc != 0 )
64    perror( "mq_unlink failed" );
65  rtems_test_assert( sc == 0 );
66
67  puts( "Init - Close message queue instance 1" );
68  sc = mq_close( Queue );
69  if ( sc != 0 )
70    perror( "mq_close failed" );
71  rtems_test_assert( sc == 0 );
72
73  sb = rtems_workspace_get_information( &start );
74  rtems_test_assert( start.Free.number == 1 );
75  to_alloc = start.Free.largest;
76
77  /* find the largest we can actually allocate */
78  while ( 1 ) {
79    sb = rtems_workspace_allocate( to_alloc, &alloced );
80    if ( sb )
81      break;
82    to_alloc -= 4;
83  }
84
85  rtems_workspace_free( alloced );
86
87  /*
88   * Now do the test
89   */
90  puts( "Init - Memory allocation error test" );
91
92  name = Get_Longest_Name();
93  do {
94    sb = rtems_workspace_allocate( to_alloc, &alloced );
95    rtems_test_assert( sb );
96
97    second_Queue = mq_open(name,O_CREAT | O_RDWR, 0x777, &attr );
98
99    /* free the memory we snagged, then check the status */
100    rtems_workspace_free( alloced );
101
102    to_alloc -= 4;
103  } while ( second_Queue == -1 );
104
105  puts( "Init - Message Queue created" );
106
107  puts( "Init - Unlink message queue" );
108    sc = mq_unlink( name );
109    if ( sc != 0 )
110      perror( "mq_unlink failed" );
111    rtems_test_assert( sc == 0 );
112
113  puts( "Init - Close message queue" );
114    sc = mq_close( second_Queue );
115    if ( sc != 0 )
116      perror( "mq_close failed" );
117    rtems_test_assert( sc == 0 );
118
119  TEST_END();
120  rtems_test_exit( 0 );
121
122  return NULL; /* just so the compiler thinks we returned something */
123}
124
125/* configuration information */
126
127#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
128#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
129
130#define CONFIGURE_POSIX_INIT_THREAD_TABLE
131
132/* account for message buffers and string names */
133#define CONFIGURE_MESSAGE_BUFFER_MEMORY \
134    (2 * CONFIGURE_MESSAGE_BUFFERS_FOR_QUEUE(1, sizeof(int)))
135
136#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
137
138#define CONFIGURE_MAXIMUM_POSIX_THREADS                   1
139#define CONFIGURE_MAXIMUM_POSIX_MESSAGE_QUEUES            1
140
141#define CONFIGURE_POSIX_INIT_THREAD_TABLE
142
143#define CONFIGURE_INIT
144#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.