Skip to content
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

Non-persistent and edge-triggered modes for FileDescriptorEvent. #1596

Merged
merged 2 commits into from
Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions core/vibe/core/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -821,15 +821,16 @@ Timer createTimer(void delegate() callback)
Params:
file_descriptor = The Posix file descriptor to watch
event_mask = Specifies which events will be listened for
event_mode = Specifies event waiting mode

Returns:
Returns a newly created FileDescriptorEvent associated with the given
file descriptor.
*/
FileDescriptorEvent createFileDescriptorEvent(int file_descriptor, FileDescriptorEvent.Trigger event_mask)
FileDescriptorEvent createFileDescriptorEvent(int file_descriptor, FileDescriptorEvent.Trigger event_mask, FileDescriptorEvent.Mode event_mode = FileDescriptorEvent.Mode.persistent)
{
auto drv = getEventDriver();
return drv.createFileDescriptorEvent(file_descriptor, event_mask);
return drv.createFileDescriptorEvent(file_descriptor, event_mask, event_mode);
}


Expand Down
10 changes: 9 additions & 1 deletion core/vibe/core/driver.d
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ interface EventDriver {

/** Creates an event for waiting on a non-bocking file handle.
*/
FileDescriptorEvent createFileDescriptorEvent(int file_descriptor, FileDescriptorEvent.Trigger triggers);
FileDescriptorEvent createFileDescriptorEvent(int file_descriptor, FileDescriptorEvent.Trigger triggers, FileDescriptorEvent.Mode mode);

/** Creates a new timer.

Expand Down Expand Up @@ -244,6 +244,14 @@ interface FileDescriptorEvent {
any = read|write /// Match any kind of event
}

/** Event waiting mode.
*/
enum Mode {
nonPersistent, /// Indicates that the event is non-persistent
persistent, /// Indicates that the event is persistent
edgeTriggered /// Indicates that the event should be edge-triggered
}

/** Waits for the selected event to occur.

Params:
Expand Down
2 changes: 1 addition & 1 deletion core/vibe/core/drivers/libasync.d
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ final class LibasyncDriver : EventDriver {
return new LibasyncManualEvent(this);
}

FileDescriptorEvent createFileDescriptorEvent(int file_descriptor, FileDescriptorEvent.Trigger triggers)
FileDescriptorEvent createFileDescriptorEvent(int file_descriptor, FileDescriptorEvent.Trigger triggers, FileDescriptorEvent.Mode mode)
{
assert(false);
}
Expand Down
19 changes: 13 additions & 6 deletions core/vibe/core/drivers/libevent2.d
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,9 @@ final class Libevent2Driver : EventDriver {
return new Libevent2ManualEvent(this);
}

Libevent2FileDescriptorEvent createFileDescriptorEvent(int fd, FileDescriptorEvent.Trigger events)
Libevent2FileDescriptorEvent createFileDescriptorEvent(int fd, FileDescriptorEvent.Trigger events, FileDescriptorEvent.Mode mode)
{
return new Libevent2FileDescriptorEvent(this, fd, events);
return new Libevent2FileDescriptorEvent(this, fd, events, mode);
}

size_t createTimer(void delegate() callback) { return m_timers.create(TimerInfo(callback)); }
Expand Down Expand Up @@ -813,20 +813,24 @@ final class Libevent2FileDescriptorEvent : Libevent2Object, FileDescriptorEvent
private {
int m_fd;
deimos.event2.event.event* m_event;
bool m_persistent;
Trigger m_activeEvents;
Task m_waiter;
}

this(Libevent2Driver driver, int file_descriptor, Trigger events)
this(Libevent2Driver driver, int file_descriptor, Trigger events, Mode mode)
{
assert(events != Trigger.none);
super(driver, false);
m_fd = file_descriptor;
m_persistent = mode != Mode.nonPersistent;
short evts = 0;
if (events & Trigger.read) evts |= EV_READ;
if (events & Trigger.write) evts |= EV_WRITE;
m_event = event_new(driver.eventLoop, file_descriptor, evts|EV_PERSIST, &onFileTriggered, cast(void*)this);
event_add(m_event, null);
if (m_persistent) evts |= EV_PERSIST;
if (mode == Mode.edgeTriggered) evts |= EV_ET;
m_event = event_new(driver.eventLoop, file_descriptor, evts, &onFileTriggered, cast(void*)this);
if (m_persistent) event_add(m_event, null);
}

~this()
Expand All @@ -843,8 +847,10 @@ final class Libevent2FileDescriptorEvent : Libevent2Object, FileDescriptorEvent
m_activeEvents &= ~which;
}

while ((m_activeEvents & which) == Trigger.none)
while ((m_activeEvents & which) == Trigger.none) {
if (!m_persistent) event_add(m_event, null);
getThreadLibeventDriverCore().yieldForEvent();
}
return m_activeEvents & which;
}

Expand All @@ -863,6 +869,7 @@ final class Libevent2FileDescriptorEvent : Libevent2Object, FileDescriptorEvent
m_driver.rearmTimer(tm, timeout, false);

while ((m_activeEvents & which) == Trigger.none) {
if (!m_persistent) event_add(m_event, null);
getThreadLibeventDriverCore().yieldForEvent();
if (!m_driver.isTimerPending(tm)) break;
}
Expand Down
2 changes: 1 addition & 1 deletion core/vibe/core/drivers/win32.d
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ final class Win32EventDriver : EventDriver {
return new Win32ManualEvent(this);
}

FileDescriptorEvent createFileDescriptorEvent(int file_descriptor, FileDescriptorEvent.Trigger events)
FileDescriptorEvent createFileDescriptorEvent(int file_descriptor, FileDescriptorEvent.Trigger events, FileDescriptorEvent.Mode mode)
{
assert(false, "Not implemented.");
}
Expand Down