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.com/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 */ |
---|
23 | void *POSIX_Init(void *argument); |
---|
24 | void *Middle(void *argument); |
---|
25 | void *Low(void *argument); |
---|
26 | |
---|
27 | pthread_mutex_t MutexId; |
---|
28 | |
---|
29 | void *Low( |
---|
30 | void *argument |
---|
31 | ) |
---|
32 | { |
---|
33 | int status; |
---|
34 | benchmark_timer_t end_time; |
---|
35 | |
---|
36 | status = pthread_mutex_lock( &MutexId ); |
---|
37 | end_time = benchmark_timer_read(); |
---|
38 | |
---|
39 | rtems_test_assert( status == 0 ); |
---|
40 | |
---|
41 | put_time( |
---|
42 | "pthread_mutex_unlock - unblocking, preempt", |
---|
43 | end_time, |
---|
44 | OPERATION_COUNT, |
---|
45 | 0, |
---|
46 | 0 |
---|
47 | ); |
---|
48 | |
---|
49 | puts( "*** END OF POSIX TIME TEST PSXTMMUTEX07 ***" ); |
---|
50 | rtems_test_exit( 0 ); |
---|
51 | return NULL; |
---|
52 | } |
---|
53 | |
---|
54 | void *Middle( |
---|
55 | void *argument |
---|
56 | ) |
---|
57 | { |
---|
58 | int status; |
---|
59 | |
---|
60 | status = pthread_mutex_lock( &MutexId ); |
---|
61 | rtems_test_assert( status == 0 ); |
---|
62 | |
---|
63 | /* thread switch occurs */ |
---|
64 | |
---|
65 | status = pthread_mutex_unlock( &MutexId ); |
---|
66 | rtems_test_assert( status == 0 ); |
---|
67 | |
---|
68 | /* thread switch occurs */ |
---|
69 | |
---|
70 | return NULL; |
---|
71 | } |
---|
72 | |
---|
73 | void *POSIX_Init( |
---|
74 | void *argument |
---|
75 | ) |
---|
76 | { |
---|
77 | int i; |
---|
78 | int status; |
---|
79 | pthread_t threadId; |
---|
80 | pthread_attr_t attr; |
---|
81 | struct sched_param param; |
---|
82 | |
---|
83 | puts( "\n\n*** POSIX TIME TEST PSXTMMUTEX02 ***" ); |
---|
84 | |
---|
85 | /* |
---|
86 | * Deliberately create the mutex BEFORE the threads. This way the |
---|
87 | * threads should preempt this thread and block as they are created. |
---|
88 | */ |
---|
89 | status = pthread_mutex_init( &MutexId, NULL ); |
---|
90 | rtems_test_assert( status == 0 ); |
---|
91 | |
---|
92 | /* |
---|
93 | * Lock the mutex so the threads will block. |
---|
94 | */ |
---|
95 | status = pthread_mutex_lock( &MutexId ); |
---|
96 | rtems_test_assert( status == 0 ); |
---|
97 | |
---|
98 | /* |
---|
99 | * Now lower our priority |
---|
100 | */ |
---|
101 | status = pthread_attr_init( &attr ); |
---|
102 | rtems_test_assert( status == 0 ); |
---|
103 | |
---|
104 | status = pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED ); |
---|
105 | rtems_test_assert( status == 0 ); |
---|
106 | |
---|
107 | status = pthread_attr_setschedpolicy( &attr, SCHED_RR ); |
---|
108 | rtems_test_assert( status == 0 ); |
---|
109 | |
---|
110 | param.sched_priority = 2; |
---|
111 | status = pthread_attr_setschedparam( &attr, ¶m ); |
---|
112 | rtems_test_assert( status == 0 ); |
---|
113 | |
---|
114 | /* |
---|
115 | * And create rest of threads as more important than we are. They |
---|
116 | * will preempt us as they are created and block. |
---|
117 | */ |
---|
118 | for ( i=0 ; i < OPERATION_COUNT ; i++ ) { |
---|
119 | |
---|
120 | param.sched_priority = 3 + i; |
---|
121 | status = pthread_attr_setschedparam( &attr, ¶m ); |
---|
122 | rtems_test_assert( status == 0 ); |
---|
123 | |
---|
124 | status = pthread_create( |
---|
125 | &threadId, |
---|
126 | &attr, |
---|
127 | (i == OPERATION_COUNT - 1) ? Low : Middle, |
---|
128 | NULL |
---|
129 | ); |
---|
130 | rtems_test_assert( status == 0 ); |
---|
131 | } |
---|
132 | |
---|
133 | /* |
---|
134 | * Now start the timer which will be stopped in Low |
---|
135 | */ |
---|
136 | benchmark_timer_initialize(); |
---|
137 | |
---|
138 | status = pthread_mutex_unlock( &MutexId ); |
---|
139 | rtems_test_assert( status == 0 ); |
---|
140 | |
---|
141 | /* thread switch occurs */ |
---|
142 | return NULL; |
---|
143 | } |
---|
144 | |
---|
145 | /* configuration information */ |
---|
146 | |
---|
147 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
148 | #define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER |
---|
149 | |
---|
150 | #define CONFIGURE_MAXIMUM_POSIX_THREADS OPERATION_COUNT + 2 |
---|
151 | #define CONFIGURE_MAXIMUM_POSIX_MUTEXES 1 |
---|
152 | #define CONFIGURE_POSIX_INIT_THREAD_TABLE |
---|
153 | |
---|
154 | #define CONFIGURE_INIT |
---|
155 | |
---|
156 | #include <rtems/confdefs.h> |
---|
157 | /* end of file */ |
---|