github.com/searKing/golang/go@v1.2.117/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 //go:build cgo && windows 10 11 #ifndef GO_OS_SIGNAL_CGO_SIGNAL_HANDLER_WINDOWS_HPP__ 12 #define GO_OS_SIGNAL_CGO_SIGNAL_HANDLER_WINDOWS_HPP__ 13 14 #include <csignal> 15 #include <functional> 16 #include <map> 17 #include <utility> 18 19 #include "base_signal_handler.hpp" 20 21 namespace searking { 22 23 // Callbacks Predefinations 24 25 typedef void (*SignalHandlerSignalHandler)(int signum); 26 typedef void (*SignalHandlerOnSignal)(void *ctx, int fd, int signum); 27 // Never used this Handler in cgo, for go needs 28 class SignalHandler : public BaseSignalHandler<SignalHandler> { 29 protected: 30 SignalHandler() : on_signal_ctx_(nullptr), on_signal_(nullptr) {} 31 32 void SetGoRegisteredSignalHandlersIfEmpty(int signum, 33 SignalHandlerSignalHandler handler); 34 void DoSignalChan(int signum); 35 void InvokeGoSignalHandler(int signum); 36 37 public: 38 // Thread safe GetInstance. 39 static SignalHandler &GetInstance(); 40 void operator()(int signum); 41 // never invoke a go function, see 42 // https://github.com/golang/go/issues/35814 43 void RegisterOnSignal( 44 std::function<void(void *ctx, int fd, int signum)> callback, void *ctx); 45 46 static int SetSig(int signum); 47 static int SetSig(int signum, SignalHandlerSignalHandler handler); 48 49 private: 50 void *on_signal_ctx_; 51 std::function<void(void *ctx, int fd, int signum)> on_signal_; 52 std::map<int, SignalHandlerSignalHandler> go_registered_handlers_; 53 54 private: 55 SignalHandler(const SignalHandler &) = delete; 56 void operator=(const SignalHandler &) = delete; 57 }; 58 } // namespace searking 59 #endif // GO_OS_SIGNAL_CGO_SIGNAL_HANDLER_WINDOWS_HPP__