source: rtems/cpukit/score/src/coremsg.c @ 28352fae

4.104.115
Last change on this file since 28352fae was 28352fae, checked in by Ralf Corsepius <ralf.corsepius@…>, on 11/29/09 at 13:51:53

Whitespace removal.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 *  CORE Message Queue Handler
3 *
4 *  DESCRIPTION:
5 *
6 *  This package is the implementation of the CORE Message Queue Handler.
7 *  This core object provides task synchronization and communication functions
8 *  via messages passed to queue objects.
9 *
10 *  COPYRIGHT (c) 1989-2009.
11 *  On-Line Applications Research Corporation (OAR).
12 *
13 *  The license and distribution terms for this file may be
14 *  found in the file LICENSE in this distribution or at
15 *  http://www.rtems.com/license/LICENSE.
16 *
17 *  $Id$
18 */
19
20#if HAVE_CONFIG_H
21#include "config.h"
22#endif
23
24#include <rtems/system.h>
25#include <rtems/score/chain.h>
26#include <rtems/score/isr.h>
27#include <rtems/score/object.h>
28#include <rtems/score/coremsg.h>
29#include <rtems/score/states.h>
30#include <rtems/score/thread.h>
31#include <rtems/score/wkspace.h>
32
33/*
34 *  _CORE_message_queue_Initialize
35 *
36 *  This routine initializes a newly created message queue based on the
37 *  specified data.
38 *
39 *  Input parameters:
40 *    the_message_queue            - the message queue to initialize
41 *    the_class                    - the API specific object class
42 *    the_message_queue_attributes - the message queue's attributes
43 *    maximum_pending_messages     - maximum message and reserved buffer count
44 *    maximum_message_size         - maximum size of each message
45 *
46 *  Output parameters:
47 *    true   - if the message queue is initialized
48 *    false  - if the message queue is NOT initialized
49 */
50
51bool _CORE_message_queue_Initialize(
52  CORE_message_queue_Control    *the_message_queue,
53  CORE_message_queue_Attributes *the_message_queue_attributes,
54  uint32_t                       maximum_pending_messages,
55  size_t                         maximum_message_size
56)
57{
58  size_t message_buffering_required;
59  size_t allocated_message_size;
60
61  the_message_queue->maximum_pending_messages   = maximum_pending_messages;
62  the_message_queue->number_of_pending_messages = 0;
63  the_message_queue->maximum_message_size       = maximum_message_size;
64  _CORE_message_queue_Set_notify( the_message_queue, NULL, NULL );
65
66  /*
67   *  Round size up to multiple of a pointer for chain init and
68   *  check for overflow on adding overhead to each message.
69   */
70  allocated_message_size = maximum_message_size;
71  if (allocated_message_size & (sizeof(uint32_t) - 1)) {
72    allocated_message_size += sizeof(uint32_t);
73    allocated_message_size &= ~(sizeof(uint32_t) - 1);
74  }
75
76  if (allocated_message_size < maximum_message_size)
77    return false;
78
79  /*
80   *  Calculate how much total memory is required for message buffering and
81   *  check for overflow on the multiplication.
82   */
83  message_buffering_required = (size_t) maximum_pending_messages *
84       (allocated_message_size + sizeof(CORE_message_queue_Buffer_control));
85
86  if (message_buffering_required < allocated_message_size)
87    return false;
88
89  /*
90   *  Attempt to allocate the message memory
91   */
92  the_message_queue->message_buffers = (CORE_message_queue_Buffer *)
93     _Workspace_Allocate( message_buffering_required );
94
95  if (the_message_queue->message_buffers == 0)
96    return false;
97
98  /*
99   *  Initialize the pool of inactive messages, pending messages,
100   *  and set of waiting threads.
101   */
102  _Chain_Initialize (
103    &the_message_queue->Inactive_messages,
104    the_message_queue->message_buffers,
105    (size_t) maximum_pending_messages,
106    allocated_message_size + sizeof( CORE_message_queue_Buffer_control )
107  );
108
109  _Chain_Initialize_empty( &the_message_queue->Pending_messages );
110
111  _Thread_queue_Initialize(
112    &the_message_queue->Wait_queue,
113    _CORE_message_queue_Is_priority( the_message_queue_attributes ) ?
114       THREAD_QUEUE_DISCIPLINE_PRIORITY : THREAD_QUEUE_DISCIPLINE_FIFO,
115    STATES_WAITING_FOR_MESSAGE,
116    CORE_MESSAGE_QUEUE_STATUS_TIMEOUT
117  );
118
119  return true;
120}
Note: See TracBrowser for help on using the repository browser.