Skip to content

Commit 712ebb9

Browse files
author
Will
committed
adapter/darwin: simplify sync routines
1 parent badd869 commit 712ebb9

File tree

7 files changed

+110
-231
lines changed

7 files changed

+110
-231
lines changed

devel/include/detail/wtr/watcher/adapter/darwin/watch.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <CoreServices/CoreServices.h>
88
#include <dispatch/dispatch.h>
99
#include <filesystem>
10+
#include <mutex>
1011
#include <string>
1112
#include <unordered_set>
1213

@@ -68,7 +69,7 @@ struct ContextData {
6869
::wtr::watcher::event::callback const& callback{};
6970
pathset* seen_created_paths{nullptr};
7071
fspath* last_rename_path{nullptr};
71-
Mutex* in_use{nullptr};
72+
std::mutex* mtx{nullptr};
7273
};
7374

7475
/* We make a path from a C string...
@@ -244,13 +245,13 @@ inline auto event_recv(
244245
&& maybe_ctx; /* Once in a blue moon, this doesn't exist */
245246
if (! data_ok) return;
246247
auto ctx = *static_cast<ContextData*>(maybe_ctx);
247-
auto synced = ctx.in_use->try_sync();
248-
if (! synced) return;
248+
if (! ctx.mtx->try_lock()) return;
249249
for (unsigned long i = 0; i < count; i++) {
250250
auto path = path_from_event_at(paths, i);
251251
auto flag = flags[i];
252252
event_recv_one(ctx, path, flag);
253253
}
254+
ctx.mtx->unlock();
254255
}
255256

256257
static_assert(event_recv == FSEventStreamCallback{event_recv});
@@ -381,11 +382,11 @@ inline auto wait(semabin const& sb)
381382
produces a warning. Releasing seems safer than not, so
382383
we'll do that.
383384
*/
384-
inline auto close_event_stream(FSEventStreamRef stream, ContextData& ctx)
385-
-> bool
385+
inline auto
386+
close_event_stream(FSEventStreamRef stream, ContextData& ctx) -> bool
386387
{
387388
if (! stream) return false;
388-
auto _ = ctx.in_use->eventually_sync();
389+
auto _ = std::scoped_lock{*ctx.mtx};
389390
FSEventStreamFlushSync(stream);
390391
FSEventStreamStop(stream);
391392
FSEventStreamInvalidate(stream);
@@ -413,8 +414,8 @@ inline auto watch(
413414
static auto queue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
414415
auto seen_created_paths = ContextData::pathset{};
415416
auto last_rename_path = ContextData::fspath{};
416-
auto in_use = Mutex{};
417-
auto ctx = ContextData{cb, &seen_created_paths, &last_rename_path, &in_use};
417+
auto mtx = std::mutex{};
418+
auto ctx = ContextData{cb, &seen_created_paths, &last_rename_path, &mtx};
418419

419420
auto fsevs = open_event_stream(path, queue, &ctx);
420421

devel/include/detail/wtr/watcher/adapter/linux/fanotify/watch.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ inline auto make_sysres = [](
141141
character string to the event's directory entry
142142
after the file handle to the directory.
143143
Confusing, right? */
144-
inline auto pathof(fanotify_event_metadata const* const mtd, int* ec)
145-
-> std::string
144+
inline auto
145+
pathof(fanotify_event_metadata const* const mtd, int* ec) -> std::string
146146
{
147147
constexpr size_t path_ulim = PATH_MAX - sizeof('\0');
148148
auto dir_info = (fanotify_event_info_fid*)(mtd + 1);
@@ -213,9 +213,8 @@ parse_ev(fanotify_event_metadata const* const m, size_t read_len, int* ec)
213213
: ev_et::other;
214214
auto isfromto = [et](unsigned a, unsigned b) -> bool
215215
{ return et == ev_et::rename && a & FAN_MOVED_FROM && b & FAN_MOVED_TO; };
216-
auto one = [&](auto* m) -> Parsed {
217-
return {ev(pathof(m, ec), et, pt), n, m->event_len};
218-
};
216+
auto one = [&](auto* m) -> Parsed
217+
{ return {ev(pathof(m, ec), et, pt), n, m->event_len}; };
219218
auto assoc = [&](auto* m, auto* n) -> Parsed
220219
{
221220
auto nn = peek(n, read_len);

devel/include/detail/wtr/watcher/adapter/linux/inotify/watch.hpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ inline auto make_sysres = [](
164164
};
165165
};
166166

167-
inline auto
168-
peek(inotify_event const* const in_ev, inotify_event const* const ev_tail)
169-
-> inotify_event*
167+
inline auto peek(
168+
inotify_event const* const in_ev,
169+
inotify_event const* const ev_tail) -> inotify_event*
170170
{
171171
auto len_to_next = sizeof(inotify_event) + (in_ev ? in_ev->len : 0);
172172
auto next = (inotify_event*)((char*)in_ev + len_to_next);
@@ -198,12 +198,10 @@ inline auto parse_ev(
198198
{ return b && b->cookie && b->cookie == a->cookie && et == ev_et::rename; };
199199
auto isfromto = [](auto* a, auto* b) -> bool
200200
{ return (a->mask & IN_MOVED_FROM) && (b->mask & IN_MOVED_TO); };
201-
auto one = [&](auto* a, auto* next) -> parsed {
202-
return {ev(pathof(a), et, pt), next};
203-
};
204-
auto assoc = [&](auto* a, auto* b) -> parsed {
205-
return {ev(ev(pathof(a), et, pt), ev(pathof(b), et, pt)), peek(b, tail)};
206-
};
201+
auto one = [&](auto* a, auto* next) -> parsed
202+
{ return {ev(pathof(a), et, pt), next}; };
203+
auto assoc = [&](auto* a, auto* b) -> parsed
204+
{ return {ev(ev(pathof(a), et, pt), ev(pathof(b), et, pt)), peek(b, tail)}; };
207205
auto next = peek(in, tail);
208206

209207
return ! isassoc(in, next) ? one(in, next)

devel/include/test_watcher/event.hpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ template<class T>
2828
inline auto to(::wtr::watcher::event_without_time const& from) noexcept -> T;
2929

3030
template<>
31-
inline auto
32-
to<std::string>(::wtr::watcher::event_without_time const& from) noexcept
33-
-> std::string
31+
inline auto to<std::string>(
32+
::wtr::watcher::event_without_time const& from) noexcept -> std::string
3433
{
3534
using wtr::to, std::string;
3635
auto&& fields = "\n path_name: " + to<string>(from.path_name)
@@ -43,9 +42,8 @@ to<std::string>(::wtr::watcher::event_without_time const& from) noexcept
4342

4443
template<>
4544
struct std::hash<wtr::watcher::event_without_time> {
46-
inline auto
47-
operator()(wtr::watcher::event_without_time const& ev) const noexcept
48-
-> std::size_t
45+
inline auto operator()(
46+
wtr::watcher::event_without_time const& ev) const noexcept -> std::size_t
4947
{
5048
return std::hash<decltype(ev.path_name.string())>{}(ev.path_name.string())
5149
#ifdef _WIN32
@@ -141,20 +139,20 @@ check_event_lists_eq(auto const& event_sent_list, auto const& event_recv_list)
141139
if (event_sent_list[i].path_type != wtr::event::path_type::watcher) {
142140
if (event_sent_list[i].path_name != event_recv_list[i].path_name)
143141
std::cerr << "Bad .path_name field... (" << event_sent_list[i].path_name
144-
<< " vs " << event_recv_list[i].path_name << "):"
145-
<< "\n Sent:\n " << event_sent_list[i] << "\n Received:\n "
146-
<< event_recv_list[i] << "\n";
142+
<< " vs " << event_recv_list[i].path_name
143+
<< "):" << "\n Sent:\n " << event_sent_list[i]
144+
<< "\n Received:\n " << event_recv_list[i] << "\n";
147145
if (event_sent_list[i].effect_type != event_recv_list[i].effect_type)
148146
std::cerr << "Bad .effect_type field... ("
149147
<< event_sent_list[i].effect_type << " vs "
150-
<< event_recv_list[i].effect_type << "):"
151-
<< "\n Sent:\n " << event_sent_list[i] << "\n Received:\n "
148+
<< event_recv_list[i].effect_type << "):" << "\n Sent:\n "
149+
<< event_sent_list[i] << "\n Received:\n "
152150
<< event_recv_list[i] << "\n";
153151
if (event_sent_list[i].path_type != event_recv_list[i].path_type)
154152
std::cerr << "Bad .path_type field... (" << event_sent_list[i].path_type
155-
<< " vs " << event_recv_list[i].path_type << "):"
156-
<< "\n Sent:\n " << event_sent_list[i] << "\n Received:\n "
157-
<< event_recv_list[i] << "\n";
153+
<< " vs " << event_recv_list[i].path_type
154+
<< "):" << "\n Sent:\n " << event_sent_list[i]
155+
<< "\n Received:\n " << event_recv_list[i] << "\n";
158156
CHECK(event_sent_list[i].path_name == event_recv_list[i].path_name);
159157
CHECK(event_sent_list[i].effect_type == event_recv_list[i].effect_type);
160158
CHECK(event_sent_list[i].path_type == event_recv_list[i].path_type);

devel/include/wtr/watcher-/watch.hpp

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,30 +61,35 @@ class watch {
6161
std::filesystem::path const& path,
6262
event::callback const& callback) noexcept
6363
: watching{std::async(
64-
std::launch::async,
65-
[this, path, callback]
66-
{
67-
using ::detail::wtr::watcher::adapter::watch;
68-
69-
auto ec = std::error_code{};
70-
auto abs_path = std::filesystem::absolute(path, ec);
71-
auto pre_ok = ! ec && std::filesystem::is_directory(abs_path, ec)
72-
&& ! ec && this->is_living.state() == sb::state::pending;
73-
74-
auto live_msg =
75-
(pre_ok ? "s/self/live@" : "e/self/live@") + abs_path.string();
76-
callback(
77-
{live_msg, event::effect_type::create, event::path_type::watcher});
78-
79-
auto post_ok = ! pre_ok || watch(abs_path, callback, this->is_living);
80-
81-
auto die_msg =
82-
(post_ok ? "s/self/die@" : "e/self/die@") + abs_path.string();
83-
callback(
84-
{die_msg, event::effect_type::destroy, event::path_type::watcher});
85-
86-
return pre_ok && post_ok;
87-
})}
64+
std::launch::async,
65+
[this, path, callback]
66+
{
67+
using ::detail::wtr::watcher::adapter::watch;
68+
69+
auto ec = std::error_code{};
70+
auto abs_path = std::filesystem::absolute(path, ec);
71+
auto pre_ok = ! ec && std::filesystem::is_directory(abs_path, ec)
72+
&& ! ec && this->is_living.state() == sb::state::pending;
73+
74+
auto live_msg =
75+
(pre_ok ? "s/self/live@" : "e/self/live@") + abs_path.string();
76+
callback(
77+
{live_msg,
78+
event::effect_type::create,
79+
event::path_type::watcher});
80+
81+
auto post_ok =
82+
! pre_ok || watch(abs_path, callback, this->is_living);
83+
84+
auto die_msg =
85+
(post_ok ? "s/self/die@" : "e/self/die@") + abs_path.string();
86+
callback(
87+
{die_msg,
88+
event::effect_type::destroy,
89+
event::path_type::watcher});
90+
91+
return pre_ok && post_ok;
92+
})}
8893
{}
8994

9095
inline auto close() noexcept -> bool

devel/include/wtr/watcher.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// clang-format off
77
#include "wtr/watcher-/event.hpp"
88
#include "detail/wtr/watcher/semabin.hpp"
9-
#include "detail/wtr/watcher/synchronized.hpp"
109
#include "detail/wtr/watcher/adapter/darwin/watch.hpp"
1110
#include "detail/wtr/watcher/adapter/linux/sysres.hpp"
1211
#include "detail/wtr/watcher/adapter/linux/fanotify/watch.hpp"

0 commit comments

Comments
 (0)