source: rtems/cpukit/score/src/corerwlockobtainwrite.c @ 25f5730f

4.11
Last change on this file since 25f5730f was c499856, checked in by Chris Johns <chrisj@…>, on Mar 20, 2014 at 9:10:47 PM

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

  • Property mode set to 100644
File size: 2.2 KB
Line 
1/**
2 * @file
3 *
4 * @brief RWLock Obtain for Writing
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/watchdog.h>
24
25void _CORE_RWLock_Obtain_for_writing(
26  CORE_RWLock_Control                 *the_rwlock,
27  Thread_Control                      *executing,
28  Objects_Id                           id,
29  bool                                 wait,
30  Watchdog_Interval                    timeout,
31  CORE_RWLock_API_mp_support_callout   api_rwlock_mp_support
32)
33{
34  ISR_Level       level;
35
36  /*
37   *  If unlocked, then OK to read.
38   *  Otherwise, we have to block.
39   *  If locked for reading and no waiters, then OK to read.
40   *  If any thread is waiting, then we wait.
41   */
42
43  _ISR_Disable( level );
44    switch ( the_rwlock->current_state ) {
45      case CORE_RWLOCK_UNLOCKED:
46        the_rwlock->current_state = CORE_RWLOCK_LOCKED_FOR_WRITING;
47        _ISR_Enable( level );
48        executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL;
49        return;
50
51      case CORE_RWLOCK_LOCKED_FOR_READING:
52      case CORE_RWLOCK_LOCKED_FOR_WRITING:
53        break;
54    }
55
56    /*
57     *  If the thread is not willing to wait, then return immediately.
58     */
59
60    if ( !wait ) {
61      _ISR_Enable( level );
62      executing->Wait.return_code = CORE_RWLOCK_UNAVAILABLE;
63      return;
64    }
65
66    /*
67     *  We need to wait to enter this critical section
68     */
69
70    _Thread_queue_Enter_critical_section( &the_rwlock->Wait_queue );
71    executing->Wait.queue       = &the_rwlock->Wait_queue;
72    executing->Wait.id          = id;
73    executing->Wait.option      = CORE_RWLOCK_THREAD_WAITING_FOR_WRITE;
74    executing->Wait.return_code = CORE_RWLOCK_SUCCESSFUL;
75    _ISR_Enable( level );
76
77    _Thread_queue_Enqueue_with_handler(
78       &the_rwlock->Wait_queue,
79       executing,
80       timeout,
81       _CORE_RWLock_Timeout
82    );
83
84
85    /* return to API level so it can dispatch and we block */
86}
Note: See TracBrowser for help on using the repository browser.