source: rtems/testsuites/psxtests/psxsem01/init.c @ 8c5cc6b2

4.104.114.84.95
Last change on this file since 8c5cc6b2 was 8c5cc6b2, checked in by Jennifer Averett <Jennifer.Averett@…>, on Nov 18, 1999 at 7:51:13 PM

Added test code for named semaphores and moved to use of directive_failed
family of macros to produce better checks and diagnostic messages.

  • Property mode set to 100644
File size: 6.7 KB
Line 
1#define CONFIGURE_INIT
2#include "system.h"
3#include <sched.h>
4#include <fcntl.h>
5#include <time.h>
6#include <tmacros.h>
7
8void *POSIX_Init(
9  void *argument
10)
11{
12  int             status;
13  int             value;
14  int             i;
15  sem_t           sems[CONFIGURE_MAXIMUM_POSIX_SEMAPHORES];
16  sem_t           sem2;
17  sem_t           *n_sem1;
18  sem_t           *n_sem2;
19  sem_t           testsem;
20  struct timespec waittime;
21  char            failure_msg[80];
22
23  puts( "\n\n*** POSIX SEMAPHORE MANAGER TEST 1 ***" );
24
25  puts( "Init: sem_init - SUCCESSFUL" );
26  for (i = 0; i < CONFIGURE_MAXIMUM_POSIX_SEMAPHORES; i++) {
27    status = sem_init(&sems[i], 0, i);
28    sprintf(failure_msg, "sem_init %d", i );
29    fatal_directive_status( status, 0, failure_msg);
30  }
31  puts( "Init: sem_init - UNSUCCESSFUL (ENOSPC)" );
32  status = sem_init(&sem2, 0, 1);
33  fatal_directive_status( status, -1, "sem_init error return status");
34  fatal_directive_status( errno, ENOSPC, "sem_init errorno ENOSPC" );
35 
36  puts( "Init: sem_init - UNSUCCESSFUL (ENOSYS -- pshared not supported)" );
37  status = sem_init(&sem2, 1, 1);
38  fatal_directive_status( status, -1, "sem_init error return status");
39  fatal_directive_status( errno, ENOSYS, "sem_init errno set to ENOSYS");
40 
41  puts( "Init: sem_getvalue - SUCCESSFUL ");
42  for (i = 0; i < CONFIGURE_MAXIMUM_POSIX_SEMAPHORES; i++) {
43    status = sem_getvalue(&sems[i], &value);
44    sprintf( failure_msg, "sem_getvalue %d", i );
45    fatal_directive_status( status, 0, failure_msg );
46    fatal_directive_status( value, i, "sem_getvalue correct value" );
47  }
48  puts( "Init: sem_getvalue - UNSUCCESSFUL ");
49  status = sem_getvalue(&sem2, &value);
50  fatal_directive_status( status, -1, "sem_init error return status"); 
51  fatal_directive_status( errno, EINVAL, "sem_getvalue errno EINVAL");
52
53  puts( "Init: sem_destroy - SUCCESSFUL" );
54  status = sem_destroy(&sems[0]);
55  fatal_directive_status( status, 0, "sem_destroy semaphore 0"); 
56
57  puts( "Init: sem_destroy - UNSUCCESSFUL (EINVAL)" );
58  status = sem_destroy(&sem2);
59  fatal_directive_status( status, -1, "sem_init error return status");
60  fatal_directive_status( errno, EINVAL, "sem_destroy errno EINVAL");
61
62  puts( "Init: sem_wait - SUCCESSFUL" );
63  status = sem_wait(&sems[1]);
64  fatal_directive_status( status, 0, "sem_wait semaphore 1"); 
65
66  puts( "Init: sem_wait - UNSUCCESSFUL (EINVAL)" );
67  status = sem_wait(&sem2);
68  fatal_directive_status( status, -1, "sem_init error return status");
69  fatal_directive_status( errno, EINVAL, "sem_wait errno EINVAL");
70
71  puts( "Init: sem_post - SUCCESSFUL" );
72  status = sem_post(&sems[1]);
73  fatal_directive_status( status, 0, "sem_post semaphore 1"); 
74
75  puts( "Init: sem_wait - SUCCESSFUL (after a sem_post)" );
76  status = sem_wait(&sems[1]);
77  fatal_directive_status( status, 0, "sem_wait semaphore 1"); 
78
79  puts( "Init: sem_trywait - SUCCESSFUL" );
80  status = sem_trywait(&sems[2]);
81  fatal_directive_status( status, 0, "sem_trywait semaphore 2"); 
82
83  puts( "Init: sem_trywait - UNSUCCESSFUL (EAGAIN)" );
84  status = sem_trywait(&sems[1]);
85  fatal_directive_status( status, -1, "sem_init error return status");
86  fatal_directive_status( errno, EAGAIN, "sem_trywait errno EAGAIN");
87
88  puts( "Init: sem_trywait - UNSUCCESSFUL (EINVAL)" );
89  status = sem_trywait(&sem2);
90  fatal_directive_status( status, -1, "sem_init error return status");
91  fatal_directive_status( errno, EINVAL, "sem_trywait errno EINVAL");
92
93  puts( "Init: sem_timedwait - SUCCESSFUL" );
94  waittime.tv_sec = 0;
95  waittime.tv_nsec = 100;
96  status = sem_timedwait(&sems[2], &waittime);
97  fatal_directive_status( status, 0, "sem_timedwait semaphore 2"); 
98
99  puts( "Init: sem_timedwait - UNSUCCESSFUL (ETIMEDOUT)" );
100  status = sem_timedwait(&sems[1], &waittime);
101  fatal_directive_status( status, -1, "sem_init error return status");
102  fatal_directive_status( errno, ETIMEDOUT, "sem_init errno ETIMEDOUT");
103
104  puts( "Init: sem_timedwait - UNSUCCESSFUL (EINVAL)" );
105  status = sem_timedwait(&sem2, &waittime);
106  fatal_directive_status( status, -1, "sem_init error return status");
107  fatal_directive_status( errno, EINVAL, "sem_init errno EINVAL");
108
109  puts( "Init: sem_post - UNSUCCESSFUL (EINVAL)" );
110  status = sem_post(&sem2);
111  fatal_directive_status( status, -1, "sem_init error return status");
112  fatal_directive_status( errno, EINVAL, "sem_post errno EINVAL");
113
114  puts( "Init: sem_destroy - SUCCESSFUL" );
115  for (i = 1; i < CONFIGURE_MAXIMUM_POSIX_SEMAPHORES; i++) {
116    status = sem_destroy(&sems[i]);
117    sprintf( failure_msg, "sem_destroy %d", i );
118    fatal_directive_status( status, 0, failure_msg ); 
119  }
120
121  /* Modes are currently unsupported */
122
123  /*
124   * Validate all sem_open return paths.
125   */
126
127  puts( "Init: sem_open - sem1 SUCCESSFUL" );
128  n_sem1 = sem_open("sem1", O_CREAT, 00777, 1);
129  assert( n_sem1 != SEM_FAILED );
130
131  puts( "Init: sem_open - UNSUCCESSFUL (EEXIST)" );
132  n_sem2 = sem_open("sem1", O_CREAT | O_EXCL, 00777, 1);
133  fatal_directive_status( 
134    (int) n_sem2, (int ) SEM_FAILED, "sem_open error return status" );
135  fatal_directive_status( errno, EEXIST,  "sem_open errno EEXIST"); 
136
137  /*
138   * Validate we can wait on a semaphore opened with sem_open.
139   */
140
141  puts( "Init: sem_wait on sem1" );
142  status = sem_wait(n_sem1);
143  fatal_directive_status( status, 0, "sem_wait opened semaphore"); 
144
145#if 0
146  puts( "Init: sem_unlink - sem1 SUCCESSFUL" );
147  status = sem_unlink( "sem1" );
148  fatal_directive_status( status, 0, "sem_unlink locked semaphore");
149
150  puts( "Init: sem_open - Reopen sem1 SUCCESSFUL with a different id" );
151  n_sem2 = sem_open( "sem1", O_CREAT | O_EXCL, 00777, 1);
152  assert( n_sem2 != SEM_FAILED );
153#endif
154
155  /*
156   * Validate we can call close on a semaphore opened with sem_open.
157   */
158
159  puts( "Init: sem_close - SUCCESSFUL" );
160  status = sem_close( n_sem1 );
161  fatal_directive_status( status, 0, "sem_close semaphore"); 
162
163  /*
164   * Notes: 
165   *  Calls to sem_close when sem_link has not been previously called shall
166   *  have no effect on the state of the semaphore.
167   *
168   *
169 
170  puts( "Init: sem_close - UNSUCCESSFUL (EINVAL)" );
171  status = sem_close(n_sem2);
172  fatal_directive_status( status, -1, "sem_init error return status");
173  fatal_directive_status( errno, EINVAL, "sem_close errno EINVAL");
174
175  puts( "Init: sem_unlink - SUCCESSFUL" );
176  status = sem_unlink("sem1");
177  fatal_directive_status( status, 0, "sem_unlink semaphore");
178
179  puts( "Init: sem_unlink - UNSUCCESSFUL (ENOENT)" );
180  status = sem_unlink("sem2");
181  fatal_directive_status( status, -1, "sem_init error return status");
182  fatal_directive_status( errno, ENOENT, "sem_unlink errno ENOENT");
183  assert( (status == -1) && (errno == ENOENT) );
184  */
185
186  /* Try adding in unlinking before closing... (can we still open?) */
187
188  puts( "*** POSIX SEMAPHORE MANAGER TEST 1 COMPLETED ***" );
189  exit(0);
190
191  return NULL; /* just so the compiler thinks we returned something */
192}
193
194
195
Note: See TracBrowser for help on using the repository browser.