source: rtems/c/src/exec/score/src/coremsg.c @ ed0e74e

4.104.114.84.95
Last change on this file since ed0e74e was ed0e74e, checked in by Joel Sherrill <joel.sherrill@…>, on Dec 3, 1998 at 9:53:25 PM

Corrected spacing.

  • Property mode set to 100644
File size: 13.8 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-1998.
11 *  On-Line Applications Research Corporation (OAR).
12 *  Copyright assigned to U.S. Government, 1994.
13 *
14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
16 *  http://www.OARcorp.com/rtems/license.html.
17 *
18 *  $Id$
19 */
20
21#include <rtems/system.h>
22#include <rtems/score/chain.h>
23#include <rtems/score/isr.h>
24#include <rtems/score/object.h>
25#include <rtems/score/coremsg.h>
26#include <rtems/score/states.h>
27#include <rtems/score/thread.h>
28#include <rtems/score/wkspace.h>
29#if defined(RTEMS_MULTIPROCESSING)
30#include <rtems/score/mpci.h>
31#endif
32
33/*PAGE
34 *
35 *  _CORE_message_queue_Initialize
36 *
37 *  This routine initializes a newly created message queue based on the
38 *  specified data.
39 *
40 *  Input parameters:
41 *    the_message_queue            - the message queue to initialize
42 *    the_class                    - the API specific object class
43 *    the_message_queue_attributes - the message queue's attributes
44 *    maximum_pending_messages     - maximum message and reserved buffer count
45 *    maximum_message_size         - maximum size of each message
46 *    proxy_extract_callout        - remote extract support
47 *
48 *  Output parameters:
49 *    TRUE   - if the message queue is initialized
50 *    FALSE  - if the message queue is NOT initialized
51 */
52
53boolean _CORE_message_queue_Initialize(
54  CORE_message_queue_Control    *the_message_queue,
55  Objects_Classes                the_class,
56  CORE_message_queue_Attributes *the_message_queue_attributes,
57  unsigned32                     maximum_pending_messages,
58  unsigned32                     maximum_message_size,
59  Thread_queue_Extract_callout   proxy_extract_callout
60)
61{
62  unsigned32 message_buffering_required;
63  unsigned32 allocated_message_size;
64
65  the_message_queue->maximum_pending_messages   = maximum_pending_messages;
66  the_message_queue->number_of_pending_messages = 0;
67  the_message_queue->maximum_message_size       = maximum_message_size;
68  _CORE_message_queue_Set_notify( the_message_queue, NULL, NULL );
69 
70  /*
71   * round size up to multiple of a ptr for chain init
72   */
73 
74  allocated_message_size = maximum_message_size;
75  if (allocated_message_size & (sizeof(unsigned32) - 1)) {
76      allocated_message_size += sizeof(unsigned32);
77      allocated_message_size &= ~(sizeof(unsigned32) - 1);
78  }
79   
80  message_buffering_required = maximum_pending_messages *
81       (allocated_message_size + sizeof(CORE_message_queue_Buffer_control));
82 
83  the_message_queue->message_buffers = (CORE_message_queue_Buffer *) 
84     _Workspace_Allocate( message_buffering_required );
85 
86  if (the_message_queue->message_buffers == 0)
87    return FALSE;
88 
89  _Chain_Initialize (
90    &the_message_queue->Inactive_messages,
91    the_message_queue->message_buffers,
92    maximum_pending_messages,
93    allocated_message_size + sizeof( CORE_message_queue_Buffer_control )
94  );
95 
96  _Chain_Initialize_empty( &the_message_queue->Pending_messages );
97 
98  _Thread_queue_Initialize(
99    &the_message_queue->Wait_queue,
100    the_class,
101    _CORE_message_queue_Is_priority( the_message_queue_attributes ) ?
102       THREAD_QUEUE_DISCIPLINE_PRIORITY : THREAD_QUEUE_DISCIPLINE_FIFO,
103    STATES_WAITING_FOR_MESSAGE,
104    proxy_extract_callout,
105    CORE_MESSAGE_QUEUE_STATUS_TIMEOUT
106  );
107
108  return TRUE;
109}
110
111/*PAGE
112 *
113 *  _CORE_message_queue_Close
114 *
115 *  This function closes a message by returning all allocated space and
116 *  flushing the message_queue's task wait queue.
117 *
118 *  Input parameters:
119 *    the_message_queue      - the message_queue to be flushed
120 *    remote_extract_callout - function to invoke remotely
121 *    status                 - status to pass to thread
122 *
123 *  Output parameters:  NONE
124 */
125 
126void _CORE_message_queue_Close(
127  CORE_message_queue_Control *the_message_queue,
128  Thread_queue_Flush_callout  remote_extract_callout,
129  unsigned32                  status
130)
131{
132 
133  if ( the_message_queue->number_of_pending_messages != 0 )
134    (void) _CORE_message_queue_Flush_support( the_message_queue );
135  else
136    _Thread_queue_Flush(
137      &the_message_queue->Wait_queue,
138      remote_extract_callout,
139      status
140    );
141
142  (void) _Workspace_Free( the_message_queue->message_buffers );
143
144}
145
146/*PAGE
147 *
148 *  _CORE_message_queue_Flush
149 *
150 *  This function flushes the message_queue's task wait queue.  The number
151 *  of messages flushed from the queue is returned.
152 *
153 *  Input parameters:
154 *    the_message_queue - the message_queue to be flushed
155 *
156 *  Output parameters:
157 *    returns - the number of messages flushed from the queue
158 */
159 
160unsigned32 _CORE_message_queue_Flush(
161  CORE_message_queue_Control *the_message_queue
162)
163{
164  if ( the_message_queue->number_of_pending_messages != 0 )
165    return _CORE_message_queue_Flush_support( the_message_queue );
166  else
167    return 0;
168}
169
170/*PAGE
171 *
172 *  _CORE_message_queue_Broadcast
173 *
174 *  This function sends a message for every thread waiting on the queue and
175 *  returns the number of threads made ready by the message.
176 *
177 *  Input parameters:
178 *    the_message_queue            - message is submitted to this message queue
179 *    buffer                       - pointer to message buffer
180 *    size                         - size in bytes of message to send
181 *    id                           - id of message queue
182 *    api_message_queue_mp_support - api specific mp support callout
183 *    count                        - area to store number of threads made ready
184 *
185 *  Output parameters:
186 *    count                         - number of threads made ready
187 *    CORE_MESSAGE_QUEUE_SUCCESSFUL - if successful
188 *    error code                    - if unsuccessful
189 */
190
191CORE_message_queue_Status _CORE_message_queue_Broadcast(
192  CORE_message_queue_Control                *the_message_queue,
193  void                                      *buffer,
194  unsigned32                                 size,
195  Objects_Id                                 id,
196  CORE_message_queue_API_mp_support_callout  api_message_queue_mp_support,
197  unsigned32                                *count
198)
199{
200  Thread_Control          *the_thread;
201  unsigned32               number_broadcasted;
202  Thread_Wait_information *waitp;
203  unsigned32               constrained_size;
204
205  number_broadcasted = 0;
206  while ((the_thread = _Thread_queue_Dequeue(&the_message_queue->Wait_queue))) {
207    waitp = &the_thread->Wait;
208    number_broadcasted += 1;
209
210    constrained_size = size;
211    if ( size > the_message_queue->maximum_message_size )
212        constrained_size = the_message_queue->maximum_message_size;
213
214    _CORE_message_queue_Copy_buffer(
215      buffer,
216      waitp->return_argument,
217      constrained_size
218    );
219
220    *(unsigned32 *)the_thread->Wait.return_argument_1 = size;
221
222#if defined(RTEMS_MULTIPROCESSING)
223    if ( !_Objects_Is_local_id( the_thread->Object.id ) )
224      (*api_message_queue_mp_support) ( the_thread, id );
225#endif
226
227  }
228  *count = number_broadcasted;
229  return CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL;
230}
231
232/*PAGE
233 *
234 *  _CORE_message_queue_Seize
235 *
236 *  This kernel routine dequeues a message, copies the message buffer to
237 *  a given destination buffer, and frees the message buffer to the
238 *  inactive message pool.  The thread will be blocked if wait is TRUE,
239 *  otherwise an error will be given to the thread if no messages are available.
240 *
241 *  Input parameters:
242 *    the_message_queue - pointer to message queue
243 *    id                - id of object we are waitig on
244 *    buffer            - pointer to message buffer to be filled
245 *    size              - pointer to the size of buffer to be filled
246 *    wait              - TRUE if wait is allowed, FALSE otherwise
247 *    timeout           - time to wait for a message
248 *
249 *  Output parameters:  NONE
250 *
251 *  NOTE: Dependent on BUFFER_LENGTH
252 *
253 *  INTERRUPT LATENCY:
254 *    available
255 *    wait
256 */
257
258void _CORE_message_queue_Seize(
259  CORE_message_queue_Control *the_message_queue,
260  Objects_Id                  id,
261  void                       *buffer,
262  unsigned32                 *size,
263  boolean                     wait,
264  Watchdog_Interval           timeout
265)
266{
267  ISR_Level                          level;
268  CORE_message_queue_Buffer_control *the_message;
269  Thread_Control                    *executing;
270
271  executing = _Thread_Executing;
272  executing->Wait.return_code = CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL;
273  _ISR_Disable( level );
274  if ( the_message_queue->number_of_pending_messages != 0 ) {
275    the_message_queue->number_of_pending_messages -= 1;
276
277    the_message = _CORE_message_queue_Get_pending_message( the_message_queue );
278    _ISR_Enable( level );
279    *size = the_message->Contents.size;
280    _CORE_message_queue_Copy_buffer(the_message->Contents.buffer,buffer,*size );
281    _CORE_message_queue_Free_message_buffer(the_message_queue, the_message );
282    return;
283  }
284
285  if ( !wait ) {
286    _ISR_Enable( level );
287    executing->Wait.return_code = CORE_MESSAGE_QUEUE_STATUS_UNSATISFIED_NOWAIT;
288    return;
289  }
290
291  _Thread_queue_Enter_critical_section( &the_message_queue->Wait_queue );
292  executing->Wait.queue              = &the_message_queue->Wait_queue;
293  executing->Wait.id                 = id;
294  executing->Wait.return_argument    = (void *)buffer;
295  executing->Wait.return_argument_1  = (void *)size;
296  _ISR_Enable( level );
297
298  _Thread_queue_Enqueue( &the_message_queue->Wait_queue, timeout );
299}
300
301/*PAGE
302 *
303 *  _CORE_message_queue_Flush_support
304 *
305 *  This message handler routine removes all messages from a message queue
306 *  and returns them to the inactive message pool.  The number of messages
307 *  flushed from the queue is returned
308 *
309 *  Input parameters:
310 *    the_message_queue - pointer to message queue
311 *
312 *  Output parameters:
313 *    returns - number of messages placed on inactive chain
314 *
315 *  INTERRUPT LATENCY:
316 *    only case
317 */
318
319unsigned32 _CORE_message_queue_Flush_support(
320  CORE_message_queue_Control *the_message_queue
321)
322{
323  ISR_Level   level;
324  Chain_Node *inactive_first;
325  Chain_Node *message_queue_first;
326  Chain_Node *message_queue_last;
327  unsigned32  count;
328
329  _ISR_Disable( level );
330    inactive_first      = the_message_queue->Inactive_messages.first;
331    message_queue_first = the_message_queue->Pending_messages.first;
332    message_queue_last  = the_message_queue->Pending_messages.last;
333
334    the_message_queue->Inactive_messages.first = message_queue_first;
335    message_queue_last->next = inactive_first;
336    inactive_first->previous = message_queue_last;
337    message_queue_first->previous          =
338               _Chain_Head( &the_message_queue->Inactive_messages );
339
340    _Chain_Initialize_empty( &the_message_queue->Pending_messages );
341
342    count = the_message_queue->number_of_pending_messages;
343    the_message_queue->number_of_pending_messages = 0;
344  _ISR_Enable( level );
345  return count;
346}
347
348/*PAGE
349 *
350 *  _CORE_message_queue_Submit
351 *
352 *  This routine implements the send and urgent message functions. It
353 *  processes a message that is to be submitted to the designated
354 *  message queue.  The message will either be processed as a
355 *  send message which it will be inserted at the rear of the queue
356 *  or it will be processed as an urgent message which will be inserted
357 *  at the front of the queue.
358 *
359 *  Input parameters:
360 *    the_message_queue            - message is submitted to this message queue
361 *    buffer                       - pointer to message buffer
362 *    size                         - size in bytes of message to send
363 *    id                           - id of message queue
364 *    api_message_queue_mp_support - api specific mp support callout
365 *    submit_type                  - send or urgent message
366 *
367 *  Output parameters:
368 *    CORE_MESSAGE_QUEUE_SUCCESSFUL - if successful
369 *    error code                    - if unsuccessful
370 */
371
372CORE_message_queue_Status _CORE_message_queue_Submit(
373  CORE_message_queue_Control                *the_message_queue,
374  void                                      *buffer,
375  unsigned32                                 size,
376  Objects_Id                                 id,
377  CORE_message_queue_API_mp_support_callout  api_message_queue_mp_support,
378  CORE_message_queue_Submit_types            submit_type
379)
380{
381  CORE_message_queue_Buffer_control   *the_message;
382  Thread_Control                      *the_thread;
383
384  if ( size > the_message_queue->maximum_message_size )
385    return CORE_MESSAGE_QUEUE_STATUS_INVALID_SIZE;
386
387  /*
388   * Is there a thread currently waiting on this message queue?
389   */
390     
391  the_thread = _Thread_queue_Dequeue( &the_message_queue->Wait_queue );
392  if ( the_thread )
393  {
394    _CORE_message_queue_Copy_buffer(
395      buffer,
396      the_thread->Wait.return_argument,
397      size
398    );
399    *(unsigned32 *)the_thread->Wait.return_argument_1 = size;
400   
401#if defined(RTEMS_MULTIPROCESSING)
402    if ( !_Objects_Is_local_id( the_thread->Object.id ) )
403      (*api_message_queue_mp_support) ( the_thread, id );
404#endif
405
406    return CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL;
407  }
408
409  /*
410   * No one waiting on this one currently.
411   * Allocate a message buffer and store it away
412   */
413
414  if ( the_message_queue->number_of_pending_messages ==
415       the_message_queue->maximum_pending_messages ) {
416    return CORE_MESSAGE_QUEUE_STATUS_TOO_MANY;
417  }
418
419  the_message = _CORE_message_queue_Allocate_message_buffer(the_message_queue);
420  if ( the_message == 0)
421    return CORE_MESSAGE_QUEUE_STATUS_UNSATISFIED;
422
423  _CORE_message_queue_Copy_buffer( buffer, the_message->Contents.buffer, size );
424  the_message->Contents.size = size;
425 
426  the_message_queue->number_of_pending_messages += 1;
427
428  switch ( submit_type ) {
429    case CORE_MESSAGE_QUEUE_SEND_REQUEST:
430      _CORE_message_queue_Append( the_message_queue, the_message );
431      break;
432    case CORE_MESSAGE_QUEUE_URGENT_REQUEST:
433      _CORE_message_queue_Prepend( the_message_queue, the_message );
434      break;
435  }
436
437  return CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL;
438}
Note: See TracBrowser for help on using the repository browser.