pion  5.0.6
include/pion/process.hpp
00001 // ---------------------------------------------------------------------
00002 // pion:  a Boost C++ framework for building lightweight HTTP interfaces
00003 // ---------------------------------------------------------------------
00004 // Copyright (C) 2007-2014 Splunk Inc.  (https://github.com/splunk/pion)
00005 //
00006 // Distributed under the Boost Software License, Version 1.0.
00007 // See http://www.boost.org/LICENSE_1_0.txt
00008 //
00009 
00010 #ifndef __PION_PROCESS_HEADER__
00011 #define __PION_PROCESS_HEADER__
00012 
00013 #include <string>
00014 #include <boost/noncopyable.hpp>
00015 #include <boost/thread/once.hpp>
00016 #include <boost/thread/mutex.hpp>
00017 #include <boost/thread/condition.hpp>
00018 #include <pion/config.hpp>
00019 
00020 // Dump file generation support on Windows
00021 #ifdef _MSC_VER
00022 #include <windows.h>
00023 #include <tchar.h>
00024 #include <DbgHelp.h>
00025 // based on dbghelp.h
00026 typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD dwPid, HANDLE hFile, MINIDUMP_TYPE DumpType,
00027                                     CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
00028                                     CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
00029                                     CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
00030 #endif
00031 
00032 namespace pion {    // begin namespace pion
00033 
00037 class PION_API process :
00038     private boost::noncopyable
00039 {
00040 public:
00041 
00042     // default destructor
00043     ~process() {}
00044     
00046     process(void) {}
00047     
00049     static void shutdown(void);
00050     
00052     static void wait_for_shutdown(void);
00053 
00055     static void initialize(void);
00056     
00058     static void daemonize(void);
00059 
00060 #ifdef _MSC_VER
00061 
00062     class dumpfile_init_exception : public std::exception
00063     {
00064     public:
00065         dumpfile_init_exception(const std::string& cause) : m_cause(cause) {}
00066 
00067         virtual const char* what() const { return m_cause.c_str(); }
00068     protected:
00069         std::string m_cause;
00070     };
00071 
00077     static void set_dumpfile_directory(const std::string& dir);
00078 
00079 protected:
00081     static LONG WINAPI unhandled_exception_filter(struct _EXCEPTION_POINTERS *pExceptionInfo);
00082 
00084     static std::string generate_dumpfile_name();
00085 #endif
00086 
00087 protected:
00088 
00090     struct config_type {
00092 #ifdef _MSC_VER
00093         config_type() : shutdown_now(false), h_dbghelp(NULL), p_dump_proc(NULL) {}
00094 #else
00095         config_type() : shutdown_now(false) {}
00096 #endif
00097     
00099         bool                    shutdown_now;
00100         
00102         boost::condition        shutdown_cond;
00103 
00105         boost::mutex            shutdown_mutex;
00106 
00107 // Dump file generation support on Windows
00108 #ifdef _MSC_VER
00109 
00110         std::string             dumpfile_dir;
00111         
00113         HMODULE                 h_dbghelp;
00114 
00116         MINIDUMPWRITEDUMP       p_dump_proc;
00117 #endif
00118     };
00119 
00121     static inline config_type& get_config(void) {
00122         boost::call_once(process::create_config, m_instance_flag);
00123         return *m_config_ptr;
00124     }
00125 
00126 
00127 private:
00128 
00130     static void create_config(void);
00131 
00132     
00134     static boost::once_flag             m_instance_flag;
00135 
00137     static config_type *          m_config_ptr;
00138 };
00139 
00140 
00141 }   // end namespace pion
00142 
00143 #endif