summaryrefslogtreecommitdiffstats
path: root/cpukit/score/src/coremsginsert.c
blob: 69836df5936ea599992bae41b233340eb20d8478 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
 * @file
 *
 * @brief Insert a Message into the Message Queue
 * @ingroup ScoreMessageQueue
 */

/*
 *  COPYRIGHT (c) 1989-2005.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems/system.h>
#include <rtems/score/chain.h>
#include <rtems/score/isr.h>
#include <rtems/score/object.h>
#include <rtems/score/coremsgimpl.h>
#include <rtems/score/states.h>
#include <rtems/score/thread.h>
#include <rtems/score/wkspace.h>

void _CORE_message_queue_Insert_message(
  CORE_message_queue_Control        *the_message_queue,
  CORE_message_queue_Buffer_control *the_message,
  CORE_message_queue_Submit_types    submit_type
)
{
  ISR_Level  level;
  #if defined(RTEMS_SCORE_COREMSG_ENABLE_NOTIFICATION)
    bool    notify = false;
    #define SET_NOTIFY() \
      do { \
        if ( the_message_queue->number_of_pending_messages == 0 ) \
          notify = true; \
      } while (0)
  #else
    #define SET_NOTIFY()
  #endif

  _CORE_message_queue_Set_message_priority( the_message, submit_type );

  #if !defined(RTEMS_SCORE_COREMSG_ENABLE_MESSAGE_PRIORITY)
    _ISR_Disable( level );
      SET_NOTIFY();
      the_message_queue->number_of_pending_messages++;
      if ( submit_type == CORE_MESSAGE_QUEUE_SEND_REQUEST )
        _CORE_message_queue_Append_unprotected(the_message_queue, the_message);
      else
        _CORE_message_queue_Prepend_unprotected(the_message_queue, the_message);
    _ISR_Enable( level );
  #else
    if ( submit_type == CORE_MESSAGE_QUEUE_SEND_REQUEST ) {
      _ISR_Disable( level );
        SET_NOTIFY();
        the_message_queue->number_of_pending_messages++;
        _CORE_message_queue_Append_unprotected(the_message_queue, the_message);
      _ISR_Enable( level );
    } else if ( submit_type == CORE_MESSAGE_QUEUE_URGENT_REQUEST ) {
      _ISR_Disable( level );
        SET_NOTIFY();
        the_message_queue->number_of_pending_messages++;
        _CORE_message_queue_Prepend_unprotected(the_message_queue, the_message);
      _ISR_Enable( level );
    } else {
      CORE_message_queue_Buffer_control *this_message;
      Chain_Node                        *the_node;
      Chain_Control                     *the_header;
      int                                the_priority;

      the_priority = _CORE_message_queue_Get_message_priority(the_message);
      the_header = &the_message_queue->Pending_messages;
      the_node = _Chain_First( the_header );
      while ( !_Chain_Is_tail( the_header, the_node ) ) {
        int this_priority;

        this_message = (CORE_message_queue_Buffer_control *) the_node;

        this_priority = _CORE_message_queue_Get_message_priority(this_message);

        if ( this_priority <= the_priority ) {
          the_node = the_node->next;
          continue;
        }
        break;
      }
      _ISR_Disable( level );
        SET_NOTIFY();
        the_message_queue->number_of_pending_messages++;
        _Chain_Insert_unprotected( the_node->previous, &the_message->Node );
      _ISR_Enable( level );
    }
  #endif

  #if defined(RTEMS_SCORE_COREMSG_ENABLE_NOTIFICATION)
    /*
     *  According to POSIX, does this happen before or after the message
     *  is actually enqueued.  It is logical to think afterwards, because
     *  the message is actually in the queue at this point.
     */
    if ( notify && the_message_queue->notify_handler )
      (*the_message_queue->notify_handler)(the_message_queue->notify_argument);
  #endif
}