pion-net  4.0.9
ShutdownManager.hpp
1 // ------------------------------------------------------------------
2 // pion-net: a C++ framework for building lightweight HTTP interfaces
3 // ------------------------------------------------------------------
4 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com)
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_SHUTDOWNMANAGER_HEADER__
11 #define __PION_SHUTDOWNMANAGER_HEADER__
12 
13 #include <boost/thread/mutex.hpp>
14 #include <boost/thread/condition.hpp>
15 #ifndef PION_WIN32
16  #include <signal.h>
17 #endif
18 
19 
24 public:
25  // default constructor & destructor
26  ShutdownManager(void) : m_shutdown_now(false) {}
27  ~ShutdownManager() {}
28 
30  inline void shutdown(void) {
31  boost::mutex::scoped_lock shutdown_lock(m_shutdown_mutex);
32  m_shutdown_now = true;
33  m_shutdown_cond.notify_all();
34  }
35 
37  inline void wait(void) {
38  boost::mutex::scoped_lock shutdown_lock(m_shutdown_mutex);
39  while (! m_shutdown_now)
40  m_shutdown_cond.wait(shutdown_lock);
41  }
42 
43 private:
45  bool m_shutdown_now;
46 
48  boost::mutex m_shutdown_mutex;
49 
51  boost::condition m_shutdown_cond;
52 };
53 
55 static ShutdownManager main_shutdown_manager;
56 
57 
59 #ifdef PION_WIN32
60 BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
61 {
62  switch(ctrl_type) {
63  case CTRL_C_EVENT:
64  case CTRL_BREAK_EVENT:
65  case CTRL_CLOSE_EVENT:
66  case CTRL_SHUTDOWN_EVENT:
67  main_shutdown_manager.shutdown();
68  return TRUE;
69  default:
70  return FALSE;
71  }
72 }
73 #else
74 void handle_signal(int sig)
75 {
76  main_shutdown_manager.shutdown();
77 }
78 #endif
79 
80 
81 #endif
void wait(void)
blocks until the shutdown condition has been signaled
void shutdown(void)
signals the shutdown condition