OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [kernel/] [current/] [tests/] [klock.c] - Blame information for rev 842

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
/*=================================================================
2
//
3
//        klock.c
4
//
5
//        Kernel lock test
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under
14
// the terms of the GNU General Public License as published by the Free
15
// Software Foundation; either version 2 or (at your option) any later
16
// version.
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21
// for more details.
22
//
23
// You should have received a copy of the GNU General Public License
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26
//
27
// As a special exception, if other files instantiate templates or use
28
// macros or inline functions from this file, or you compile this file
29
// and link it with other works to produce a work based on this file,
30
// this file does not by itself cause the resulting work to be covered by
31
// the GNU General Public License. However the source code for this file
32
// must still be made available in accordance with section (3) of the GNU
33
// General Public License v2.
34
//
35
// This exception does not invalidate any other reasons why a work based
36
// on this file might be covered by the GNU General Public License.
37
// -------------------------------------------
38
// ####ECOSGPLCOPYRIGHTEND####
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):     dsm
43
// Contributors:    dsm
44
// Date:          1998-03-18
45
// Description:   Tests some basic thread functions.
46
//####DESCRIPTIONEND####
47
*/
48
//==========================================================================
49
 
50
#include <cyg/kernel/kapi.h>
51
 
52
#include <cyg/infra/testcase.h>
53
 
54
//==========================================================================
55
 
56
#ifdef CYGFUN_KERNEL_API_C
57
 
58
#if (CYGINT_KERNEL_SCHEDULER_UNIQUE_PRIORITIES == 0)
59
 
60
//==========================================================================
61
 
62
#include "testaux.h"
63
 
64
#include <cyg/hal/hal_arch.h>           // for CYGNUM_HAL_STACK_SIZE_TYPICAL
65
 
66
//==========================================================================
67
 
68
#ifdef CYGNUM_HAL_STACK_SIZE_TYPICAL
69
#define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
70
#else
71
#define STACKSIZE 2000
72
#endif
73
 
74
//==========================================================================
75
 
76
static char stack[2][STACKSIZE];
77
 
78
static cyg_thread thread[2];
79
 
80
static cyg_handle_t pt0,pt1;
81
 
82
static cyg_mutex_t mx;
83
static cyg_cond_t cv;
84
static cyg_sem_t sem;
85
static cyg_flag_t fl;
86
static cyg_mbox mbox;
87
static cyg_handle_t mbh;
88
 
89
volatile static int thread0_state = 0;
90
volatile static int thread1_state = 0;
91
 
92
//==========================================================================
93
 
94
static void entry0( cyg_addrword_t data )
95
{
96
    CHECK( 222 == (int)data );
97
 
98
    // Do everything with the scheduler locked.
99
    cyg_scheduler_lock();
100
 
101
    // --------------------------------------------------
102
    // Mutex test
103
 
104
    cyg_mutex_lock( &mx );
105
    thread0_state = 1;
106
 
107
    // Get thread 2 running.
108
    cyg_thread_resume(pt1);
109
    thread0_state = 2;
110
 
111
#ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
112
    cyg_cond_wait( &cv );
113
    thread0_state = 3;
114
 
115
    while( thread1_state < 2 ) cyg_thread_yield();
116
 
117
    cyg_cond_broadcast( &cv );
118
    thread0_state = 4;
119
#endif
120
 
121
    cyg_mutex_unlock( &mx );
122
    thread0_state = 5;
123
 
124
 
125
    // --------------------------------------------------
126
    // Semaphore test
127
 
128
    cyg_semaphore_wait( &sem );
129
    thread0_state = 6;
130
 
131
    cyg_semaphore_post( &sem );
132
    thread0_state = 7;
133
 
134
    while( thread1_state < 7 ) cyg_thread_yield();
135
 
136
    // --------------------------------------------------
137
    // Flags test
138
 
139
    cyg_flag_wait( &fl, 1, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR);
140
    thread0_state = 8;
141
 
142
    cyg_flag_setbits( &fl, 2 );
143
    thread0_state = 9;
144
 
145
    // --------------------------------------------------
146
    // Message box test
147
 
148
#ifdef  CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
149
    {
150
      void *mbret;
151
 
152
      mbret = cyg_mbox_get( mbh );
153
      CYG_TEST_CHECK( mbret == (void *)0xAAAAAAAA , "bad result from cyg_mbox_timed_get()");
154
      thread0_state = 10;
155
 
156
      while( thread1_state < 10 ) cyg_thread_yield();
157
 
158
      cyg_mbox_put( mbh, (void *)0xBBBBBBBB );
159
      thread0_state = 11;
160
    }
161
#endif
162
    // --------------------------------------------------    
163
 
164
    thread0_state = 999;
165
 
166
    cyg_thread_yield();
167
    cyg_thread_yield();
168
    cyg_thread_yield();
169
 
170
    CYG_TEST_CHECK( thread0_state == 999, "thread 0 not in exit state");
171
    CYG_TEST_CHECK( thread1_state == 999, "thread 1 not in exit state");
172
    CYG_TEST_PASS_FINISH("Kernel lock test OK");
173
}
174
 
175
//==========================================================================
176
 
177
static void entry1( cyg_addrword_t data )
178
{
179
    cyg_bool res;
180
 
181
    CHECK( 333 == (int)data );
182
 
183
    // Do everything with the scheduler locked.
184
    cyg_scheduler_lock();
185
 
186
    // --------------------------------------------------
187
    // Mutex test
188
#ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
189
    cyg_mutex_lock( &mx );
190
    thread1_state = 1;
191
 
192
    while( thread0_state < 2 ) cyg_thread_yield();
193
 
194
    cyg_cond_signal( &cv );
195
    thread1_state = 2;
196
 
197
    res = cyg_cond_timed_wait( &cv, cyg_current_time()+10 );
198
    CYG_TEST_CHECK( res , "FALSE result from cyg_cond_timed_wait()" );
199
    thread1_state = 3;
200
 
201
    cyg_mutex_unlock( &mx );
202
    thread1_state = 4;
203
#endif
204
 
205
    // --------------------------------------------------
206
    // Semaphore test
207
 
208
    while( thread0_state < 5 ) cyg_thread_yield();
209
 
210
    cyg_semaphore_post( &sem );
211
    thread1_state = 5;
212
 
213
    while( thread0_state < 6 ) cyg_thread_yield();
214
    thread1_state = 6;
215
 
216
#ifdef CYGFUN_KERNEL_THREADS_TIMER
217
    res = cyg_semaphore_timed_wait( &sem, cyg_current_time()+10 );
218
#else
219
    res = cyg_semaphore_wait( &sem );
220
#endif
221
    CYG_TEST_CHECK( res , "FALSE result from cyg_semaphore[_timed]_wait()" );
222
    thread1_state = 7;
223
 
224
    // --------------------------------------------------
225
    // Flags test
226
 
227
    cyg_flag_setbits( &fl, 1 );
228
    thread1_state = 8;
229
 
230
#ifdef CYGFUN_KERNEL_THREADS_TIMER
231
    cyg_flag_timed_wait( &fl, 2, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR,
232
                         cyg_current_time()+10 );
233
#else
234
    cyg_flag_wait( &fl, 2, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR );
235
#endif
236
    thread1_state = 9;
237
 
238
    // --------------------------------------------------
239
    // Message box test
240
#ifdef  CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
241
    {
242
      void *mbret;
243
#ifdef CYGFUN_KERNEL_THREADS_TIMER
244
      cyg_mbox_timed_put( mbh, (void *)0xAAAAAAAA, cyg_current_time()+10 );
245
#else
246
      cyg_mbox_put( mbh, (void *)0xAAAAAAAA );
247
#endif
248
      thread1_state = 10;
249
 
250
      while( thread0_state < 10 ) cyg_thread_yield();
251
 
252
#ifdef CYGFUN_KERNEL_THREADS_TIMER
253
      mbret = cyg_mbox_timed_get( mbh, cyg_current_time()+10);
254
#else
255
      mbret = cyg_mbox_get( mbh );
256
#endif
257
      thread1_state = 11;
258
      CYG_TEST_CHECK( mbret == (void *)0xBBBBBBBB , "bad result from cyg_mbox[_timed]_get()");
259
      thread1_state = 12;
260
    }
261
#endif    
262
    // --------------------------------------------------
263
 
264
    thread1_state = 999;
265
    cyg_thread_exit();
266
}
267
 
268
//==========================================================================
269
 
270
void kthread1_main( void )
271
{
272
    CYG_TEST_INIT();
273
 
274
    cyg_thread_create(4, entry0, (cyg_addrword_t)222, "kthread1-0",
275
        (void *)stack[0], STACKSIZE, &pt0, &thread[0] );
276
    cyg_thread_create(4, entry1, (cyg_addrword_t)333, "kthread1-1",
277
        (void *)stack[1], STACKSIZE, &pt1, &thread[1] );
278
 
279
    // Init all the objects
280
 
281
    cyg_mutex_init( &mx );
282
    cyg_cond_init( &cv, &mx );
283
    cyg_semaphore_init( &sem, 0 );
284
    cyg_flag_init( &fl );
285
    cyg_mbox_create( &mbh, &mbox );
286
 
287
    cyg_thread_resume(pt0);
288
 
289
    cyg_scheduler_start();
290
 
291
    CYG_TEST_FAIL_FINISH("Not reached");
292
}
293
 
294
//==========================================================================
295
 
296
externC void
297
cyg_start( void )
298
{
299
    kthread1_main();
300
}
301
 
302
//==========================================================================
303
 
304
#else /* CYGINT_KERNEL_SCHEDULER_UNIQUE_PRIORITIES == 0 */
305
# define NA_MSG "Schedule has unique priorities"
306
#endif
307
 
308
#else /* def CYGFUN_KERNEL_API_C */
309
# define NA_MSG "Kernel C API layer disabled"
310
#endif
311
 
312
#ifdef NA_MSG
313
externC void
314
cyg_start( void )
315
{
316
    CYG_TEST_INIT();
317
    CYG_TEST_NA(NA_MSG);
318
}
319
#endif
320
 
321
//==========================================================================
322
/* EOF klock.c */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.