pion  5.0.6
process.hpp
1 // ---------------------------------------------------------------------
2 // pion: a Boost C++ framework for building lightweight HTTP interfaces
3 // ---------------------------------------------------------------------
4 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_PROCESS_HEADER__
11 #define __PION_PROCESS_HEADER__
12 
13 #include <string>
14 #include <boost/noncopyable.hpp>
15 #include <boost/thread/once.hpp>
16 #include <boost/thread/mutex.hpp>
17 #include <boost/thread/condition.hpp>
18 #include <pion/config.hpp>
19 
20 // Dump file generation support on Windows
21 #ifdef _MSC_VER
22 #include <windows.h>
23 #include <tchar.h>
24 #include <DbgHelp.h>
25 // based on dbghelp.h
26 typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD dwPid, HANDLE hFile, MINIDUMP_TYPE DumpType,
27  CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
28  CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
29  CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
30 #endif
31 
32 namespace pion { // begin namespace pion
33 
37 class PION_API process :
38  private boost::noncopyable
39 {
40 public:
41 
42  // default destructor
43  ~process() {}
44 
46  process(void) {}
47 
49  static void shutdown(void);
50 
52  static void wait_for_shutdown(void);
53 
55  static void initialize(void);
56 
58  static void daemonize(void);
59 
60 #ifdef _MSC_VER
61 
62  class dumpfile_init_exception : public std::exception
63  {
64  public:
65  dumpfile_init_exception(const std::string& cause) : m_cause(cause) {}
66 
67  virtual const char* what() const { return m_cause.c_str(); }
68  protected:
69  std::string m_cause;
70  };
71 
77  static void set_dumpfile_directory(const std::string& dir);
78 
79 protected:
81  static LONG WINAPI unhandled_exception_filter(struct _EXCEPTION_POINTERS *pExceptionInfo);
82 
84  static std::string generate_dumpfile_name();
85 #endif
86 
87 protected:
88 
90  struct config_type {
92 #ifdef _MSC_VER
93  config_type() : shutdown_now(false), h_dbghelp(NULL), p_dump_proc(NULL) {}
94 #else
95  config_type() : shutdown_now(false) {}
96 #endif
97 
100 
102  boost::condition shutdown_cond;
103 
105  boost::mutex shutdown_mutex;
106 
107 // Dump file generation support on Windows
108 #ifdef _MSC_VER
109  std::string dumpfile_dir;
111 
113  HMODULE h_dbghelp;
114 
116  MINIDUMPWRITEDUMP p_dump_proc;
117 #endif
118  };
119 
121  static inline config_type& get_config(void) {
122  boost::call_once(process::create_config, m_instance_flag);
123  return *m_config_ptr;
124  }
125 
126 
127 private:
128 
130  static void create_config(void);
131 
132 
134  static boost::once_flag m_instance_flag;
135 
137  static config_type * m_config_ptr;
138 };
139 
140 
141 } // end namespace pion
142 
143 #endif
bool shutdown_now
true if we should shutdown now
Definition: process.hpp:99
config_type()
constructor just initializes native types
Definition: process.hpp:95
data type for static/global process configuration information
Definition: process.hpp:90
process(void)
default constructor
Definition: process.hpp:46
boost::mutex shutdown_mutex
used to protect the shutdown condition
Definition: process.hpp:105
boost::condition shutdown_cond
triggered when it is time to shutdown
Definition: process.hpp:102
static config_type & get_config(void)
returns a singleton instance of config_type
Definition: process.hpp:121