File: CtrlCHandler.h

package info (click to toggle)
zeroc-ice 3.7.10-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 75,704 kB
  • sloc: cpp: 356,894; java: 226,081; cs: 98,312; javascript: 35,027; python: 28,716; objc: 27,050; php: 7,526; ruby: 7,190; yacc: 2,949; ansic: 2,469; xml: 1,589; lex: 1,241; makefile: 472; sh: 52
file content (92 lines) | stat: -rw-r--r-- 2,797 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

#ifndef ICE_UTIL_CTRL_C_HANDLER_H
#define ICE_UTIL_CTRL_C_HANDLER_H

#include <IceUtil/Config.h>
#include <IceUtil/Exception.h>

namespace IceUtil
{

/**
 * Invoked when a signal occurs. The callback must not raise exceptions.
 * On Linux and macOS, the callback is NOT a signal handler and can call
 * functions that are not async-signal safe.
 * @param sig The signal number that occurred.
 */
#ifdef ICE_CPP11_MAPPING
using CtrlCHandlerCallback = std::function<void(int sig)>;
#else
typedef void (*CtrlCHandlerCallback)(int sig);
#endif

/**
 * Provides a portable way to handle Ctrl-C and Ctrl-C like signals.
 * On Linux and macOS, the CtrlCHandler handles SIGHUP, SIGINT and SIGTERM.
 * On Windows, it is essentially a wrapper for SetConsoleCtrlHandler().
 *
 * \headerfile Ice/Ice.h
 */
class ICE_API CtrlCHandler
{
public:

     /**
     * Registers a callback function that handles Ctrl-C like signals.
     * On Linux and macOS, this constructor masks the SIGHUP, SIGINT and SIGTERM
     * signals and then creates a thread that waits for these signals using sigwait.
     * On Windows, this constructor calls SetConsoleCtrlCHandler to register a handler
     * routine that calls the supplied callback function.
     * Only a single CtrlCHandler object can exist in a process at a give time.
     * @param cb The callback function to invoke when a signal is received.
     */
    explicit CtrlCHandler(CtrlCHandlerCallback cb = ICE_NULLPTR);

     /**
     * Unregisters the callback function.
     * On Linux and macOS, this destructor joins and terminates the thread created
     * by the constructor but does not "unmask" SIGHUP, SIGINT and SIGTERM. As a result,
     * these signals are ignored after this destructor completes.
     * On Windows, this destructor unregisters the SetConsoleCtrlHandler handler
     * routine, and as a result a Ctrl-C or similar signal will terminate the application
     * after this destructor completes.
     */
    ~CtrlCHandler();

    /**
     * Replaces the signal callback.
     * @param cb The new callback.
     * @return The old callback, or nil if no callback is currently set.
     */
    CtrlCHandlerCallback setCallback(CtrlCHandlerCallback cb);

    /**
     * Obtains the signal callback.
     * @return The callback.
     */
    CtrlCHandlerCallback getCallback() const;
};

/**
 * Raised by the CtrlCHandler constructor if another CtrlCHandler already exists.
 *
 * \headerfile Ice/Ice.h
 */
class ICE_API CtrlCHandlerException : public ExceptionHelper<CtrlCHandlerException>
{
public:

    CtrlCHandlerException(const char*, int);
    virtual std::string ice_id() const;

#ifndef ICE_CPP11_MAPPING
    virtual CtrlCHandlerException* ice_clone() const;
#endif
};

}

#endif