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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [compat/] [posix/] [current/] [src/] [mutex.cxx] - Blame information for rev 810

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

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      pthread.cxx
4
//
5
//      POSIX pthreads implementation
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):           nickg
43
// Contributors:        nickg, jlarmour, Wade Jensen
44
// Date:                2000-03-27
45
// Purpose:             POSIX pthread implementation
46
// Description:         This file contains the implementation of the POSIX pthread
47
//                      functions.
48
//              
49
//              
50
//
51
//####DESCRIPTIONEND####
52
//
53
//==========================================================================
54
 
55
#include <pkgconf/posix.h>
56
 
57
#include <cyg/infra/cyg_trac.h>        // tracing macros
58
#include <cyg/infra/cyg_ass.h>         // assertion macros
59
 
60
#include "pprivate.h"                  // POSIX private header
61
 
62
#include <cyg/kernel/thread.hxx>       // thread definitions
63
#include <cyg/kernel/mutex.hxx>        // mutex definitions
64
#include <cyg/kernel/clock.hxx>        // clock definitions
65
#include <cyg/kernel/sched.hxx>        // scheduler primitives
66
#include <pthread.h>
67
 
68
#include <cyg/kernel/thread.inl>       // thread inlines
69
#include <cyg/kernel/sched.inl>        // scheduler inlines
70
 
71
//-----------------------------------------------------------------------------
72
// new operator to allow us to construct mutex objects
73
 
74
inline void *operator new(size_t size,  cyg_uint8 *ptr) { return (void *)ptr; };
75
 
76
//=============================================================================
77
// Mutexes
78
 
79
//-----------------------------------------------------------------------------
80
// Mutex attributes manipulation functions
81
 
82
//-----------------------------------------------------------------------------
83
// Initialize attribute object
84
 
85
externC int pthread_mutexattr_init ( pthread_mutexattr_t *attr)
86
{
87
    PTHREAD_ENTRY();
88
 
89
    PTHREAD_CHECK(attr);
90
 
91
    attr->protocol      = PTHREAD_PRIO_NONE;
92
#ifdef _POSIX_THREAD_PRIO_PROTECT    
93
    attr->prioceiling   = 0;
94
#endif
95
 
96
    PTHREAD_RETURN(0);
97
}
98
 
99
//-----------------------------------------------------------------------------
100
// Destroy attribute object
101
 
102
externC int pthread_mutexattr_destroy ( pthread_mutexattr_t *attr)
103
{
104
    PTHREAD_ENTRY();
105
 
106
    PTHREAD_CHECK(attr);
107
 
108
    // Nothing to do here...
109
 
110
    PTHREAD_RETURN(0);
111
}
112
 
113
//-----------------------------------------------------------------------------
114
// Optional functions depending on priority inversion protection options.
115
 
116
#if defined(_POSIX_THREAD_PRIO_INHERIT) || defined(_POSIX_THREAD_PRIO_PROTECT)
117
 
118
// Set priority inversion protection protocol
119
externC int pthread_mutexattr_setprotocol ( pthread_mutexattr_t *attr,
120
                                            int protocol)
121
{
122
    PTHREAD_ENTRY();
123
 
124
    PTHREAD_CHECK(attr);
125
 
126
    switch( protocol )
127
    {
128
    case PTHREAD_PRIO_NONE:
129
#if defined(_POSIX_THREAD_PRIO_INHERIT)        
130
    case PTHREAD_PRIO_INHERIT:
131
#endif
132
#if defined(_POSIX_THREAD_PRIO_PROTECT)
133
    case PTHREAD_PRIO_PROTECT:
134
#endif        
135
        attr->protocol = protocol;
136
        PTHREAD_RETURN(0);
137
 
138
    default:
139
        PTHREAD_RETURN(EINVAL);
140
    }
141
 
142
    PTHREAD_RETURN(0);
143
}
144
 
145
// Get priority inversion protection protocol
146
externC int pthread_mutexattr_getprotocol ( pthread_mutexattr_t *attr,
147
                                            int *protocol)
148
{
149
    PTHREAD_ENTRY();
150
 
151
    PTHREAD_CHECK(attr);
152
 
153
    if( protocol != NULL )
154
        *protocol = attr->protocol;
155
 
156
    PTHREAD_RETURN(0);
157
}
158
 
159
#if defined(_POSIX_THREAD_PRIO_PROTECT)
160
 
161
// Set priority for priority ceiling protocol
162
externC int pthread_mutexattr_setprioceiling ( pthread_mutexattr_t *attr,
163
                                               int prioceiling)
164
{
165
    PTHREAD_ENTRY();
166
 
167
    PTHREAD_CHECK(attr);
168
 
169
 
170
    attr->prioceiling = prioceiling;
171
 
172
    PTHREAD_RETURN(0);
173
}
174
 
175
// Get priority for priority ceiling protocol
176
externC int pthread_mutexattr_getprioceiling ( pthread_mutexattr_t *attr,
177
                                               int *prioceiling)
178
{
179
    PTHREAD_ENTRY();
180
 
181
    PTHREAD_CHECK(attr);
182
 
183
    if( prioceiling != NULL )
184
        *prioceiling = attr->prioceiling;
185
 
186
    PTHREAD_RETURN(0);
187
}
188
 
189
// Set priority ceiling of given mutex, returning old ceiling.
190
externC int pthread_mutex_setprioceiling( pthread_mutex_t *mutex,
191
                                          int prioceiling,
192
                                          int *old_ceiling)
193
{
194
    PTHREAD_ENTRY();
195
 
196
    PTHREAD_CHECK(mutex);
197
 
198
    pthread_mutex_lock( mutex );
199
 
200
    Cyg_Mutex *mx = (Cyg_Mutex *)mutex;
201
 
202
    if( old_ceiling != NULL )
203
        *old_ceiling = mx->get_ceiling();
204
 
205
    mx->set_ceiling( prioceiling );
206
 
207
    pthread_mutex_unlock( mutex );
208
 
209
    PTHREAD_RETURN(0);
210
}
211
 
212
// Get priority ceiling of given mutex
213
externC int pthread_mutex_getprioceiling( pthread_mutex_t *mutex,
214
                                          int *prioceiling)
215
{
216
    PTHREAD_ENTRY();
217
 
218
    PTHREAD_CHECK(mutex);
219
 
220
    Cyg_Mutex *mx = (Cyg_Mutex *)mutex;
221
 
222
    if( prioceiling != NULL )
223
        *prioceiling = mx->get_ceiling();
224
 
225
    PTHREAD_RETURN(0);
226
}
227
 
228
#endif // defined(_POSIX_THREAD_PRIO_PROTECT)
229
 
230
#endif // defined(_POSIX_THREAD_PRIO_INHERIT) || defined(_POSIX_THREAD_PRIO_PROTECT)
231
 
232
//-----------------------------------------------------------------------------
233
// Mutex functions
234
 
235
//-----------------------------------------------------------------------------
236
// Initialize mutex. If mutex_attr is NULL, use default attributes.
237
 
238
externC int pthread_mutex_init (pthread_mutex_t *mutex,
239
                                const pthread_mutexattr_t *mutex_attr)
240
{
241
    PTHREAD_ENTRY();
242
 
243
    PTHREAD_CHECK( mutex );
244
 
245
    pthread_mutexattr_t use_attr;
246
 
247
    // Set up the attributes we are going to use
248
    if( mutex_attr == NULL )
249
        pthread_mutexattr_init( &use_attr );
250
    else use_attr = *mutex_attr;
251
 
252
    // Now translate the POSIX protocol identifier into the eCos one.
253
    Cyg_Mutex::cyg_protcol protocol;
254
 
255
    switch( use_attr.protocol )
256
    {
257
#if defined(_POSIX_THREAD_PRIO_PROTECT)
258
    case PTHREAD_PRIO_PROTECT:
259
        protocol = Cyg_Mutex::CEILING;
260
        break;
261
#endif
262
#if defined(_POSIX_THREAD_PRIO_INHERIT)
263
    case PTHREAD_PRIO_INHERIT:
264
        protocol = Cyg_Mutex::INHERIT;
265
        break;
266
#endif        
267
    case PTHREAD_PRIO_NONE:
268
        protocol = Cyg_Mutex::NONE;
269
        break;
270
 
271
    default:
272
        PTHREAD_RETURN(EINVAL);
273
    }
274
 
275
    Cyg_Mutex *mx = new((cyg_uint8 *)mutex) Cyg_Mutex(  protocol );
276
 
277
    mx = mx; // silence compiler warning
278
#if defined(_POSIX_THREAD_PRIO_PROTECT)
279
    if ( protocol == Cyg_Mutex::CEILING )
280
        mx->set_ceiling( use_attr.prioceiling );
281
#endif
282
 
283
    PTHREAD_RETURN(0);
284
}
285
 
286
//-----------------------------------------------------------------------------
287
// Destroy mutex.
288
 
289
externC int pthread_mutex_destroy (pthread_mutex_t *mutex)
290
{
291
    PTHREAD_ENTRY();
292
 
293
    int err = ENOERR;
294
 
295
    PTHREAD_CHECK( mutex );
296
 
297
    Cyg_Mutex *mx = (Cyg_Mutex *)mutex;
298
 
299
    if( mx->get_owner() != NULL )
300
        err = EBUSY;
301
    else mx->~Cyg_Mutex();
302
 
303
    PTHREAD_RETURN(err);
304
}
305
 
306
//-----------------------------------------------------------------------------
307
// Lock mutex, waiting for it if necessary.
308
 
309
externC int pthread_mutex_lock (pthread_mutex_t *mutex)
310
{
311
    PTHREAD_ENTRY();
312
 
313
    PTHREAD_CHECK( mutex );
314
 
315
    Cyg_Mutex *mx = (Cyg_Mutex *)mutex;
316
 
317
    if( mx->get_owner() == Cyg_Thread::self() )
318
        PTHREAD_RETURN(EDEADLK);
319
 
320
    // Loop here until we acquire the mutex. Even if we are kicked out
321
    // of the wait by a signal or release we must retry.
322
    while( !mx->lock() )
323
        continue;
324
 
325
    PTHREAD_RETURN(0);
326
}
327
 
328
//-----------------------------------------------------------------------------
329
// Try to lock mutex.
330
 
331
externC int pthread_mutex_trylock (pthread_mutex_t *mutex)
332
{
333
    PTHREAD_ENTRY();
334
 
335
    PTHREAD_CHECK( mutex );
336
 
337
    Cyg_Mutex *mx = (Cyg_Mutex *)mutex;
338
 
339
    if( mx->get_owner() == Cyg_Thread::self() )
340
        PTHREAD_RETURN(EDEADLK);
341
 
342
    if( mx->trylock() )
343
        PTHREAD_RETURN(0);
344
 
345
    PTHREAD_RETURN(EBUSY);
346
}
347
 
348
 
349
//-----------------------------------------------------------------------------
350
// Unlock mutex.
351
 
352
externC int pthread_mutex_unlock (pthread_mutex_t *mutex)
353
{
354
    PTHREAD_ENTRY();
355
 
356
    PTHREAD_CHECK( mutex );
357
 
358
    Cyg_Mutex *mx = (Cyg_Mutex *)mutex;
359
 
360
    mx->unlock();
361
 
362
    PTHREAD_RETURN(0);
363
}
364
 
365
 
366
//=============================================================================
367
// Condition Variables
368
 
369
//-----------------------------------------------------------------------------
370
// Attribute manipulation functions
371
// We do not actually support any attributes at present, so these do nothing.
372
 
373
//-----------------------------------------------------------------------------
374
// Initialize condition variable attributes
375
 
376
externC int pthread_condattr_init (pthread_condattr_t *attr)
377
{
378
    PTHREAD_ENTRY();
379
 
380
    PTHREAD_CHECK(attr);
381
 
382
    // There are no condition variable attributes at present
383
 
384
    PTHREAD_RETURN(0);
385
}
386
 
387
//-----------------------------------------------------------------------------
388
// Destroy condition variable attributes
389
 
390
externC int pthread_condattr_destroy (pthread_condattr_t *attr)
391
{
392
    PTHREAD_ENTRY();
393
 
394
    PTHREAD_CHECK(attr);
395
 
396
    // nothing to do here...
397
 
398
    PTHREAD_RETURN(0);
399
}
400
 
401
//-----------------------------------------------------------------------------
402
// Condition variable functions
403
 
404
//-----------------------------------------------------------------------------
405
// Initialize condition variable.
406
 
407
externC int pthread_cond_init (pthread_cond_t *cond,
408
                               const pthread_condattr_t *attr)
409
{
410
    PTHREAD_ENTRY();
411
 
412
    PTHREAD_CHECK( cond );
413
 
414
    Cyg_Condition_Variable *cv =
415
        new((cyg_uint8 *)cond) Cyg_Condition_Variable();
416
 
417
    cv = cv;
418
 
419
    PTHREAD_RETURN(0);
420
}
421
 
422
//-----------------------------------------------------------------------------
423
// Destroy condition variable.
424
 
425
externC int pthread_cond_destroy (pthread_cond_t *cond)
426
{
427
    PTHREAD_ENTRY();
428
 
429
    PTHREAD_CHECK( cond );
430
 
431
    ((Cyg_Condition_Variable *)cond)->~Cyg_Condition_Variable();
432
 
433
    PTHREAD_RETURN(0);
434
}
435
 
436
//-----------------------------------------------------------------------------
437
// Wake up one thread waiting for condition variable
438
 
439
externC int pthread_cond_signal (pthread_cond_t *cond)
440
{
441
    PTHREAD_ENTRY();
442
 
443
    PTHREAD_CHECK( cond );
444
 
445
    ((Cyg_Condition_Variable *)cond)->signal();
446
 
447
    PTHREAD_RETURN(0);
448
}
449
 
450
//-----------------------------------------------------------------------------
451
// Wake up all threads waiting for condition variable
452
 
453
externC int pthread_cond_broadcast (pthread_cond_t *cond)
454
{
455
    PTHREAD_ENTRY();
456
 
457
    PTHREAD_CHECK( cond );
458
 
459
    ((Cyg_Condition_Variable *)cond)->broadcast();
460
 
461
    PTHREAD_RETURN(0);
462
}
463
 
464
//-----------------------------------------------------------------------------
465
// Block on condition variable until signalled. The mutex is
466
// assumed to be locked before this call, will be unlocked
467
// during the wait, and will be re-locked on wakeup.
468
 
469
externC int pthread_cond_wait (pthread_cond_t *cond,
470
                               pthread_mutex_t *mutex)
471
{
472
    PTHREAD_ENTRY();
473
 
474
    // check for cancellation first.
475
    PTHREAD_TESTCANCEL();
476
 
477
    PTHREAD_CHECK( cond );
478
    PTHREAD_CHECK( mutex );
479
 
480
    ((Cyg_Condition_Variable *)cond)->wait( *(Cyg_Mutex *)mutex );
481
 
482
    // check if we were woken because we were being cancelled
483
    PTHREAD_TESTCANCEL();
484
 
485
    PTHREAD_RETURN(0);
486
}
487
 
488
//-----------------------------------------------------------------------------
489
// Block on condition variable until signalled, or the timeout expires.
490
 
491
externC int pthread_cond_timedwait (pthread_cond_t *cond,
492
                                    pthread_mutex_t *mutex,
493
                                    const struct timespec *abstime)
494
{
495
    PTHREAD_ENTRY();
496
 
497
    // check for cancellation first.
498
    PTHREAD_TESTCANCEL();
499
 
500
    PTHREAD_CHECK( cond );
501
    PTHREAD_CHECK( mutex );
502
    PTHREAD_CHECK( abstime );
503
 
504
    // Only initialize the converters once or they will consume a huge
505
    // amount or runtime.
506
 
507
    static struct Cyg_Clock::converter ns_converter;
508
    static struct Cyg_Clock::converter sec_converter;
509
    static volatile cyg_atomic conv_init;
510
    if (!conv_init)
511
    {
512
 
513
        // Try to avoid unnecessarily locking the scheduler when we are not
514
        // initializing the converters.  Check the conv_init flag again to
515
        // avoid race conditions.
516
 
517
        struct Cyg_Clock::converter temp_ns_converter, temp_sec_converter;
518
 
519
        Cyg_Clock::real_time_clock
520
            ->get_other_to_clock_converter( 1, &temp_ns_converter );
521
        Cyg_Clock::real_time_clock
522
            ->get_other_to_clock_converter( 1000000000, &temp_sec_converter );
523
 
524
        Cyg_Scheduler::lock();
525
        if (!conv_init)
526
        {
527
            ns_converter = temp_ns_converter;
528
            sec_converter = temp_sec_converter;
529
            conv_init=1;
530
        }
531
        Cyg_Scheduler::unlock();
532
    }
533
 
534
    cyg_tick_count ticks;
535
    ticks = Cyg_Clock::convert( abstime->tv_sec, &sec_converter );
536
    ticks += Cyg_Clock::convert( abstime->tv_nsec, &ns_converter );
537
 
538
    ((Cyg_Condition_Variable *)cond)->wait( *(Cyg_Mutex *)mutex, ticks );
539
 
540
    // check if we were woken because we were being cancelled
541
    PTHREAD_TESTCANCEL();
542
 
543
    if ( Cyg_Thread::self()->get_wake_reason() == Cyg_Thread::TIMEOUT )
544
        PTHREAD_RETURN(ETIMEDOUT);
545
    else
546
        PTHREAD_RETURN(0);
547
}
548
 
549
// -------------------------------------------------------------------------
550
// EOF mutex.cxx

powered by: WebSVN 2.1.0

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