source: rtems/cpukit/score/src/coremsgseize.c @ cc18d7b

4.115
Last change on this file since cc18d7b was cc18d7b, checked in by Sebastian Huber <sebastian.huber@…>, on 04/30/15 at 11:12:54

score: Fine grained locking for message queues

Aggregate several critical sections into a bigger one. Sending and
receiving messages is now protected by an ISR lock. Thread dispatching
is only disabled in case a blocking operation is necessary. The message
copy procedure is done inside the critical section (interrupts
disabled). Thus this change may have a negative impact on the interrupt
latency in case very large messages are transferred.

Update #2273.

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief Size a Message from the Message Queue
5 *  @ingroup ScoreMessageQueue
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2007.
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/system.h>
22#include <rtems/score/chain.h>
23#include <rtems/score/isr.h>
24#include <rtems/score/coremsgimpl.h>
25#include <rtems/score/thread.h>
26#include <rtems/score/statesimpl.h>
27#include <rtems/score/wkspace.h>
28
29void _CORE_message_queue_Seize(
30  CORE_message_queue_Control      *the_message_queue,
31  Thread_Control                  *executing,
32  Objects_Id                       id,
33  void                            *buffer,
34  size_t                          *size_p,
35  bool                             wait,
36  Watchdog_Interval                timeout,
37  ISR_lock_Context                *lock_context
38)
39{
40  CORE_message_queue_Buffer_control *the_message;
41
42  executing->Wait.return_code = CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL;
43  _CORE_message_queue_Acquire_critical( the_message_queue, lock_context );
44  the_message = _CORE_message_queue_Get_pending_message( the_message_queue );
45  if ( the_message != NULL ) {
46    the_message_queue->number_of_pending_messages -= 1;
47
48    *size_p = the_message->Contents.size;
49    executing->Wait.count =
50      _CORE_message_queue_Get_message_priority( the_message );
51    _CORE_message_queue_Copy_buffer(
52      the_message->Contents.buffer,
53      buffer,
54      *size_p
55    );
56
57    #if !defined(RTEMS_SCORE_COREMSG_ENABLE_BLOCKING_SEND)
58      /*
59       *  There is not an API with blocking sends enabled.
60       *  So return immediately.
61       */
62      _CORE_message_queue_Free_message_buffer(the_message_queue, the_message);
63      _CORE_message_queue_Release( the_message_queue, lock_context );
64      return;
65    #else
66    {
67      Thread_Control   *the_thread;
68
69      /*
70       *  There could be a thread waiting to send a message.  If there
71       *  is not, then we can go ahead and free the buffer.
72       *
73       *  NOTE: If we note that the queue was not full before this receive,
74       *  then we can avoid this dequeue.
75       */
76      the_thread = _Thread_queue_First_locked(
77        &the_message_queue->Wait_queue
78      );
79      if ( the_thread == NULL ) {
80        _CORE_message_queue_Free_message_buffer(
81          the_message_queue,
82          the_message
83        );
84        _CORE_message_queue_Release( the_message_queue, lock_context );
85        return;
86      }
87
88      /*
89       *  There was a thread waiting to send a message.  This code
90       *  puts the messages in the message queue on behalf of the
91       *  waiting task.
92       */
93      _CORE_message_queue_Set_message_priority(
94        the_message,
95        the_thread->Wait.count
96      );
97      the_message->Contents.size = (size_t) the_thread->Wait.option;
98      _CORE_message_queue_Copy_buffer(
99        the_thread->Wait.return_argument_second.immutable_object,
100        the_message->Contents.buffer,
101        the_message->Contents.size
102      );
103
104      _CORE_message_queue_Insert_message(
105         the_message_queue,
106         the_message,
107         _CORE_message_queue_Get_message_priority( the_message )
108      );
109      _Thread_queue_Extract_critical(
110        &the_message_queue->Wait_queue,
111        the_thread,
112        lock_context
113      );
114      #if defined(RTEMS_MULTIPROCESSING)
115        _Thread_Dispatch_enable( _Per_CPU_Get() );
116      #endif
117      return;
118    }
119    #endif
120  }
121
122  if ( !wait ) {
123    _CORE_message_queue_Release( the_message_queue, lock_context );
124    executing->Wait.return_code = CORE_MESSAGE_QUEUE_STATUS_UNSATISFIED_NOWAIT;
125    return;
126  }
127
128  executing->Wait.id = id;
129  executing->Wait.return_argument_second.mutable_object = buffer;
130  executing->Wait.return_argument = size_p;
131  /* Wait.count will be filled in with the message priority */
132
133  _Thread_queue_Enqueue_critical(
134    &the_message_queue->Wait_queue,
135    executing,
136    STATES_WAITING_FOR_MESSAGE,
137    timeout,
138    CORE_MESSAGE_QUEUE_STATUS_TIMEOUT,
139    lock_context
140  );
141  #if defined(RTEMS_MULTIPROCESSING)
142    _Thread_Dispatch_enable( _Per_CPU_Get() );
143  #endif
144}
Note: See TracBrowser for help on using the repository browser.