SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CleanupManager.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 
31 /*
32  *@file CleanupManager.cc
33  *@brief Manage exitAll callbacks.
34  *
35  *@author
36  * Michael Callahan
37  * Department of Computer Science
38  * University of Utah
39  *@date November 2004
40  *
41  */
42 
43 /*
44  *@details
45  * How to use this class:
46  *
47  * #include <Core/Thread/Legacy/Legacy/CleanupManager.h>
48  *
49  * Make a callback function that takes zero arguements and returns
50  * void. Be very careful about introducing crashes into this
51  * callback function as it then becomes difficult to exit from
52  * scirun. It's recommended that you avoid calling new from
53  * within your callback.
54  *
55  * Register with CleanupManager::add_callback(YOUR_CALLBACK_HERE, MISC_DATA);
56  *
57  * Your callback will only ever be called once, no matter how many
58  * times you register it. In addition you can unregister it or
59  *
60  * design it such that it doesn't do anything if it doesn't need to.
61  * Here is an example of how this could be used:
62  */
63 // class Myclass {
64 // public:
65 // Myclass() {
66 // CleanupManager::add_callback(this->cleanup_wrap, this);
67 // }
68 //
69 // ~Myclass() {
70 // // Need to remove and call callback at same time or else you get
71 // // a race condition and the callback could be called twice.
72 // CleanupManger::invoke_remove_callback(this->cleanup_wrap, this);
73 // }
74 //
75 // private:
76 // static void cleanup_wrap(void *ptr) {
77 // ((Myclass *)ptr)->cleanup();
78 // }
79 //
80 // void cleanup();
81 // }
82 //
83 
84 #ifndef SCI_project_CleanupManager_h
85 #define SCI_project_CleanupManager_h 1
86 
88 #include <vector>
89 
91 
92 namespace SCIRun {
93 
94 typedef void (*CleanupManagerCallback)(void *);
95 
97 public:
98 
99  // Initializes the mutex lock for the cleanup manager. Initialize
100  // is called from the Thread::initialize and is only called once.
101  static void initialize();
102 
103  static void add_callback(CleanupManagerCallback cb, void *data);
104  static void invoke_remove_callback(CleanupManagerCallback cb, void *data);
105  static void remove_callback(CleanupManagerCallback cb, void *data);
106 
107  // After calling this, all registered functions will be removed.
108  // Should this also delete all the memory allocated by initialize()?
109  static void call_callbacks();
110 
111 protected:
112  // callbacks_ needs to be allocated off the heap to make sure it's
113  // still around when exitAll is called.
114  static std::vector<std::pair<CleanupManagerCallback, void *> >* callbacks_;
115  static bool initialized_;
116  static Mutex * lock_;
117 };
118 
119 } // End namespace SCIRun
120 
121 
122 #endif /* SCI_project_CleanupManager_h */
static Mutex * lock_
Definition: CleanupManager.h:116
#define SCISHARE
Definition: share.h:39
static std::vector< std::pair< CleanupManagerCallback, void * > > * callbacks_
Definition: CleanupManager.h:114
Definition: Mutex.h:65
static bool initialized_
Definition: CleanupManager.h:115
dictionary data
Definition: eabLatVolData.py:11
Definition: CleanupManager.h:96
void(* CleanupManagerCallback)(void *)
Definition: CleanupManager.h:94