Apollo  6.0
Open source self driving car software
segment.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
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 CYBER_TRANSPORT_SHM_SEGMENT_H_
18 #define CYBER_TRANSPORT_SHM_SEGMENT_H_
19 
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <unordered_map>
24 
28 
29 namespace apollo {
30 namespace cyber {
31 namespace transport {
32 
33 class Segment;
34 using SegmentPtr = std::shared_ptr<Segment>;
35 
36 struct WritableBlock {
37  uint32_t index = 0;
38  Block* block = nullptr;
39  uint8_t* buf = nullptr;
40 };
42 
43 class Segment {
44  public:
45  explicit Segment(uint64_t channel_id);
46  virtual ~Segment() {}
47 
48  bool AcquireBlockToWrite(std::size_t msg_size, WritableBlock* writable_block);
49  void ReleaseWrittenBlock(const WritableBlock& writable_block);
50 
51  bool AcquireBlockToRead(ReadableBlock* readable_block);
52  void ReleaseReadBlock(const ReadableBlock& readable_block);
53 
54  protected:
55  virtual bool Destroy();
56  virtual void Reset() = 0;
57  virtual bool Remove() = 0;
58  virtual bool OpenOnly() = 0;
59  virtual bool OpenOrCreate() = 0;
60 
61  bool init_;
63  uint64_t channel_id_;
64 
67  void* managed_shm_;
68  std::mutex block_buf_lock_;
69  std::unordered_map<uint32_t, uint8_t*> block_buf_addrs_;
70 
71  private:
72  bool Remap();
73  bool Recreate(const uint64_t& msg_size);
74  uint32_t GetNextWritableBlockIndex();
75 };
76 
77 } // namespace transport
78 } // namespace cyber
79 } // namespace apollo
80 
81 #endif // CYBER_TRANSPORT_SHM_SEGMENT_H_
uint64_t channel_id_
Definition: segment.h:63
void * managed_shm_
Definition: segment.h:67
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
State * state_
Definition: segment.h:65
Block * blocks_
Definition: segment.h:66
uint8_t * buf
Definition: segment.h:39
ShmConf conf_
Definition: segment.h:62
std::shared_ptr< Segment > SegmentPtr
Definition: segment.h:34
Definition: state.h:29
Definition: segment.h:43
bool init_
Definition: segment.h:61
std::unordered_map< uint32_t, uint8_t * > block_buf_addrs_
Definition: segment.h:69
Definition: block.h:27
virtual ~Segment()
Definition: segment.h:46
uint32_t index
Definition: segment.h:37
Definition: shm_conf.h:27
Block * block
Definition: segment.h:38
std::mutex block_buf_lock_
Definition: segment.h:68