Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / include / on_exit.h
1 #ifndef CEPH_ON_EXIT_H
2 #define CEPH_ON_EXIT_H
3
4 #include <pthread.h>
5 #include <assert.h>
6 #include <vector>
7
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);
19       assert(ret == 0);
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