Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / libraries / third_party / pthreads-win32 / cleanup.c
blob381d1e87c83250cdc9a4de1128ae2af26a93f599
1 /*
2 * cleanup.c
4 * Description:
5 * This translation unit implements routines associated
6 * with cleaning up threads.
9 * --------------------------------------------------------------------------
11 * Pthreads-win32 - POSIX Threads Library for Win32
12 * Copyright(C) 1998 John E. Bossom
13 * Copyright(C) 1999,2005 Pthreads-win32 contributors
15 * Contact Email: rpj@callisto.canberra.edu.au
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
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.
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.
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
39 #include "pthread.h"
40 #include "implement.h"
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.
49 ptw32_cleanup_t *
50 ptw32_pop_cleanup (int execute)
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.
58 * PARAMETERS
59 * execute
60 * if nonzero, execute the cleanup handler
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.
70 * RESULTS
71 * N/A
73 * ------------------------------------------------------
76 ptw32_cleanup_t *cleanup;
78 cleanup = (ptw32_cleanup_t *) pthread_getspecific (ptw32_cleanupKey);
80 if (cleanup != NULL)
82 if (execute && (cleanup->routine != NULL))
85 (*cleanup->routine) (cleanup->arg);
89 pthread_setspecific (ptw32_cleanupKey, (void *) cleanup->prev);
93 return (cleanup);
95 } /* ptw32_pop_cleanup */
98 void
99 ptw32_push_cleanup (ptw32_cleanup_t * cleanup,
100 ptw32_cleanup_callback_t routine, void *arg)
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
112 * PARAMETERS
113 * cleanup
114 * a pointer to an instance of pthread_cleanup_t,
116 * routine
117 * pointer to a cleanup handler,
119 * arg
120 * parameter to be passed to the cleanup handler
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.
134 * RESULTS
135 * pthread_cleanup_t *
136 * pointer to the previous cleanup
138 * ------------------------------------------------------
141 cleanup->routine = routine;
142 cleanup->arg = arg;
144 cleanup->prev = (ptw32_cleanup_t *) pthread_getspecific (ptw32_cleanupKey);
146 pthread_setspecific (ptw32_cleanupKey, (void *) cleanup);
148 } /* ptw32_push_cleanup */