1 /** 2 * Copyright 2020 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CCSRC_CXXAPI_MULTI_PROCESS_H 18 #define MINDSPORE_CCSRC_CXXAPI_MULTI_PROCESS_H 19 #include <iostream> 20 #include <functional> 21 #include "include/api/status.h" 22 23 namespace mindspore { 24 struct MessageFlag { 25 uint64_t heartbeat = 0; 26 uint64_t stop = false; 27 uint64_t msg_len = 0; 28 uint64_t msg_total_len = 0; 29 uint64_t read_ready_flag = false; 30 uint64_t read_finish_flag = false; 31 }; 32 33 class MultiProcess; 34 using ProcessFuncCall = std::function<Status(MultiProcess *multi_process)>; 35 using CreateBufferCall = std::function<uint8_t *(size_t msg_len)>; 36 37 class MultiProcess { 38 public: 39 MultiProcess(); 40 ~MultiProcess(); 41 42 Status MainProcess(const ProcessFuncCall &parent_process, const ProcessFuncCall &child_process); 43 Status SendMsg(const void *buffer, uint64_t msg_len); 44 Status ReceiveMsg(const CreateBufferCall &create_buffer_call) const; 45 46 private: 47 uint8_t *shmat_addr_ = nullptr; 48 uint8_t *shmat_data_addr_ = nullptr; 49 uint64_t shmat_data_max_size_ = 0; 50 uint64_t memory_size_ = 0; 51 52 bool peer_stopped_ = false; 53 bool stopped_ = false; 54 MessageFlag *send_msg_ = nullptr; 55 MessageFlag *receive_msg_ = nullptr; 56 57 static void HeartbeatThreadFunc(MultiProcess *multi_process); 58 void HeartbeatThreadFuncInner(); 59 Status ParentProcess(const ProcessFuncCall &parent_process); 60 void ChildProcess(const ProcessFuncCall &child_process); 61 }; 62 } // namespace mindspore 63 #endif // MINDSPORE_CCSRC_CXXAPI_MULTI_PROCESS_H 64