# net/utils/ShutdownManager.hpp

    00001 // ------------------------------------------------------------------
    00002 // pion-net: a C++ framework for building lightweight HTTP interfaces
    00003 // ------------------------------------------------------------------
    00004 // Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
    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_SHUTDOWNMANAGER_HEADER__
    00011 #define __PION_SHUTDOWNMANAGER_HEADER__
    00012
    00013 #include <boost/thread/mutex.hpp>
    00014 #include <boost/thread/condition.hpp>
    00015 #ifndef PION_WIN32
    00016     #include <signal.h>
    00017 #endif
    00018
    00019
    00023 class ShutdownManager {
    00024 public:
    00025     // default constructor & destructor
    00026     ShutdownManager(void) : m_shutdown_now(false) {}
    00027     ~ShutdownManager() {}
    00028
    00030     inline void shutdown(void) {
    00031         boost::mutex::scoped_lock shutdown_lock(m_shutdown_mutex);
    00032         m_shutdown_now = true;
    00033         m_shutdown_cond.notify_all();
    00034     }
    00035
    00037     inline void wait(void) {
    00038         boost::mutex::scoped_lock shutdown_lock(m_shutdown_mutex);
    00039         while (! m_shutdown_now)
    00040             m_shutdown_cond.wait(shutdown_lock);
    00041     }
    00042
    00043 private:
    00045     bool                    m_shutdown_now;
    00046
    00048     boost::mutex            m_shutdown_mutex;
    00049
    00051     boost::condition        m_shutdown_cond;
    00052 };
    00053
    00055 static ShutdownManager  main_shutdown_manager;
    00056
    00057
    00059 #ifdef PION_WIN32
    00060 BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
    00061 {
    00062     switch(ctrl_type) {
    00063         case CTRL_C_EVENT:
    00064         case CTRL_BREAK_EVENT:
    00065         case CTRL_CLOSE_EVENT:
    00066         case CTRL_SHUTDOWN_EVENT:
    00067             main_shutdown_manager.shutdown();
    00068             return TRUE;
    00069         default:
    00070             return FALSE;
    00071     }
    00072 }
    00073 #else
    00074 void handle_signal(int sig)
    00075 {
    00076     main_shutdown_manager.shutdown();
    00077 }
    00078 #endif
    00079
    00080
    00081 #endif