diff options
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/Makefile.am | 3 | ||||
-rw-r--r-- | src/include/on_exit.h | 48 |
2 files changed, 50 insertions, 1 deletions
diff --git a/src/include/Makefile.am b/src/include/Makefile.am index 26f98a800f3..4e467e9fa8a 100644 --- a/src/include/Makefile.am +++ b/src/include/Makefile.am @@ -80,4 +80,5 @@ noinst_HEADERS += \ include/rbd/librbd.h \ include/rbd/librbd.hpp\ include/util.h\ - include/stat.h + include/stat.h \ + include/on_exit.h diff --git a/src/include/on_exit.h b/src/include/on_exit.h new file mode 100644 index 00000000000..4bb8f2c0687 --- /dev/null +++ b/src/include/on_exit.h @@ -0,0 +1,48 @@ +#ifndef CEPH_ON_EXIT_H +#define CEPH_ON_EXIT_H + +#include <pthread.h> +#include <assert.h> +#include <vector> + +/* + * Create a static instance at the file level to get callbacks called when the + * process exits via main() or exit(). + */ + +class OnExitManager { + public: + typedef void (*callback_t)(void *arg); + + OnExitManager() { + assert(pthread_mutex_init(&lock_, NULL) == 0); + } + + ~OnExitManager() { + pthread_mutex_lock(&lock_); + std::vector<struct cb>::iterator it; + for (it = funcs_.begin(); it != funcs_.end(); it++) { + it->func(it->arg); + } + funcs_.clear(); + pthread_mutex_unlock(&lock_); + } + + void add_callback(callback_t func, void *arg) { + pthread_mutex_lock(&lock_); + struct cb callback = { func, arg }; + funcs_.push_back(callback); + pthread_mutex_unlock(&lock_); + } + + private: + struct cb { + callback_t func; + void *arg; + }; + + std::vector<struct cb> funcs_; + pthread_mutex_t lock_; +}; + +#endif |