source: rtems/testsuites/psxtests/psxcond01/init.c @ 74c15fe6

4.11
Last change on this file since 74c15fe6 was 74c15fe6, checked in by Joel Sherrill <joel.sherrill@…>, on Dec 10, 2013 at 11:56:22 PM

psxtmcond01: Fix bug where mutex not locked and add EPERM error check

This test formerly had a bug in that it attempted to wait on a
condition variable with a Mutex that was not locked. This is
undefined by POSIX. But a recent change to match GNU/Linux behavior
resulted in finding a bug in the test.

  • Property mode set to 100644
File size: 2.6 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2013.
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.com/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include "tmacros.h"
15#include <pthread.h>
16#include <errno.h>
17
18/* forward declarations to avoid warnings */
19void *POSIX_Init(void *argument);
20void *BlockingThread(void *argument);
21
22pthread_cond_t  Condition;
23pthread_mutex_t Mutex1;
24pthread_mutex_t Mutex2;
25
26void *BlockingThread(
27  void *argument
28)
29{
30  int sc;
31
32  puts( "BlockingThread - pthread_cond_wait with mutex not locked - EPERM" );
33  sc = pthread_cond_wait( &Condition, &Mutex1 );
34  fatal_posix_service_status( sc, EPERM, "mutex1 not locked" );
35
36  sc = pthread_mutex_lock( &Mutex1 );
37  fatal_posix_service_status( sc, 0, "mutex1 lock" );
38 
39  puts( "BlockingThread - pthread_cond_wait on Mutex1 - OK" );
40  sc = pthread_cond_wait( &Condition, &Mutex1 );
41
42  printf(
43    "ERROR - BlockingThread returned from pthread_cond_wait! (rc=%d/%s)\n",
44    sc,
45    strerror(sc)
46  );
47  rtems_test_exit( 0 );
48
49  return NULL;
50}
51
52void *POSIX_Init(
53  void *argument
54)
55{
56  int        sc;
57  pthread_t  Thread;
58
59  puts( "\n\n*** POSIX TEST -- CONDITION VARIABLE 01 ***" );
60
61  puts( "Init - pthread_mutex_init - Mutex1 - OK" );
62  sc = pthread_mutex_init( &Mutex1, NULL );
63  fatal_posix_service_status( sc, 0, "mutex1 create ok" );
64
65  puts( "Init - pthread_mutex_init - Mutex2 - OK" );
66  sc = pthread_mutex_init( &Mutex2, NULL );
67  fatal_posix_service_status( sc, 0, "mutex2 create ok" );
68
69  puts( "Init - pthread_cond_init - Condition - OK" );
70  sc = pthread_cond_init( &Condition, NULL );
71  fatal_posix_service_status( sc, 0, "Condition create ok" );
72
73  puts( "Init -  pthread_create - OK" );
74  sc = pthread_create( &Thread, NULL, BlockingThread, NULL );
75  fatal_posix_service_status( sc, 0, "Thread create ok" );
76
77  puts( "Init - sleep to let BlockingThread run" );
78  sleep(1);
79
80  puts( "Init - pthread_cond_wait on Mutex2 - EINVAL" );
81  sc = pthread_cond_wait( &Condition, &Mutex2 );
82  fatal_posix_service_status( sc, EINVAL, "cond_wait EINVAL" );
83
84  puts( "*** END OF POSIX TEST CONDITION VARIABLE 01 ***" );
85  rtems_test_exit( 0 );
86
87  return NULL; /* just so the compiler thinks we returned something */
88}
89
90#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
91#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
92
93#define CONFIGURE_MAXIMUM_POSIX_THREADS 2
94#define CONFIGURE_MAXIMUM_POSIX_MUTEXES 2
95#define CONFIGURE_MAXIMUM_POSIX_CONDITION_VARIABLES 1
96
97#define CONFIGURE_POSIX_INIT_THREAD_TABLE
98
99#define CONFIGURE_INIT
100#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.