summaryrefslogtreecommitdiffstats
path: root/cpukit/posix/src/keyrundestructors.c
blob: 7e3c88fdb6b59b536b13c52f659ee5f1d4d6b794 (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
/**
 * @file
 *
 * @brief Thread-Specific Data Key Create
 * @ingroup POSIX_KEY Key
 */

/*
 * Copyright (c) 2012 Zhongwei Yao.
 * Copyright (c) 2010 embedded brains GmbH.
 *
 * COPYRIGHT (c) 1989-2007.
 * 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/thread.h>
#include <rtems/score/wkspace.h>
#include <rtems/score/chain.h>
#include <rtems/posix/key.h>
#include <rtems/posix/threadsup.h>

/*
 *  _POSIX_Keys_Run_destructors
 *
 *  17.1.1 Thread-Specific Data Key Create, P1003.1c/Draft 10, p. 163
 *
 *  NOTE:  This is the routine executed when a thread exits to
 *         run through all the keys and do the destructor action.
 */
void _POSIX_Keys_Run_destructors(
  Thread_Control *thread
)
{
  Chain_Control *chain;
  Chain_Node *iter, *next;
  void *value;
  void (*destructor) (void *);
  POSIX_Keys_Control *the_key;
  Objects_Locations location;

  _Thread_Disable_dispatch();

  chain = &(
      (POSIX_API_Control *)thread->API_Extensions[ THREAD_API_POSIX ]
  )->Key_Chain;
  iter = _Chain_First( chain );
  while ( !_Chain_Is_tail( chain, iter ) ) {
    next = _Chain_Next( iter );
    /**
     * remove key from rbtree and chain.
     * here Chain_Node *iter can be convert to POSIX_Keys_Key_value_pair *,
     * because Chain_Node is the first member of POSIX_Keys_Key_value_pair
     * structure.
     */
    _RBTree_Extract_unprotected(
        &_POSIX_Keys_Key_value_lookup_tree,
        &((POSIX_Keys_Key_value_pair *)iter)->Key_value_lookup_node
    );
    _Chain_Extract_unprotected( iter );

    /**
     * run key value's destructor if destructor and value are both non-null.
     */
    the_key = _POSIX_Keys_Get(
        ((POSIX_Keys_Key_value_pair *)iter)->key,
        &location
    );
    destructor = the_key->destructor;
    value = ((POSIX_Keys_Key_value_pair *)iter)->value;
    if ( destructor != NULL && value != NULL )
      (*destructor)( value );
    /**
     * disable dispatch is nested here
     */
    _Thread_Enable_dispatch();

    /**
     * put back this node to keypool
     */
    _Freechain_Put( &_POSIX_Keys_Keypool.super_fc,
                    (void *)iter );

    iter = next;
  }
  _Thread_Enable_dispatch();
}