-
Notifications
You must be signed in to change notification settings - Fork 167
/
ecal_memfile_pool.h
112 lines (90 loc) 路 3.64 KB
/
ecal_memfile_pool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ========================= eCAL LICENSE =================================
*/
/**
* @brief memory file pool handler
**/
#pragma once
#include <ecal/ecal_event.h>
#include <ecal/ecal_log.h>
#include "ecal_memfile.h"
#include "ecal_memfile_header.h"
#include <atomic>
#include <condition_variable>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <thread>
namespace eCAL
{
using MemFileDataCallbackT = std::function<size_t (const std::string &, const std::string &, const char *, size_t, long long, long long, long long, size_t)>;
////////////////////////////////////////
// CMemFileObserver
////////////////////////////////////////
class CMemFileObserver
{
public:
CMemFileObserver();
~CMemFileObserver();
CMemFileObserver(const CMemFileObserver&) = delete;
CMemFileObserver& operator=(const CMemFileObserver&) = delete;
CMemFileObserver(CMemFileObserver&& rhs) = delete;
CMemFileObserver& operator=(CMemFileObserver&& rhs) = delete;
bool Create(const std::string& memfile_name_, const std::string& memfile_event_);
bool Destroy();
bool Start(const std::string& topic_name_, const std::string& topic_id_, const int timeout_, const MemFileDataCallbackT& callback_);
bool Stop();
bool IsObserving() {return(m_is_observing);};
bool ResetTimeout();
protected:
void Observe(const std::string& topic_name_, const std::string& topic_id_, const int timeout_);
bool ReadFileHeader(SMemFileHeader& memfile_hdr);
std::atomic<bool> m_created;
std::atomic<bool> m_do_stop;
std::atomic<bool> m_is_observing;
std::atomic<std::chrono::steady_clock::time_point> m_time_of_last_life_signal;
MemFileDataCallbackT m_data_callback;
std::thread m_thread;
EventHandleT m_event_snd;
EventHandleT m_event_ack;
CMemoryFile m_memfile;
};
////////////////////////////////////////
// CMemFileThreadPool
////////////////////////////////////////
class CMemFileThreadPool
{
public:
CMemFileThreadPool();
~CMemFileThreadPool();
void Create();
void Destroy();
bool ObserveFile(const std::string& memfile_name_, const std::string& memfile_event_, const std::string& topic_name_, const std::string& topic_id_, int timeout_observation_ms, const MemFileDataCallbackT& callback_);
protected:
void CleanupPoolThread();
void CleanupPool();
std::atomic<bool> m_created;
std::mutex m_observer_pool_sync;
std::map<std::string, std::shared_ptr<CMemFileObserver>> m_observer_pool;
std::atomic<bool> m_do_cleanup;
std::condition_variable m_do_cleanup_cv;
std::mutex m_do_cleanup_mtx;
std::thread m_cleanup_thread;
};
}