source: rtems/cpukit/posix/src/cleanuppop.c @ 927a0a1

4.115
Last change on this file since 927a0a1 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
RevLine 
[1be3fad]1/**
2 * @file
3 *
4 * @brief Removes Routine from Top of Calling Thread's stack and Invoke it
[5cb175bb]5 * @ingroup POSIXAPI
[1be3fad]6 */
7
[db7f70a]8/*
[c3925db]9 *  COPYRIGHT (c) 1989-2008.
[feaa007]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.
[db7f70a]15 */
16
[f42b726]17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
[db7f70a]21#include <pthread.h>
22
[927a0a1]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
[be1b8a7]29#include <rtems/score/chainimpl.h>
[db7f70a]30#include <rtems/score/isr.h>
31#include <rtems/score/wkspace.h>
32#include <rtems/posix/cancel.h>
[0c5317d]33#include <rtems/posix/pthreadimpl.h>
[db7f70a]34
[64adc13]35/*
[db7f70a]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;
[17f1ffb]44  POSIX_Cancel_Handler_control      tmp_handler;
[db7f70a]45  Chain_Control                     *handler_stack;
46  POSIX_API_Control                 *thread_support;
47  ISR_Level                          level;
[874297f3]48
[db7f70a]49  thread_support = _Thread_Executing->API_Extensions[ THREAD_API_POSIX ];
[874297f3]50
[db7f70a]51  handler_stack = &thread_support->Cancellation_Handlers;
52
[c3925db]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();
[db7f70a]61  _ISR_Disable( level );
[c3925db]62
[17f1ffb]63    if ( _Chain_Is_empty( handler_stack ) ) {
[c3925db]64      _Thread_Enable_dispatch();
[17f1ffb]65      _ISR_Enable( level );
66      return;
67    }
[874297f3]68
69    handler = (POSIX_Cancel_Handler_control *)
[e5b78e2]70        _Chain_Tail( handler_stack )->previous;
[db7f70a]71    _Chain_Extract_unprotected( &handler->Node );
[874297f3]72
[db7f70a]73  _ISR_Enable( level );
74
[17f1ffb]75  tmp_handler = *handler;
76
[c3925db]77  _Workspace_Free( handler );
78
[17f1ffb]79  _Thread_Enable_dispatch();
80
[db7f70a]81  if ( execute )
[17f1ffb]82    (*tmp_handler.routine)( tmp_handler.arg );
[db7f70a]83}
[927a0a1]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.