github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/common/ipc/shm_session.h (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  #pragma once
    21  #include "common/common.h"
    22  #include "common/ipc/shm_base.h"
    23  #include "common/ipc/shm_bookkeeper.h"
    24  #include "common/quitable_thread.h"
    25  #include <atomic>
    26  #include <thread>
    27  
    28  namespace neb {
    29  namespace ipc {
    30  
    31  struct shm_session_failure : public std::exception {
    32    inline shm_session_failure(const std::string &msg) : m_msg(msg) {}
    33    inline const char *what() const throw() { return m_msg.c_str(); }
    34  protected:
    35    std::string m_msg;
    36  };
    37  
    38  struct shm_session_timeout : public shm_session_failure {
    39    inline shm_session_timeout() : shm_session_failure("shm session timeout"){};
    40  };
    41  
    42  struct shm_session_already_start : public shm_session_failure {
    43    inline shm_session_already_start()
    44        : shm_session_failure("shm session already start"){};
    45  };
    46  
    47  void clean_shm_session_env();
    48  
    49  namespace internal {
    50  
    51  class shm_session_base : public quitable_thread {
    52  public:
    53    shm_session_base(const std::string &name);
    54    virtual ~shm_session_base();
    55  
    56    virtual void start_session();
    57  
    58    shm_bookkeeper *bookkeeper() const { return m_bookkeeper.get(); };
    59  
    60    void reset();
    61  
    62  protected:
    63    inline std::string server_session_mutex_name() {
    64      return m_name + ".server.mutex";
    65    }
    66  
    67  protected:
    68    virtual void thread_func() = 0;
    69  
    70    inline std::string server_sema_name() { return m_name + ".server_sema"; }
    71    inline std::string client_sema_name() { return m_name + ".client_sema"; }
    72  
    73  protected:
    74    std::string m_name;
    75    std::unique_ptr<shm_bookkeeper> m_bookkeeper;
    76    std::unique_ptr<boost::interprocess::named_semaphore> m_server_sema;
    77    std::unique_ptr<boost::interprocess::named_semaphore> m_client_sema;
    78  };
    79  class shm_session_util : public shm_session_base {
    80  public:
    81    shm_session_util(const std::string &name);
    82  
    83  protected:
    84    virtual void thread_func();
    85  };
    86  class shm_session_server : public shm_session_base {
    87  public:
    88    shm_session_server(const std::string &name);
    89  
    90    void wait_until_client_start();
    91    bool is_client_alive();
    92  
    93    virtual void start_session();
    94  
    95  protected:
    96    virtual void thread_func();
    97  
    98  protected:
    99    std::atomic_bool m_client_started;
   100    std::atomic_bool m_client_alive;
   101  };
   102  
   103  class shm_session_client : public shm_session_base {
   104  public:
   105    shm_session_client(const std::string &name);
   106  
   107    bool is_server_alive();
   108  
   109  protected:
   110    virtual void thread_func();
   111  
   112  protected:
   113    std::atomic_bool m_server_alive;
   114    };
   115  }
   116  }
   117  }