1 /*
2  * cleanup.c
3  *
4  * Description:
5  * This translation unit implements routines associated
6  * with cleaning up threads.
7  *
8  *
9  * --------------------------------------------------------------------------
10  *
11  *      Pthreads-win32 - POSIX Threads Library for Win32
12  *      Copyright(C) 1998 John E. Bossom
13  *      Copyright(C) 1999,2005 Pthreads-win32 contributors
14  *
15  *      Contact Email: rpj@callisto.canberra.edu.au
16  *
17  *      The current list of contributors is contained
18  *      in the file CONTRIBUTORS included with the source
19  *      code distribution. The list can also be seen at the
20  *      following World Wide Web location:
21  *      http://sources.redhat.com/pthreads-win32/contributors.html
22  *
23  *      This library is free software; you can redistribute it and/or
24  *      modify it under the terms of the GNU Lesser General Public
25  *      License as published by the Free Software Foundation; either
26  *      version 2 of the License, or (at your option) any later version.
27  *
28  *      This library is distributed in the hope that it will be useful,
29  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
30  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
31  *      Lesser General Public License for more details.
32  *
33  *      You should have received a copy of the GNU Lesser General Public
34  *      License along with this library in the file COPYING.LIB;
35  *      if not, write to the Free Software Foundation, Inc.,
36  *      51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
37  */
38 
39 #include "pthread.h"
40 #include "implement.h"
41 
42 
43 /*
44  * The functions ptw32_pop_cleanup and ptw32_push_cleanup
45  * are implemented here for applications written in C with no
46  * SEH or C++ destructor support.
47  */
48 
49 ptw32_cleanup_t *
ptw32_pop_cleanup(int execute)50 ptw32_pop_cleanup (int execute)
51      /*
52       * ------------------------------------------------------
53       * DOCPUBLIC
54       *      This function pops the most recently pushed cleanup
55       *      handler. If execute is nonzero, then the cleanup handler
56       *      is executed if non-null.
57       *
58       * PARAMETERS
59       *      execute
60       *              if nonzero, execute the cleanup handler
61       *
62       *
63       * DESCRIPTION
64       *      This function pops the most recently pushed cleanup
65       *      handler. If execute is nonzero, then the cleanup handler
66       *      is executed if non-null.
67       *      NOTE: specify 'execute' as nonzero to avoid duplication
68       *                of common cleanup code.
69       *
70       * RESULTS
71       *              N/A
72       *
73       * ------------------------------------------------------
74       */
75 {
76   ptw32_cleanup_t *cleanup;
77 
78   cleanup = (ptw32_cleanup_t *) pthread_getspecific (ptw32_cleanupKey);
79 
80   if (cleanup != NULL)
81     {
82       if (execute && (cleanup->routine != NULL))
83 	{
84 
85 	  (*cleanup->routine) (cleanup->arg);
86 
87 	}
88 
89       pthread_setspecific (ptw32_cleanupKey, (void *) cleanup->prev);
90 
91     }
92 
93   return (cleanup);
94 
95 }				/* ptw32_pop_cleanup */
96 
97 
98 void
ptw32_push_cleanup(ptw32_cleanup_t * cleanup,ptw32_cleanup_callback_t routine,void * arg)99 ptw32_push_cleanup (ptw32_cleanup_t * cleanup,
100 		    ptw32_cleanup_callback_t routine, void *arg)
101      /*
102       * ------------------------------------------------------
103       * DOCPUBLIC
104       *      This function pushes a new cleanup handler onto the thread's stack
105       *      of cleanup handlers. Each cleanup handler pushed onto the stack is
106       *      popped and invoked with the argument 'arg' when
107       *              a) the thread exits by calling 'pthread_exit',
108       *              b) when the thread acts on a cancellation request,
109       *              c) or when the thread calls pthread_cleanup_pop with a nonzero
110       *                 'execute' argument
111       *
112       * PARAMETERS
113       *      cleanup
114       *              a pointer to an instance of pthread_cleanup_t,
115       *
116       *      routine
117       *              pointer to a cleanup handler,
118       *
119       *      arg
120       *              parameter to be passed to the cleanup handler
121       *
122       *
123       * DESCRIPTION
124       *      This function pushes a new cleanup handler onto the thread's stack
125       *      of cleanup handlers. Each cleanup handler pushed onto the stack is
126       *      popped and invoked with the argument 'arg' when
127       *              a) the thread exits by calling 'pthread_exit',
128       *              b) when the thread acts on a cancellation request,
129       *              c) or when the thrad calls pthread_cleanup_pop with a nonzero
130       *                 'execute' argument
131       *      NOTE: pthread_push_cleanup, ptw32_pop_cleanup must be paired
132       *                in the same lexical scope.
133       *
134       * RESULTS
135       *              pthread_cleanup_t *
136       *                              pointer to the previous cleanup
137       *
138       * ------------------------------------------------------
139       */
140 {
141   cleanup->routine = routine;
142   cleanup->arg = arg;
143 
144   cleanup->prev = (ptw32_cleanup_t *) pthread_getspecific (ptw32_cleanupKey);
145 
146   pthread_setspecific (ptw32_cleanupKey, (void *) cleanup);
147 
148 }				/* ptw32_push_cleanup */
149