source: rtems/testsuites/psxtmtests/psxtmrwlock07/init.c @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 3.7 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.org/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <coverhd.h>
15#include <tmacros.h>
16#include <timesys.h>
17#include "test_support.h"
18#include <pthread.h>
19#include <sched.h>
20#include <rtems/timerdrv.h>
21
22/* forward declarations to avoid warnings */
23void *POSIX_Init(void *argument);
24void *Middle(void *argument);
25void *Low(void *argument);
26
27pthread_rwlock_t     rwlock;
28
29void *Low(
30  void *argument
31)
32{
33  int      status;
34  benchmark_timer_t end_time;
35
36  /* write locking */
37    status = pthread_rwlock_wrlock(&rwlock);
38  end_time = benchmark_timer_read();
39
40  rtems_test_assert( status == 0 );
41
42  put_time(
43    "pthread_rwlock_unlock: thread waiting preempt",
44    end_time,
45    OPERATION_COUNT,
46    0,
47    0
48  );
49
50  puts( "*** END OF POSIX TIME TEST PSXTMRWLOCK 07 ***" );
51  rtems_test_exit( 0 );
52  return NULL;
53}
54
55void *Middle(
56  void *argument
57)
58{
59  int status;
60
61  /* write locking */
62  status =  pthread_rwlock_wrlock(&rwlock);
63  rtems_test_assert( status == 0 );
64
65    /* thread switch occurs */
66
67    status = pthread_rwlock_unlock(&rwlock); /*  unlock the rwlock */
68  rtems_test_assert( status == 0 );
69    /* thread switch occurs */
70
71  /* should never return */
72  rtems_test_assert( FALSE );
73  return NULL;
74}
75
76void *POSIX_Init(
77  void *argument
78)
79{
80  int                 i;
81  int                 status;
82  pthread_t           threadId;
83  pthread_attr_t      attr;
84  struct sched_param  param;
85  pthread_rwlockattr_t rw_attr;
86
87  puts( "\n\n*** POSIX TIME TEST PSXTMRWLOCK 07 ***" );
88
89  /*
90   * Deliberately create the lock BEFORE the threads.  This way the
91   * threads should preempt this thread and block as they are created.
92   */
93  status = pthread_rwlockattr_init( &rw_attr );
94  rtems_test_assert( status == 0 );
95  status = pthread_rwlock_init( &rwlock, &rw_attr );
96  rtems_test_assert( status == 0 );
97
98  /*
99   * Obtain the lock so the threads will block.
100   */
101  status = pthread_rwlock_wrlock(&rwlock);
102  rtems_test_assert( status == 0 );
103
104  /*
105   * Now lower our priority
106   */
107  status = pthread_attr_init( &attr );
108  rtems_test_assert( status == 0 );
109
110  status = pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
111  rtems_test_assert( status == 0 );
112
113  status = pthread_attr_setschedpolicy( &attr, SCHED_RR );
114  rtems_test_assert( status == 0 );
115
116  param.sched_priority = 2;
117  status = pthread_attr_setschedparam( &attr, &param );
118  rtems_test_assert( status == 0 );
119
120  /*
121   * And create rest of threads as more important than we are.  They
122   * will preempt us as they are created and block.
123   */
124  for ( i=0 ; i < OPERATION_COUNT ; i++ ) {
125
126    param.sched_priority = 3 + i;
127    status = pthread_attr_setschedparam( &attr, &param );
128    rtems_test_assert( status == 0 );
129
130    status = pthread_create(
131      &threadId,
132      &attr,
133      (i == OPERATION_COUNT - 1) ? Low : Middle,
134      NULL
135    );
136    rtems_test_assert( status == 0 );
137  }
138
139  /*
140   * Now start the timer which will be stopped in Low
141   * Release the lock.  Threads unblock and preempt.
142   */
143  benchmark_timer_initialize();
144
145    status = pthread_rwlock_unlock(&rwlock);
146      /* thread switch occurs */
147
148  /* should never return */
149  rtems_test_assert( FALSE );
150  return NULL;
151}
152
153/* configuration information */
154
155#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
156#define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER
157
158#define CONFIGURE_MAXIMUM_POSIX_THREADS     OPERATION_COUNT + 2
159#define CONFIGURE_MAXIMUM_POSIX_RWLOCKS     1
160#define CONFIGURE_POSIX_INIT_THREAD_TABLE
161
162#define CONFIGURE_INIT
163
164#include <rtems/confdefs.h>
165/* end of file */
Note: See TracBrowser for help on using the repository browser.