source: rtems/cpukit/posix/src/key.c @ 77fbbd6

5
Last change on this file since 77fbbd6 was 1f22b26, checked in by Sebastian Huber <sebastian.huber@…>, on 08/25/17 at 08:59:52

Include missing <limits.h>

Update #2132.

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/**
2 * @file
3 *
4 * @brief POSIX Keys Manager Initialization
5 * @ingroup POSIX_KEY Key
6 */
7
8/*
9 * Copyright (c) 2012 Zhongwei Yao.
10 * COPYRIGHT (c) 1989-2014.
11 * On-Line Applications Research Corporation (OAR).
12 * Copyright (c) 2016 embedded brains GmbH.
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.rtems.org/license/LICENSE.
17 */
18
19#if HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include <limits.h>
24
25#include <rtems/config.h>
26#include <rtems/sysinit.h>
27
28#include <rtems/posix/keyimpl.h>
29#include <rtems/score/userextimpl.h>
30#include <rtems/score/wkspace.h>
31
32Objects_Information _POSIX_Keys_Information;
33
34Freechain_Control _POSIX_Keys_Keypool;
35
36static uint32_t _POSIX_Keys_Get_keypool_bump_count( void )
37{
38  uint32_t max = Configuration.maximum_key_value_pairs;
39
40  return _Objects_Is_unlimited( max ) ?
41    _Objects_Maximum_per_allocation( max ) : 0;
42}
43
44static uint32_t _POSIX_Keys_Get_initial_keypool_size( void )
45{
46  uint32_t max = Configuration.maximum_key_value_pairs;
47
48  return _Objects_Maximum_per_allocation( max );
49}
50
51static void _POSIX_Keys_Initialize_keypool( void )
52{
53  _Freechain_Initialize(
54    &_POSIX_Keys_Keypool,
55    _Workspace_Allocate_or_fatal_error,
56    _POSIX_Keys_Get_initial_keypool_size(),
57    sizeof( POSIX_Keys_Key_value_pair )
58  );
59}
60
61POSIX_Keys_Key_value_pair * _POSIX_Keys_Key_value_allocate( void )
62{
63  return (POSIX_Keys_Key_value_pair *) _Freechain_Get(
64    &_POSIX_Keys_Keypool,
65    _Workspace_Allocate,
66    _POSIX_Keys_Get_keypool_bump_count(),
67    sizeof( POSIX_Keys_Key_value_pair )
68  );
69}
70
71static void _POSIX_Keys_Run_destructors( Thread_Control *the_thread )
72{
73  while ( true ) {
74    ISR_lock_Context  lock_context;
75    RBTree_Node      *node;
76
77    _Objects_Allocator_lock();
78    _POSIX_Keys_Key_value_acquire( the_thread, &lock_context );
79
80    node = _RBTree_Root( &the_thread->Keys.Key_value_pairs );
81    if ( node != NULL ) {
82      POSIX_Keys_Key_value_pair *key_value_pair;
83      pthread_key_t              key;
84      void                      *value;
85      POSIX_Keys_Control        *the_key;
86      void                    ( *destructor )( void * );
87
88      key_value_pair = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( node );
89      key = key_value_pair->key;
90      value = key_value_pair->value;
91      _RBTree_Extract(
92        &the_thread->Keys.Key_value_pairs,
93        &key_value_pair->Lookup_node
94      );
95
96      _POSIX_Keys_Key_value_release( the_thread, &lock_context );
97      _POSIX_Keys_Key_value_free( key_value_pair );
98
99      the_key = _POSIX_Keys_Get( key );
100      _Assert( the_key != NULL );
101      destructor = the_key->destructor;
102
103      _Objects_Allocator_unlock();
104
105      if ( destructor != NULL && value != NULL ) {
106        ( *destructor )( value );
107      }
108    } else {
109      _POSIX_Keys_Key_value_release( the_thread, &lock_context );
110      _Objects_Allocator_unlock();
111      break;
112    }
113  }
114}
115
116static void _POSIX_Keys_Restart_run_destructors(
117  Thread_Control *executing,
118  Thread_Control *the_thread
119)
120{
121  (void) executing;
122  _POSIX_Keys_Run_destructors( the_thread );
123}
124
125static User_extensions_Control _POSIX_Keys_Extensions = {
126  .Callouts = {
127    .thread_restart = _POSIX_Keys_Restart_run_destructors,
128    .thread_terminate = _POSIX_Keys_Run_destructors
129  }
130};
131
132/**
133 * @brief This routine performs the initialization necessary for this manager.
134 */
135static void _POSIX_Keys_Manager_initialization(void)
136{
137  _Objects_Initialize_information(
138    &_POSIX_Keys_Information,   /* object information table */
139    OBJECTS_POSIX_API,          /* object API */
140    OBJECTS_POSIX_KEYS,         /* object class */
141    Configuration.maximum_keys,
142                                /* maximum objects of this class */
143    sizeof( POSIX_Keys_Control ),
144                                /* size of this object's control block */
145    true,                       /* true if names for this object are strings */
146    _POSIX_PATH_MAX,            /* maximum length of each object's name */
147    NULL                        /* Proxy extraction support callout */
148  );
149
150  _POSIX_Keys_Initialize_keypool();
151
152  _User_extensions_Add_API_set( &_POSIX_Keys_Extensions );
153}
154
155RTEMS_SYSINIT_ITEM(
156  _POSIX_Keys_Manager_initialization,
157  RTEMS_SYSINIT_POSIX_KEYS,
158  RTEMS_SYSINIT_ORDER_MIDDLE
159);
Note: See TracBrowser for help on using the repository browser.