1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief POSIX Cleanup Support |
---|
5 | * @ingroup POSIXAPI |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2008. |
---|
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 <pthread.h> |
---|
22 | |
---|
23 | #include <rtems/sysinit.h> |
---|
24 | #include <rtems/score/thread.h> |
---|
25 | #include <rtems/score/threaddispatch.h> |
---|
26 | #include <rtems/score/userextimpl.h> |
---|
27 | |
---|
28 | void _pthread_cleanup_push( |
---|
29 | struct _pthread_cleanup_context *context, |
---|
30 | void ( *routine )( void * ), |
---|
31 | void *arg |
---|
32 | ) |
---|
33 | { |
---|
34 | ISR_Level level; |
---|
35 | Thread_Control *executing; |
---|
36 | |
---|
37 | context->_routine = routine; |
---|
38 | context->_arg = arg; |
---|
39 | |
---|
40 | /* This value is unused, just provide a deterministic value */ |
---|
41 | context->_canceltype = -1; |
---|
42 | |
---|
43 | _ISR_Local_disable( level ); |
---|
44 | |
---|
45 | executing = _Thread_Executing; |
---|
46 | context->_previous = executing->last_cleanup_context; |
---|
47 | executing->last_cleanup_context = context; |
---|
48 | |
---|
49 | _ISR_Local_enable( level ); |
---|
50 | } |
---|
51 | |
---|
52 | void _pthread_cleanup_pop( |
---|
53 | struct _pthread_cleanup_context *context, |
---|
54 | int execute |
---|
55 | ) |
---|
56 | { |
---|
57 | ISR_Level level; |
---|
58 | Thread_Control *executing; |
---|
59 | |
---|
60 | if ( execute != 0 ) { |
---|
61 | ( *context->_routine )( context->_arg ); |
---|
62 | } |
---|
63 | |
---|
64 | _ISR_Local_disable( level ); |
---|
65 | |
---|
66 | executing = _Thread_Executing; |
---|
67 | executing->last_cleanup_context = context->_previous; |
---|
68 | |
---|
69 | _ISR_Local_enable( level ); |
---|
70 | } |
---|
71 | |
---|
72 | static void _POSIX_Cleanup_terminate_extension( Thread_Control *the_thread ) |
---|
73 | { |
---|
74 | struct _pthread_cleanup_context *context; |
---|
75 | |
---|
76 | context = the_thread->last_cleanup_context; |
---|
77 | the_thread->last_cleanup_context = NULL; |
---|
78 | |
---|
79 | while ( context != NULL ) { |
---|
80 | ( *context->_routine )( context->_arg ); |
---|
81 | |
---|
82 | context = context->_previous; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | static void _POSIX_Cleanup_restart_extension( |
---|
87 | Thread_Control *executing, |
---|
88 | Thread_Control *the_thread |
---|
89 | ) |
---|
90 | { |
---|
91 | (void) executing; |
---|
92 | _POSIX_Cleanup_terminate_extension( the_thread ); |
---|
93 | } |
---|
94 | |
---|
95 | static User_extensions_Control _POSIX_Cleanup_extensions = { |
---|
96 | .Callouts = { |
---|
97 | .thread_restart = _POSIX_Cleanup_restart_extension, |
---|
98 | .thread_terminate = _POSIX_Cleanup_terminate_extension |
---|
99 | } |
---|
100 | }; |
---|
101 | |
---|
102 | static void _POSIX_Cleanup_initialization( void ) |
---|
103 | { |
---|
104 | _User_extensions_Add_API_set( &_POSIX_Cleanup_extensions ); |
---|
105 | } |
---|
106 | |
---|
107 | RTEMS_SYSINIT_ITEM( |
---|
108 | _POSIX_Cleanup_initialization, |
---|
109 | RTEMS_SYSINIT_POSIX_CLEANUP, |
---|
110 | RTEMS_SYSINIT_ORDER_MIDDLE |
---|
111 | ); |
---|