source: rtems/cpukit/posix/src/cleanuppop.c @ aaaedba

4.115
Last change on this file since aaaedba was 927a0a1, checked in by Sebastian Huber <sebastian.huber@…>, on 12/02/13 at 07:33:35

posix: Use cleanup contexts on the stack

Provide support for latest Newlib <pthread.h> change. The cleanup
contexts are stored on the thread stack. This is conformant with the
POSIX requirements for the pthread_cleanup_push() and
pthread_cleanup_pop() statement pair.

Passing an invalid pointer as the routine to pthread_cleanup_push() is
now a usage error and the behaviour is undefined.

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/**
2 * @file
3 *
4 * @brief Removes Routine from Top of Calling Thread's stack and Invoke it
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.com/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <pthread.h>
22
23#include <rtems/score/thread.h>
24#include <rtems/score/threaddispatch.h>
25#include <rtems/posix/threadsup.h>
26
27#ifndef HAVE_STRUCT__PTHREAD_CLEANUP_CONTEXT
28
29#include <rtems/score/chainimpl.h>
30#include <rtems/score/isr.h>
31#include <rtems/score/wkspace.h>
32#include <rtems/posix/cancel.h>
33#include <rtems/posix/pthreadimpl.h>
34
35/*
36 *  18.2.3.1 Establishing Cancellation Handlers, P1003.1c/Draft 10, p. 184
37 */
38
39void pthread_cleanup_pop(
40  int    execute
41)
42{
43  POSIX_Cancel_Handler_control      *handler;
44  POSIX_Cancel_Handler_control      tmp_handler;
45  Chain_Control                     *handler_stack;
46  POSIX_API_Control                 *thread_support;
47  ISR_Level                          level;
48
49  thread_support = _Thread_Executing->API_Extensions[ THREAD_API_POSIX ];
50
51  handler_stack = &thread_support->Cancellation_Handlers;
52
53  /*
54   * We need interrupts disabled to safely check the chain and pull
55   * the last element off.  But we also need dispatching disabled to
56   * ensure that we do not get prempted and deleted while we are holding
57   * memory that needs to be freed.
58   */
59
60  _Thread_Disable_dispatch();
61  _ISR_Disable( level );
62
63    if ( _Chain_Is_empty( handler_stack ) ) {
64      _Thread_Enable_dispatch();
65      _ISR_Enable( level );
66      return;
67    }
68
69    handler = (POSIX_Cancel_Handler_control *)
70        _Chain_Tail( handler_stack )->previous;
71    _Chain_Extract_unprotected( &handler->Node );
72
73  _ISR_Enable( level );
74
75  tmp_handler = *handler;
76
77  _Workspace_Free( handler );
78
79  _Thread_Enable_dispatch();
80
81  if ( execute )
82    (*tmp_handler.routine)( tmp_handler.arg );
83}
84
85#else /* HAVE_STRUCT__PTHREAD_CLEANUP_CONTEXT */
86
87void _pthread_cleanup_pop(
88  struct _pthread_cleanup_context *context,
89  int                              execute
90)
91{
92  POSIX_API_Control *thread_support;
93
94  if ( execute != 0 ) {
95    ( *context->_routine )( context->_arg );
96  }
97
98  _Thread_Disable_dispatch();
99
100  thread_support = _Thread_Executing->API_Extensions[ THREAD_API_POSIX ];
101  thread_support->last_cleanup_context = context->_previous;
102
103  _Thread_Enable_dispatch();
104}
105
106#endif /* HAVE_STRUCT__PTHREAD_CLEANUP_CONTEXT */
Note: See TracBrowser for help on using the repository browser.