Skip to content

Commit e7a6c3f

Browse files
committed
use utki::assert() and utki::log_debug()
1 parent 1b48243 commit e7a6c3f

1 file changed

Lines changed: 85 additions & 51 deletions

File tree

‎src/opros/wait_set.cpp‎

Lines changed: 85 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ wait_set::wait_set(unsigned capacity) :
5353
waitables(capacity),
5454
handles(capacity)
5555
{
56-
ASSERT(capacity <= MAXIMUM_WAIT_OBJECTS, [&](auto& o) {
57-
o << "capacity should be less than " << MAXIMUM_WAIT_OBJECTS;
58-
})
56+
utki::assert(
57+
capacity <= MAXIMUM_WAIT_OBJECTS,
58+
[&](auto& o) {
59+
o << "capacity should be less than " << MAXIMUM_WAIT_OBJECTS;
60+
},
61+
SL
62+
);
5963
if (capacity > MAXIMUM_WAIT_OBJECTS) {
6064
throw std::invalid_argument("wait_set::wait_set(): requested wait_set maximum size is too big");
6165
}
@@ -68,7 +72,7 @@ wait_set::wait_set(unsigned capacity) :
6872
if (capacity > std::numeric_limits<int>::max()) {
6973
throw std::invalid_argument("wait_set(): given capacity is too big, should be <= INT_MAX");
7074
}
71-
ASSERT(int(capacity) > 0)
75+
utki::assert(int(capacity) > 0, SL);
7276
this->epoll_set = epoll_create(int(capacity));
7377
if (this->epoll_set < 0) {
7478
throw std::system_error(errno, std::generic_category(), "wait_set::wait_set(): epoll_create() failed");
@@ -87,7 +91,7 @@ wait_set::wait_set(unsigned capacity) :
8791
if (capacity > std::numeric_limits<int>::max()) {
8892
throw std::invalid_argument("wait_set(): given capacity is too big, should be <= INT_MAX");
8993
}
90-
ASSERT(int(capacity) > 0)
94+
utki::assert(int(capacity) > 0, SL);
9195
this->queue = kqueue();
9296
if (this->queue == -1) {
9397
throw std::system_error(errno, std::generic_category(), "wait_set::wait_set(): kqueue creation failed");
@@ -99,34 +103,56 @@ wait_set::wait_set(unsigned capacity) :
99103

100104
#if CFG_OS == CFG_OS_MACOSX
101105

102-
void wait_set::add_filter(waitable& w, int16_t filter, void* user_data)
106+
void wait_set::add_filter(
107+
waitable& w, //
108+
int16_t filter,
109+
void* user_data
110+
)
103111
{
104112
using kevent_struct = struct kevent;
105113
kevent_struct e{};
106114

107-
EV_SET(&e, w.handle, filter, EV_ADD | EV_RECEIPT, 0, 0, user_data);
115+
EV_SET(
116+
&e, //
117+
w.handle,
118+
filter,
119+
EV_ADD | EV_RECEIPT,
120+
0,
121+
0,
122+
user_data
123+
);
108124

109125
// 0 to make effect of polling, because passing
110126
// NULL will cause to wait indefinitely.
111127
const timespec timeout = {0, 0};
112128

113-
int res = kevent(this->queue, &e, 1, nullptr, 0, &timeout);
129+
int res = kevent(
130+
this->queue, //
131+
&e,
132+
1,
133+
nullptr,
134+
0,
135+
&timeout
136+
);
114137
if (res < 0) {
115138
throw std::system_error(errno, std::generic_category(), "wait_set::add(): add_filter(): kevent() failed");
116139
}
117140

118141
// EV_ERROR is always returned because of EV_RECEIPT, according to kevent() documentation.
119-
ASSERT((e.flags & EV_ERROR) != 0)
142+
utki::assert((e.flags & EV_ERROR) != 0, SL);
120143

121144
if (e.data != 0) { // data should be 0 if added successfully
122-
LOG([&](auto& o) {
145+
utki::log_debug([&](auto& o) {
123146
o << "wait_set::add(): e.data = " << e.data << std::endl;
124-
})
147+
});
125148
throw std::runtime_error("wait_set::add(): add_filter(): kevent() failed to add filter");
126149
}
127150
}
128151

129-
void wait_set::remove_filter(waitable& w, int16_t filter) noexcept
152+
void wait_set::remove_filter(
153+
waitable& w, //
154+
int16_t filter
155+
) noexcept
130156
{
131157
using kevent_struct = struct kevent;
132158
kevent_struct e{};
@@ -139,21 +165,21 @@ void wait_set::remove_filter(waitable& w, int16_t filter) noexcept
139165
int res = kevent(this->queue, &e, 1, nullptr, 0, &timeout);
140166
if (res < 0) {
141167
// ignore the failure
142-
LOG([&](auto& o) {
168+
utki::log_debug([&](auto& o) {
143169
o << "wait_set::remove(): remove_filter(): kevent() failed" << std::endl;
144-
})
170+
});
145171
}
146172

147173
// EV_ERROR is always returned because of EV_RECEIPT, according to kevent() documentation.
148-
ASSERT((e.flags & EV_ERROR) != 0)
174+
utki::assert((e.flags & EV_ERROR) != 0, SL);
149175
}
150176

151177
#endif
152178

153179
void wait_set::add(waitable& w, utki::flags<ready> wait_for, void* user_data)
154180
{
155181
#if CFG_OS == CFG_OS_WINDOWS
156-
ASSERT(this->size() <= this->handles.size())
182+
utki::assert(this->size() <= this->handles.size(), SL);
157183
if (this->size() == this->handles.size()) {
158184
throw std::logic_error("wait_set::add(): wait set is full");
159185
}
@@ -177,15 +203,15 @@ void wait_set::add(waitable& w, utki::flags<ready> wait_for, void* user_data)
177203
(wait_for.get(ready::write) ? EPOLLOUT : 0) | (EPOLLERR);
178204
int res = epoll_ctl(this->epoll_set, EPOLL_CTL_ADD, w.handle, &e);
179205
if (res < 0) {
180-
LOG([&](auto& o) {
206+
utki::log_debug([&](auto& o) {
181207
o << "wait_set::add(): epoll_ctl() failed. If you are adding socket, "
182208
"please check that is is opened before adding to wait_set."
183209
<< std::endl;
184-
})
210+
});
185211
throw std::system_error(errno, std::generic_category(), "wait_set::add(): epoll_ctl() failed");
186212
}
187213
#elif CFG_OS == CFG_OS_MACOSX
188-
ASSERT(this->size() <= this->revents.size() / 2)
214+
utki::assert(this->size() <= this->revents.size() / 2, SL);
189215

190216
if (wait_for.get(ready::read)) {
191217
this->add_filter(w, EVFILT_READ, user_data);
@@ -295,10 +321,14 @@ void wait_set::remove(waitable& w) noexcept
295321
#elif CFG_OS == CFG_OS_LINUX
296322
int res = epoll_ctl(this->epoll_set, EPOLL_CTL_DEL, w.handle, nullptr);
297323
if (res < 0) {
298-
ASSERT(false, [&](auto& o) {
299-
o << "wait_set::Remove(): epoll_ctl failed, probably the waitable was "
300-
"not added to the wait set";
301-
})
324+
utki::assert(
325+
false,
326+
[&](auto& o) {
327+
o << "wait_set::Remove(): epoll_ctl failed, probably the waitable was "
328+
"not added to the wait set";
329+
},
330+
SL
331+
);
302332
}
303333
#elif CFG_OS == CFG_OS_MACOSX
304334
this->remove_filter(w, EVFILT_READ);
@@ -321,7 +351,7 @@ bool wait_set::wait_internal_linux(int timeout)
321351
int num_events_triggered{};
322352

323353
while (true) {
324-
ASSERT(this->revents.size() <= std::numeric_limits<int>::max())
354+
utki::assert(this->revents.size() <= std::numeric_limits<int>::max(), SL);
325355
num_events_triggered = epoll_wait(this->epoll_set, this->revents.data(), int(this->revents.size()), timeout);
326356

327357
// TRACE(<< "epoll_wait() returned " << num_events_triggered << std::endl)
@@ -342,14 +372,14 @@ bool wait_set::wait_internal_linux(int timeout)
342372
return false;
343373
}
344374

345-
ASSERT(num_events_triggered > 0)
346-
ASSERT(this->revents.size() == out_events.size())
347-
ASSERT(size_t(num_events_triggered) <= this->revents.size())
348-
ASSERT(size_t(num_events_triggered) <= out_events.size())
375+
utki::assert(num_events_triggered > 0, SL);
376+
utki::assert(this->revents.size() == out_events.size(), SL);
377+
utki::assert(size_t(num_events_triggered) <= this->revents.size(), SL);
378+
utki::assert(size_t(num_events_triggered) <= out_events.size(), SL);
349379

350380
unsigned out_i = 0;
351381
for (const auto& e : utki::make_span(this->revents.data(), num_events_triggered)) {
352-
ASSERT(out_i < out_events.size())
382+
utki::assert(out_i < out_events.size(), SL);
353383
event_info& ei = out_events[out_i];
354384
++out_i;
355385

@@ -366,12 +396,12 @@ bool wait_set::wait_internal_linux(int timeout)
366396
ei.flags.set(ready::write);
367397
}
368398

369-
ASSERT(!ei.flags.is_clear())
399+
utki::assert(!ei.flags.is_clear(), SL);
370400
}
371401

372-
ASSERT(out_i > 0)
373-
ASSERT(size_t(out_i) <= out_events.size())
374-
ASSERT(out_i == unsigned(num_events_triggered))
402+
utki::assert(out_i > 0, SL);
403+
utki::assert(size_t(out_i) <= out_events.size(), SL);
404+
utki::assert(out_i == unsigned(num_events_triggered), SL);
375405
this->triggered = utki::make_span(out_events.data(), num_events_triggered);
376406

377407
return true;
@@ -414,10 +444,10 @@ bool wait_set::wait_internal(bool wait_infinitly, uint32_t timeout)
414444

415445
// Return value cannot be WAIT_IO_COMPLETION because we supplied FALSE as
416446
// last parameter to WaitForMultipleObjectsEx().
417-
ASSERT(res != WAIT_IO_COMPLETION)
447+
utki::assert(res != WAIT_IO_COMPLETION, SL);
418448

419449
// we are not expecting abandoned mutexes
420-
ASSERT(res < WAIT_ABANDONED_0 || (WAIT_ABANDONED_0 + this->size_of_wait_set) <= res)
450+
utki::assert(res < WAIT_ABANDONED_0 || (WAIT_ABANDONED_0 + this->size_of_wait_set) <= res, SL);
421451

422452
if (res == WAIT_FAILED) {
423453
throw std::system_error(
@@ -432,11 +462,11 @@ bool wait_set::wait_internal(bool wait_infinitly, uint32_t timeout)
432462
return false;
433463
}
434464

435-
ASSERT(WAIT_OBJECT_0 <= res && res < (WAIT_OBJECT_0 + this->size_of_wait_set))
465+
utki::assert(WAIT_OBJECT_0 <= res && res < (WAIT_OBJECT_0 + this->size_of_wait_set), SL);
436466

437467
auto out_events = this->get_out_events();
438-
ASSERT(out_events.size() == this->waitables.size())
439-
ASSERT(this->handles.size() == this->waitables.size())
468+
utki::assert(out_events.size() == this->waitables.size(), SL);
469+
utki::assert(this->handles.size() == this->waitables.size(), SL);
440470

441471
// check for activities
442472
unsigned num_events = 0;
@@ -460,15 +490,15 @@ bool wait_set::wait_internal(bool wait_infinitly, uint32_t timeout)
460490

461491
// NOTE: Need to call get_readiness_flags() even if 'num_events < out_events.size()',
462492
// because it resets the readiness state of the HANDLE.
463-
ASSERT(wi.w)
493+
utki::assert(wi.w, SL);
464494
auto flags = wi.w->get_readiness_flags();
465495

466496
// WORKAROUND:
467497
// On Windows, sometimes event triggers, but then no readiness flags are reported.
468498
// As a workaround, here we need to check if there are any readiness
469499
// flags actually set.
470500
if (!flags.is_clear()) {
471-
ASSERT(num_events < out_events.size())
501+
utki::assert(num_events < out_events.size(), SL);
472502

473503
out_events[num_events].user_data = wi.user_data;
474504
out_events[num_events].flags = flags;
@@ -478,8 +508,8 @@ bool wait_set::wait_internal(bool wait_infinitly, uint32_t timeout)
478508
}
479509
}
480510

481-
ASSERT(num_events <= this->size_of_wait_set)
482-
ASSERT(num_events <= out_events.size())
511+
utki::assert(num_events <= this->size_of_wait_set, SL);
512+
utki::assert(num_events <= out_events.size(), SL);
483513
this->triggered = utki::make_span(out_events.data(), num_events);
484514

485515
return true;
@@ -496,9 +526,13 @@ bool wait_set::wait_internal(bool wait_infinitly, uint32_t timeout)
496526
auto max_time_step = uint32_t(std::numeric_limits<int>::max());
497527

498528
while (timeout >= max_time_step) {
499-
ASSERT(int(max_time_step) >= 0, [&](auto& o) {
500-
o << "timeout = 0x" << std::hex << timeout;
501-
})
529+
utki::assert(
530+
int(max_time_step) >= 0,
531+
[&](auto& o) {
532+
o << "timeout = 0x" << std::hex << timeout;
533+
},
534+
SL
535+
);
502536
auto res = this->wait_internal_linux(int(max_time_step));
503537
if (res != 0) {
504538
return res;
@@ -510,7 +544,7 @@ bool wait_set::wait_internal(bool wait_infinitly, uint32_t timeout)
510544
}
511545
}
512546

513-
ASSERT(int(timeout) >= 0)
547+
utki::assert(int(timeout) >= 0, SL);
514548
return this->wait_internal_linux(int(timeout));
515549

516550
#elif CFG_OS == CFG_OS_MACOSX
@@ -520,7 +554,7 @@ bool wait_set::wait_internal(bool wait_infinitly, uint32_t timeout)
520554
};
521555

522556
for (;;) {
523-
ASSERT(this->revents.size() <= std::numeric_limits<int>::max())
557+
utki::assert(this->revents.size() <= std::numeric_limits<int>::max(), SL);
524558
int num_events_triggered = kevent(
525559
this->queue,
526560
nullptr,
@@ -543,11 +577,11 @@ bool wait_set::wait_internal(bool wait_infinitly, uint32_t timeout)
543577
return false;
544578
}
545579

546-
ASSERT(num_events_triggered > 0)
580+
utki::assert(num_events_triggered > 0, SL);
547581

548582
auto out_events = this->get_out_events();
549583

550-
ASSERT(out_events.size() == this->revents.size())
584+
utki::assert(out_events.size() == this->revents.size(), SL);
551585

552586
size_t out_i = 0; // index into out_events
553587

@@ -575,11 +609,11 @@ bool wait_set::wait_internal(bool wait_infinitly, uint32_t timeout)
575609
oe.user_data = e.udata;
576610
}
577611

578-
ASSERT(out_i <= out_events.size())
612+
utki::assert(out_i <= out_events.size(), SL);
579613

580614
// out_i can be less than number of events triggered because there can be unsupported events
581615
// which are not counted
582-
ASSERT(out_i <= size_t(num_events_triggered))
616+
utki::assert(out_i <= size_t(num_events_triggered), SL);
583617

584618
this->triggered = utki::make_span(out_events.data(), out_i);
585619

0 commit comments

Comments
 (0)