source: trash/old-modules/transport/protlib/cleanuphandler.h@ 5641

Last change on this file since 5641 was 5641, checked in by Christoph Mayer, 15 years ago
File size: 3.7 KB
Line 
1/// ----------------------------------------*- mode: C++; -*--
2/// @file cleanuphandler.h
3/// preprocessor macros to install cleanup handlers for threads
4/// ----------------------------------------------------------
5/// $Id: cleanuphandler.h 2549 2007-04-02 22:17:37Z bless $
6/// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/include/cleanuphandler.h $
7// ===========================================================
8//
9// Copyright (C) 2005-2007, all rights reserved by
10// - Institute of Telematics, Universitaet Karlsruhe (TH)
11//
12// More information and contact:
13// https://projekte.tm.uka.de/trac/NSIS
14//
15// This program is free software; you can redistribute it and/or modify
16// it under the terms of the GNU General Public License as published by
17// the Free Software Foundation; version 2 of the License
18//
19// This program is distributed in the hope that it will be useful,
20// but WITHOUT ANY WARRANTY; without even the implied warranty of
21// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22// GNU General Public License for more details.
23//
24// You should have received a copy of the GNU General Public License along
25// with this program; if not, write to the Free Software Foundation, Inc.,
26// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27//
28// ===========================================================
29
30/** @ingroup thread
31 *
32 * This header file defines a preprocessor macro to
33 * install cleanup handlers.
34 *
35 * This cannot be done without macros because the pthread library also uses
36 * macros for cleanup.
37 * References to a common base class of all lockable classes also are not
38 * useful because they are casted to void* and cannot be casted back
39 * correctly after that in all cases, especially when pointers are
40 * changed while casting. This happens when casting around in a type
41 * hierarchy.
42 */
43
44#ifndef CLEANUP_HANDLER_H
45#define CLEANUP_HANDLER_H
46
47#include <pthread.h>
48
49namespace protlib {
50
51/** @addtogroup thread Threads
52 * @{
53 */
54
55/// install cleanup handler
56/** This macro installs a cleanup handler
57 * and does some type casting.
58 * Use uninstall_cleanup(execute) or an apropriate unlock routine to unlock
59 * the mutex and uninstall the handler.
60 * @param f pointer to a cleanup handler routine.
61 * This is casted to void (*routine)(void *)
62 * @param m cleanup handler argument, normally a pointer to the mutex.
63 * This is casted to void*.
64 */
65#define install_cleanup(f,m) pthread_cleanup_push((void (*)(void *)) f, (void *) m)
66
67/// install cleanup handler for mutex
68/** Calls install_cleanup and uses pthread_mutex_unlock as cleanup handler.
69 * @param m pointer to the mutex.
70 */
71#define install_cleanup_mutex(m) install_cleanup(pthread_mutex_unlock,m)
72
73/// lock mutex and install cleanup handler
74/** @param m mutex
75 */
76#define install_cleanup_mutex_lock(m) install_cleanup_mutex(m) pthread_mutex_lock(m)
77
78/// Lock thread and install cleanup handler
79/** @param ttype class name of thread object
80 * @param tp pointer to thread object
81 */
82#define install_cleanup_thread_lock(ttype,tp) install_cleanup(call_unlock<ttype>,tp) tp->lock()
83
84/// uninstall cleanup handler
85/** This uninstalls a cleanup handler and optionally executes it.
86 * @param exec 0 or 1
87 */
88#define uninstall_cleanup(exec) pthread_cleanup_pop(exec)
89
90/// unlock template
91/** This function calls the unlock method of an object.
92 * @param pobj pointer to the locked object.
93 */
94template <class T> void call_unlock(void* pobj) {
95 T* t;
96 t = static_cast<T*>(pobj);
97 t->unlock();
98} // end call_unlock<T>
99
100/// call void function
101/** This function calls a function of type void f(void). */
102inline void call_void_fun(void (*f)()) {
103 f();
104} // end call_void_fun
105
106//@}
107
108} // end namespace protlib
109#endif
Note: See TracBrowser for help on using the repository browser.