]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #ifndef CEPH_ON_EXIT_H |
2 | #define CEPH_ON_EXIT_H | |
3 | ||
4 | #include <pthread.h> | |
7c673cae FG |
5 | #include <vector> |
6 | ||
11fdf7f2 | 7 | #include "include/ceph_assert.h" |
7c673cae FG |
8 | /* |
9 | * Create a static instance at the file level to get callbacks called when the | |
10 | * process exits via main() or exit(). | |
11 | */ | |
12 | ||
13 | class OnExitManager { | |
14 | public: | |
15 | typedef void (*callback_t)(void *arg); | |
16 | ||
17 | OnExitManager() { | |
18 | int ret = pthread_mutex_init(&lock_, NULL); | |
11fdf7f2 | 19 | ceph_assert(ret == 0); |
7c673cae FG |
20 | } |
21 | ||
22 | ~OnExitManager() { | |
23 | pthread_mutex_lock(&lock_); | |
24 | std::vector<struct cb>::iterator it; | |
25 | for (it = funcs_.begin(); it != funcs_.end(); it++) { | |
26 | it->func(it->arg); | |
27 | } | |
28 | funcs_.clear(); | |
29 | pthread_mutex_unlock(&lock_); | |
30 | } | |
31 | ||
32 | void add_callback(callback_t func, void *arg) { | |
33 | pthread_mutex_lock(&lock_); | |
34 | struct cb callback = { func, arg }; | |
35 | funcs_.push_back(callback); | |
36 | pthread_mutex_unlock(&lock_); | |
37 | } | |
38 | ||
39 | private: | |
40 | struct cb { | |
41 | callback_t func; | |
42 | void *arg; | |
43 | }; | |
44 | ||
45 | std::vector<struct cb> funcs_; | |
46 | pthread_mutex_t lock_; | |
47 | }; | |
48 | ||
49 | #endif |