Skip to content

Commit

Permalink
Refs #19435. Some refactors on FileWatch:
Browse files Browse the repository at this point in the history
- Namespace moved to eprosima::filewatch
- Constructor receives thread settings
- Copy constructors deleted

Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>
  • Loading branch information
MiguelCompany committed Oct 11, 2023
1 parent a9ed0d9 commit 59b53c0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/cpp/utils/SystemInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ FileWatchHandle SystemInfo::watch_file(
// No-op
break;
}
}));
}, {}, {}));
#else // defined(_WIN32) || defined(__unix__)
static_cast<void>(filename);
static_cast<void>(callback);
Expand Down
60 changes: 31 additions & 29 deletions thirdparty/filewatch/FileWatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#ifndef FILEWATCHER_H
#define FILEWATCHER_H

#include <fastdds/rtps/attributes/ThreadSettings.hpp>

#include <utils/threading.hpp>

#ifdef _WIN32
Expand Down Expand Up @@ -67,6 +69,7 @@
#include <utility>
#include <vector>

namespace eprosima {
namespace filewatch {
enum class Event {
added,
Expand All @@ -93,35 +96,33 @@ namespace filewatch {

public:

FileWatch(T path, UnderpinningRegex pattern, std::function<void(const T& file, const Event event_type)> callback) :
_path(path),
_pattern(pattern),
_callback(callback),
_directory(get_directory(path))
FileWatch(
T path,
UnderpinningRegex pattern,
std::function<void(const T& file, const Event event_type)> callback,
const fastdds::rtps::ThreadSettings& watch_thread_config,
const fastdds::rtps::ThreadSettings& callback_thread_config)
: _path(path)
, _pattern(pattern)
, _callback(callback)
, _directory(get_directory(path))
{
init();
init(watch_thread_config, callback_thread_config);
}

FileWatch(T path, std::function<void(const T& file, const Event event_type)> callback) :
FileWatch<T>(path, UnderpinningRegex(_regex_all), callback) {}
FileWatch(
T path,
std::function<void(const T& file, const Event event_type)> callback,
const fastdds::rtps::ThreadSettings& watch_thread_config,
const fastdds::rtps::ThreadSettings& callback_thread_config)
: FileWatch<T>(path, UnderpinningRegex(_regex_all), callback, watch_thread_config, callback_thread_config) {}

~FileWatch() {
destroy();
}

FileWatch(const FileWatch<T>& other) : FileWatch<T>(other._path, other._callback) {}

FileWatch<T>& operator=(const FileWatch<T>& other)
{
if (this == &other) { return *this; }

destroy();
_path = other._path;
_callback = other._callback;
_directory = get_directory(other._path);
init();
return *this;
}
FileWatch(const FileWatch<T>& other) = delete;
FileWatch<T>& operator=(const FileWatch<T>& other) = delete;

// Const memeber varibles don't let me implent moves nicely, if moves are really wanted std::unique_ptr should be used and move that.
FileWatch<T>(FileWatch<T>&&) = delete;
Expand Down Expand Up @@ -201,36 +202,36 @@ namespace filewatch {
const static std::size_t event_size = (sizeof(struct inotify_event));
#endif // __unix__

void init()
void init(
const fastdds::rtps::ThreadSettings& watch_thread_config = {},
const fastdds::rtps::ThreadSettings& callback_thread_config = {})
{
#ifdef _WIN32
_close_event = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!_close_event) {
throw std::system_error(GetLastError(), std::system_category());
}
#endif // WIN32
_callback_thread = std::move(std::thread([this]() {
_callback_thread = create_thread([this]() {
try {
eprosima::set_name_to_current_thread("dds.fwatch.cb");
callback_thread();
} catch (...) {
try {
_running.set_exception(std::current_exception());
}
catch (...) {} // set_exception() may throw too
}
}));
_watch_thread = std::move(std::thread([this]() {
}, callback_thread_config, "dds.fwatch.cb");
_watch_thread = create_thread([this]() {
try {
eprosima::set_name_to_current_thread("dds.fwatch");
monitor_directory();
} catch (...) {
try {
_running.set_exception(std::current_exception());
}
catch (...) {} // set_exception() may throw too
}
}));
}, watch_thread_config, "dds.fwatch");

std::future<void> future = _running.get_future();
future.get(); //block until the monitor_directory is up and running
Expand Down Expand Up @@ -624,5 +625,6 @@ namespace filewatch {

template<class T> constexpr typename FileWatch<T>::C FileWatch<T>::_regex_all[];
template<class T> constexpr typename FileWatch<T>::C FileWatch<T>::_this_directory[];
}
} // namespace filewatch
} // namespace eprosima
#endif

0 comments on commit 59b53c0

Please sign in to comment.