github.com/searKing/golang/go@v1.2.74/os/signal/cgo/signal_handler_windows.hpp (about) 1 // Copyright (c) 2019 The searKing authors. All Rights Reserved. 2 // 3 // Use of this source code is governed by a MIT-style license 4 // that can be found in the LICENSE file in the root of the source 5 // tree. An additional intellectual property rights grant can be found 6 // in the file PATENTS. All contributing project authors may 7 // be found in the AUTHORS file in the root of the source tree. 8 9 // +build cgo 10 // +build windows 11 12 #ifndef GO_OS_SIGNAL_CGO_SIGNAL_HANDLER_WINDOWS_HPP__ 13 #define GO_OS_SIGNAL_CGO_SIGNAL_HANDLER_WINDOWS_HPP__ 14 15 #include <csignal> 16 #include <functional> 17 #include <map> 18 #include <utility> 19 20 #include "base_signal_handler.hpp" 21 22 namespace searking { 23 24 // Callbacks Predefinations 25 26 typedef void (*SignalHandlerSignalHandler)(int signum); 27 typedef void (*SignalHandlerOnSignal)(void *ctx, int fd, int signum); 28 // Never used this Handler in cgo, for go needs 29 class SignalHandler : public BaseSignalHandler<SignalHandler> { 30 protected: 31 SignalHandler() : on_signal_ctx_(nullptr), on_signal_(nullptr) {} 32 33 void SetGoRegisteredSignalHandlersIfEmpty(int signum, 34 SignalHandlerSignalHandler handler); 35 void DoSignalChan(int signum); 36 void InvokeGoSignalHandler(int signum); 37 38 public: 39 // Thread safe GetInstance. 40 static SignalHandler &GetInstance(); 41 void operator()(int signum); 42 // never invoke a go function, see 43 // https://github.com/golang/go/issues/35814 44 void RegisterOnSignal( 45 std::function<void(void *ctx, int fd, int signum)> callback, void *ctx); 46 47 static int SetSig(int signum); 48 static int SetSig(int signum, SignalHandlerSignalHandler handler); 49 50 private: 51 void *on_signal_ctx_; 52 std::function<void(void *ctx, int fd, int signum)> on_signal_; 53 std::map<int, SignalHandlerSignalHandler> go_registered_handlers_; 54 55 private: 56 SignalHandler(const SignalHandler &) = delete; 57 void operator=(const SignalHandler &) = delete; 58 }; 59 } // namespace searking 60 #endif // GO_OS_SIGNAL_CGO_SIGNAL_HANDLER_WINDOWS_HPP__