source: rtems/cpukit/score/src/corerwlockobtainread.c @ e2735012

5
Last change on this file since e2735012 was e2735012, checked in by Sebastian Huber <sebastian.huber@…>, on 06/24/15 at 09:05:39

score: Introduce Thread_queue_Queue

Separate the thread queue heads and lock from the operations. This
enables the support for light weight objects which only support one
queuing discipline.

  • Property mode set to 100644
File size: 2.6 KB
Line 
1/**
2 * @file
3 *
4 * @brief Obtain RWLock for reading
5 * @ingroup ScoreRWLock
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2006.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems/score/corerwlockimpl.h>
22#include <rtems/score/threadqimpl.h>
23#include <rtems/score/statesimpl.h>
24#include <rtems/score/watchdog.h>
25
26void _CORE_RWLock_Obtain_for_reading(
27  CORE_RWLock_Control                 *the_rwlock,
28  Thread_Control                      *executing,
29  Objects_Id                           id,
30  bool                                 wait,
31  Watchdog_Interval                    timeout,
32  CORE_RWLock_API_mp_support_callout   api_rwlock_mp_support
33)
34{
35  ISR_lock_Context lock_context;
36
37  /*
38   *  If unlocked, then OK to read.
39   *  If locked for reading and no waiters, then OK to read.
40   *  If any thread is waiting, then we wait.
41   */
42
43  _Thread_queue_Acquire( &the_rwlock->Wait_queue, &lock_context );
44    switch ( the_rwlock->current_state ) {
45      case CORE_RWLOCK_UNLOCKED:
46        the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_READING;
47        the_rwlock->number_of_readers += 1;
48        _Thread_queue_Release( &the_rwlock->Wait_queue, &lock_context );
49        executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL;
50        return;
51
52      case CORE_RWLOCK_LOCKED_FOR_READING: {
53        Thread_Control *waiter;
54        waiter = _Thread_queue_First_locked( &the_rwlock->Wait_queue );
55        if ( !waiter ) {
56          the_rwlock->number_of_readers += 1;
57          _Thread_queue_Release( &the_rwlock->Wait_queue, &lock_context );
58          executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL;
59          return;
60        }
61        break;
62      }
63      case CORE_RWLOCK_LOCKED_FOR_WRITING:
64        break;
65    }
66
67    /*
68     *  If the thread is not willing to wait, then return immediately.
69     */
70
71    if ( !wait ) {
72      _Thread_queue_Release( &the_rwlock->Wait_queue, &lock_context );
73      executing->Wait.return_code = CORE_RWLOCK_UNAVAILABLE;
74      return;
75    }
76
77    /*
78     *  We need to wait to enter this critical section
79     */
80
81    executing->Wait.id          = id;
82    executing->Wait.option      = CORE_RWLOCK_THREAD_WAITING_FOR_READ;
83    executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL;
84
85    _Thread_queue_Enqueue_critical(
86       &the_rwlock->Wait_queue.Queue,
87       the_rwlock->Wait_queue.operations,
88       executing,
89       STATES_WAITING_FOR_RWLOCK,
90       timeout,
91       CORE_RWLOCK_TIMEOUT,
92       &lock_context
93    );
94
95    /* return to API level so it can dispatch and we block */
96}
Note: See TracBrowser for help on using the repository browser.