-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix sockpool wait_for_fd for old releases #554
Closed
Coldwings
wants to merge
6
commits into
alibaba:release/0.6
from
Coldwings:fix/sockpool_waitforfd_old_releases
Closed
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8aefb11
FIX: wait_for_fd in epoll should atleast put into sleep for a tiny mo…
Coldwings 5b063be
Also fix on kqueue
Coldwings bdf2350
For old thread_usleep API
Coldwings f30b83c
wait_for_fd(0) reap events directly
Coldwings 596440a
Fix
Coldwings 38a05c7
minor fix
Coldwings File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,6 @@ class KQueue : public MasterEventEngine, public CascadingEventEngine { | |
struct kevent _events[32]; | ||
int _kq = -1; | ||
uint32_t _n = 0; // # of events to submit | ||
struct timespec _tm = {0, 0}; // used for poll | ||
|
||
int init() { | ||
if (_kq >= 0) | ||
|
@@ -57,41 +56,27 @@ class KQueue : public MasterEventEngine, public CascadingEventEngine { | |
|
||
~KQueue() override { | ||
LOG_INFO("Finish event engine: kqueue"); | ||
// if (_n > 0) LOG_INFO(VALUE(_events[0].ident), VALUE(_events[0].filter), VALUE(_events[0].flags)); | ||
// assert(_n == 0); | ||
if (_kq >= 0) | ||
close(_kq); | ||
} | ||
|
||
int enqueue(int fd, short event, uint16_t action, uint32_t event_flags, void* udata, bool immediate = false) { | ||
// LOG_INFO("enqueue _kq: `, fd: `, event: `, action: `", _kq, fd, event, action); | ||
assert(_n < LEN(_events)); | ||
auto entry = &_events[_n++]; | ||
EV_SET(entry, fd, event, action, event_flags, 0, udata); | ||
if (immediate || _n == LEN(_events)) { | ||
int ret = kevent(_kq, _events, _n, nullptr, 0, nullptr); | ||
if (ret < 0) | ||
struct timespec tm{0, 0}; | ||
int ret = kevent(_kq, _events, _n, nullptr, 0, &tm); | ||
if (ret < 0) { | ||
LOG_ERRNO_RETURN(0, -1, "failed to submit events with kevent()"); | ||
} | ||
_n = 0; | ||
} | ||
return 0; | ||
} | ||
|
||
int wait_for_fd(int fd, uint32_t interests, uint64_t timeout) override { | ||
short ev = (interests == EVENT_READ) ? EVFILT_READ : EVFILT_WRITE; | ||
enqueue(fd, ev, EV_ADD | EV_ONESHOT, 0, CURRENT); | ||
int ret = thread_usleep(timeout); | ||
ERRNO err; | ||
if (ret == -1 && err.no == EOK) { | ||
return 0; // event arrived | ||
} | ||
|
||
// enqueue(fd, ev, EV_DELETE, 0, CURRENT, true); // immediately | ||
errno = (ret == 0) ? ETIMEDOUT : err.no; | ||
return -1; | ||
} | ||
|
||
ssize_t wait_and_fire_events(uint64_t timeout = -1) override { | ||
template<typename EVCB> | ||
ssize_t do_wait_and_fire_events(uint64_t timeout, EVCB&& event_callback) { | ||
ssize_t nev = 0; | ||
struct timespec tm; | ||
tm.tv_sec = timeout / 1000 / 1000; | ||
|
@@ -107,7 +92,7 @@ class KQueue : public MasterEventEngine, public CascadingEventEngine { | |
for (int i = 0; i < ret; ++i) { | ||
if (_events[i].filter == EVFILT_USER) continue; | ||
auto th = (thread*) _events[i].udata; | ||
if (th) thread_interrupt(th, EOK); | ||
if (th) event_callback(th); | ||
} | ||
if (ret == (int) LEN(_events)) { // there may be more events | ||
tm.tv_sec = tm.tv_nsec = 0; | ||
|
@@ -116,6 +101,44 @@ class KQueue : public MasterEventEngine, public CascadingEventEngine { | |
return nev; | ||
} | ||
|
||
int wait_for_fd(int fd, uint32_t interests, uint64_t timeout) override { | ||
if (unlikely(interests == 0)) { | ||
errno = ENOSYS; | ||
return -1; | ||
} | ||
short ev = (interests == EVENT_READ) ? EVFILT_READ : EVFILT_WRITE; | ||
auto current = CURRENT; | ||
int ret = enqueue(fd, ev, EV_ADD | EV_ONESHOT, 0, current); | ||
if (ret < 0) return ret; | ||
if (!timeout) { | ||
ret = -1; | ||
do_wait_and_fire_events(0, [current, &ret](thread* th) { | ||
if (th == current) | ||
ret = 0; | ||
else | ||
thread_interrupt(th, EOK); | ||
}); | ||
if (ret <0) { | ||
enqueue(fd, ev, EV_DELETE, 0, current, true); | ||
errno = ETIMEDOUT; | ||
} | ||
return ret; | ||
} | ||
ret = thread_usleep(timeout); | ||
ERRNO err; | ||
if (ret == -1 && err.no == EOK) { | ||
return 0; // event arrived | ||
} | ||
|
||
enqueue(fd, ev, EV_DELETE, 0, current, true); | ||
errno = (ret == 0) ? ETIMEDOUT : err.no; | ||
return -1; | ||
} | ||
|
||
ssize_t wait_and_fire_events(uint64_t timeout) override { | ||
return do_wait_and_fire_events(timeout, [](thread *th) { thread_interrupt(th, EOK); }); | ||
} | ||
|
||
int cancel_wait() override { | ||
enqueue(_kq, EVFILT_USER, EV_ONESHOT, NOTE_TRIGGER, nullptr, true); | ||
return 0; | ||
|
@@ -164,11 +187,12 @@ class KQueue : public MasterEventEngine, public CascadingEventEngine { | |
} | ||
|
||
ssize_t wait_for_events(void** data, | ||
size_t count, uint64_t timeout = -1) override { | ||
int ret = get_vcpu()->master_event_engine->wait_for_fd_readable(_kq, timeout); | ||
size_t count, uint64_t timeout) override { | ||
int ret = ::photon::wait_for_fd_readable(_kq, timeout); | ||
if (ret < 0) return errno == ETIMEDOUT ? 0 : -1; | ||
if (count > LEN(_events)) | ||
count = LEN(_events); | ||
static const struct timespec _tm = {0, 0}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why static here, while not in enqueue()? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use on-stack temporary timespec is fine. Changed in latest commit. |
||
ret = kevent(_kq, _events, _n, _events, count, &_tm); | ||
if (ret < 0) | ||
LOG_ERRNO_RETURN(0, -1, "failed to call kevent()"); | ||
|
@@ -183,7 +207,7 @@ class KQueue : public MasterEventEngine, public CascadingEventEngine { | |
}; | ||
|
||
__attribute__((noinline)) | ||
KQueue* new_kqueue_engine() { | ||
static KQueue* new_kqueue_engine() { | ||
LOG_INFO("Init event engine: kqueue"); | ||
return NewObj<KQueue>()->init(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's might be safer to
rm_interest(event)
as before.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done