source: rtems/cpukit/rtems/src/regiongetsegment.c @ 1142f55

5
Last change on this file since 1142f55 was 1142f55, checked in by Sebastian Huber <sebastian.huber@…>, on 04/08/16 at 04:56:46

rtems: Add and use _Region_Get_and_lock()

Get region and lock allocator in _Region_Get_and_lock() in case the
region exists and unlock it in _Region_Unlock().

  • Property mode set to 100644
File size: 2.3 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief RTEMS Get Region Segment
5 *  @ingroup ClassicRegion
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/rtems/regionimpl.h>
22#include <rtems/rtems/optionsimpl.h>
23#include <rtems/score/threadqimpl.h>
24#include <rtems/score/statesimpl.h>
25
26rtems_status_code rtems_region_get_segment(
27  rtems_id           id,
28  uintptr_t          size,
29  rtems_option       option_set,
30  rtems_interval     timeout,
31  void              **segment
32)
33{
34  rtems_status_code  status;
35  Region_Control    *the_region;
36
37  if ( segment == NULL ) {
38    return RTEMS_INVALID_ADDRESS;
39  }
40
41  *segment = NULL;
42
43  if ( size == 0 ) {
44    return RTEMS_INVALID_SIZE;
45  }
46
47  the_region = _Region_Get_and_lock( id );
48
49  if ( the_region == NULL ) {
50    return RTEMS_INVALID_ID;
51  }
52
53  if ( size > the_region->maximum_segment_size ) {
54    status = RTEMS_INVALID_SIZE;
55  } else {
56    void *the_segment;
57
58    the_segment = _Region_Allocate_segment( the_region, size );
59
60    if ( the_segment != NULL ) {
61      the_region->number_of_used_blocks += 1;
62      *segment = the_segment;
63      status = RTEMS_SUCCESSFUL;
64    } else if ( _Options_Is_no_wait( option_set ) ) {
65      status = RTEMS_UNSATISFIED;
66    } else {
67      Per_CPU_Control *cpu_self;
68      Thread_Control  *executing;
69
70      /*
71       *  Switch from using the memory allocation mutex to using a
72       *  dispatching disabled critical section.  We have to do this
73       *  because this thread is going to block.
74       */
75      /* FIXME: This is a home grown condition variable */
76      cpu_self = _Thread_Dispatch_disable();
77      _Region_Unlock( the_region );
78
79      executing  = _Per_CPU_Get_executing( cpu_self );
80
81      executing->Wait.count           = size;
82      executing->Wait.return_argument = segment;
83
84      _Thread_queue_Enqueue(
85        &the_region->Wait_queue,
86        the_region->wait_operations,
87        executing,
88        STATES_WAITING_FOR_SEGMENT,
89        timeout,
90        RTEMS_TIMEOUT
91      );
92
93      _Thread_Dispatch_enable( cpu_self );
94
95      return (rtems_status_code) executing->Wait.return_code;
96    }
97  }
98
99  _Region_Unlock( the_region );
100  return status;
101}
Note: See TracBrowser for help on using the repository browser.