github.com/searKing/golang/go@v1.2.117/os/signal/cgo/signal_handler_unix.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 && unix && !linux 10 11 #ifndef GO_OS_SIGNAL_CGO_SIGNAL_HANDLER_UNIX_HPP_ 12 #define GO_OS_SIGNAL_CGO_SIGNAL_HANDLER_UNIX_HPP_ 13 14 #include <unistd.h> 15 // You can find out the version with _POSIX_VERSION. 16 // POSIX compliant 17 18 #include <csignal> 19 #include <functional> 20 #include <map> 21 #include <memory> 22 #include <utility> 23 24 #include "base_signal_handler.hpp" 25 26 namespace searking { 27 28 // Callbacks Predefinations 29 30 typedef void (*SignalHandlerSigActionHandler)(int signum, siginfo_t *info, 31 void *context); 32 typedef void (*SignalHandlerSignalHandler)(int signum); 33 typedef void (*SignalHandlerOnSignal)(void *ctx, int fd, int signum, 34 siginfo_t *info, void *context); 35 36 class SignalHandler : public BaseSignalHandler<SignalHandler> { 37 protected: 38 SignalHandler() : on_signal_ctx_(nullptr), on_signal_(nullptr) {} 39 40 void SetGoRegisteredSignalHandlersIfEmpty( 41 int signum, SignalHandlerSigActionHandler action, 42 SignalHandlerSignalHandler handler); 43 void DoSignalChan(int signum, siginfo_t *info, void *context); 44 void InvokeGoSignalHandler(int signum, siginfo_t *info, void *context); 45 46 public: 47 // Thread safe GetInstance. 48 static SignalHandler &GetInstance(); 49 50 void operator()(int signum, siginfo_t *info, void *context); 51 // never invoke a go function, see 52 // https://github.com/golang/go/issues/35814 53 void RegisterOnSignal(std::function<void(void *ctx, int fd, int signum, 54 siginfo_t *info, void *context)> 55 callback, 56 void *ctx); 57 58 static int SetSig(int signum); 59 static int SetSig(int signum, SignalHandlerSigActionHandler action, 60 SignalHandlerSignalHandler handler); 61 62 private: 63 void *on_signal_ctx_; 64 std::function<void(void *ctx, int fd, int signum, siginfo_t *info, 65 void *context)> 66 on_signal_; 67 std::map<int, std::pair<SignalHandlerSigActionHandler, 68 SignalHandlerSignalHandler> > 69 go_registered_handlers_; 70 }; 71 } // namespace searking 72 #endif