github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/common/ipc/shm_service_op_queue.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/wakeable_queue.h"
    24  
    25  namespace neb {
    26  namespace ipc {
    27  namespace internal {
    28  
    29  class shm_service_op_base {
    30  public:
    31    enum shm_service_op_id {
    32      op_allocate_obj,
    33      op_recv_obj,
    34      op_push_back,
    35      op_destroy,
    36      op_general,
    37    };
    38    inline shm_service_op_base(shm_service_op_id op_id) : m_op_id(op_id) {}
    39  
    40    inline shm_service_op_id op_id() const { return m_op_id; }
    41  
    42  protected:
    43    shm_service_op_id m_op_id;
    44  };
    45  
    46  class shm_service_op_allocate : public shm_service_op_base {
    47  public:
    48    inline shm_service_op_allocate(uint64_t counter,
    49                                   const std::function<void *()> &func)
    50        : shm_service_op_base(op_allocate_obj), m_counter(counter), m_func(func) {
    51    }
    52  
    53    uint64_t m_counter;
    54    std::function<void *()> m_func;
    55    void *m_ret;
    56  };
    57  
    58  class shm_service_op_recv : public shm_service_op_base {
    59  public:
    60    inline shm_service_op_recv() : shm_service_op_base(op_recv_obj) {}
    61    void *m_pointer;
    62    shm_type_id_t m_type_id;
    63  };
    64  
    65  class shm_service_op_push_back : public shm_service_op_base {
    66  public:
    67    inline shm_service_op_push_back() : shm_service_op_base(op_push_back) {}
    68    void *m_pointer;
    69    shm_type_id_t m_type_id;
    70  };
    71  class shm_service_op_destroy : public shm_service_op_base {
    72  public:
    73    template <typename T>
    74    shm_service_op_destroy(boost::interprocess::managed_shared_memory *shmem,
    75                           T *pointer)
    76        : shm_service_op_base(op_destroy) {
    77      m_func = [shmem, pointer]() { shmem->destroy_ptr(pointer); };
    78    }
    79    std::function<void()> m_func;
    80  };
    81  
    82  class shm_service_op_general : public shm_service_op_base {
    83  public:
    84    template <typename T>
    85    shm_service_op_general(T &&f) : shm_service_op_base(op_general), m_func(f) {}
    86    std::function<void()> m_func;
    87  };
    88  
    89  using shm_service_op_queue =
    90      wakeable_queue<std::shared_ptr<shm_service_op_base>>;
    91  } // namespace internal
    92  } // namespace ipc
    93  } // namespace neb