Skip to content

Commit

Permalink
Merge pull request FreeRDP#949 from hardening/eventfd
Browse files Browse the repository at this point in the history
Add support for eventfd
  • Loading branch information
awakecoding committed Feb 2, 2013
2 parents 1d9f133 + 497041b commit a00ac1f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ if(NOT ANDROID)
endif()

if(UNIX OR CYGWIN)
check_include_files(sys/eventfd.h HAVE_EVENTFD_H)
set(X11_FEATURE_TYPE "RECOMMENDED")
else()
set(X11_FEATURE_TYPE "DISABLED")
Expand Down
2 changes: 1 addition & 1 deletion config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#cmakedefine HAVE_SYS_MODEM_H
#cmakedefine HAVE_SYS_FILIO_H
#cmakedefine HAVE_SYS_STRTIO_H

#cmakedefine HAVE_EVENTFD_H
#cmakedefine HAVE_TM_GMTOFF


Expand Down
39 changes: 37 additions & 2 deletions winpr/libwinpr/synch/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
#include <unistd.h>
#endif


#ifdef HAVE_EVENTFD_H
#include <sys/eventfd.h>
#include <errno.h>
#endif

CRITICAL_SECTION cs = { NULL, 0, 0, NULL, NULL, 0 };

HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCWSTR lpName)
Expand All @@ -57,11 +63,20 @@ HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
event->pipe_fd[0] = -1;
event->pipe_fd[1] = -1;

#ifdef HAVE_EVENTFD_H
event->pipe_fd[0] = eventfd(0, EFD_NONBLOCK);
if (event->pipe_fd[0] < 0)
{
printf("CreateEventW: failed to create event\n");
return NULL;
}
#else
if (pipe(event->pipe_fd) < 0)
{
printf("CreateEventW: failed to create event\n");
return NULL;
}
#endif

handle = winpr_Handle_Insert(HANDLE_TYPE_EVENT, event);
}
Expand Down Expand Up @@ -113,7 +128,16 @@ BOOL SetEvent(HANDLE hEvent)
{
event = (WINPR_EVENT*) Object;

if (!(WaitForSingleObject(hEvent, 0) == WAIT_OBJECT_0))
#ifdef HAVE_EVENTFD_H
eventfd_t val = 1;
do
{
length = eventfd_write(event->pipe_fd[0], val);
}
while(length < 0 && errno == EINTR);
status = (length == 0) ? TRUE : FALSE;
#else
if (WaitForSingleObject(hEvent, 0) != WAIT_OBJECT_0)
{
length = write(event->pipe_fd[1], "-", 1);

Expand All @@ -124,6 +148,7 @@ BOOL SetEvent(HANDLE hEvent)
{
status = TRUE;
}
#endif
}

LeaveCriticalSection(&cs);
Expand All @@ -149,13 +174,23 @@ BOOL ResetEvent(HANDLE hEvent)

while (WaitForSingleObject(hEvent, 0) == WAIT_OBJECT_0)
{
#ifdef HAVE_EVENTFD_H
eventfd_t value;
do
{
length = eventfd_read(event->pipe_fd[0], &value);
}
while(length < 0 && errno == EINTR);

status = (length > 0) ? TRUE : FALSE;
#else
length = read(event->pipe_fd[0], &length, 1);

if (length == 1)
status = TRUE;

if (length != 1)
break;
#endif
}
}

Expand Down

0 comments on commit a00ac1f

Please sign in to comment.