FreeRDP/winpr/libwinpr/synch/event.c

349 lines
6.9 KiB
C
Raw Normal View History

2012-05-29 22:14:26 +04:00
/**
* WinPR: Windows Portable Runtime
* Synchronization Functions
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2012-11-27 05:34:36 +04:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
2012-05-29 22:14:26 +04:00
2012-11-27 05:34:36 +04:00
#include <winpr/synch.h>
#ifndef _WIN32
#include "synch.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
2013-01-24 00:59:01 +04:00
#ifdef HAVE_EVENTFD_H
#include <sys/eventfd.h>
#include <errno.h>
#endif
2013-05-17 01:32:58 +04:00
#include "../handle/handle.h"
#include "../pipe/pipe.h"
2013-05-17 01:32:58 +04:00
2014-08-18 19:22:22 +04:00
#include "../log.h"
#define TAG WINPR_TAG("synch.event")
2014-08-18 19:22:22 +04:00
CRITICAL_SECTION cs = { NULL, 0, 0, NULL, NULL, 0 };
HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCWSTR lpName)
{
2014-08-18 21:34:47 +04:00
WINPR_EVENT* event;
2014-11-16 19:06:42 +03:00
event = (WINPR_EVENT*) calloc(1, sizeof(WINPR_EVENT));
if (event)
{
event->bAttached = FALSE;
event->bManualReset = bManualReset;
if (!event->bManualReset)
{
2014-08-18 19:22:22 +04:00
WLog_ERR(TAG, "auto-reset events not yet implemented");
}
event->pipe_fd[0] = -1;
event->pipe_fd[1] = -1;
2013-01-24 00:59:01 +04:00
#ifdef HAVE_EVENTFD_H
event->pipe_fd[0] = eventfd(0, EFD_NONBLOCK);
2013-01-24 00:59:01 +04:00
if (event->pipe_fd[0] < 0)
{
2014-08-18 19:22:22 +04:00
WLog_ERR(TAG, "failed to create event");
2013-08-28 19:51:43 +04:00
free(event);
2013-01-24 00:59:01 +04:00
return NULL;
}
2014-08-18 19:22:22 +04:00
2013-01-24 00:59:01 +04:00
#else
2014-08-18 19:22:22 +04:00
if (pipe(event->pipe_fd) < 0)
{
2014-08-18 19:22:22 +04:00
WLog_ERR(TAG, "failed to create event");
2013-08-28 19:51:43 +04:00
free(event);
return NULL;
}
2014-08-18 19:22:22 +04:00
#endif
WINPR_HANDLE_SET_TYPE(event, HANDLE_TYPE_EVENT);
if (bInitialState)
2014-11-16 19:06:42 +03:00
SetEvent(event);
}
if (!cs.LockSemaphore)
InitializeCriticalSection(&cs);
2014-11-16 19:06:42 +03:00
return (HANDLE)event;
}
HANDLE CreateEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCSTR lpName)
{
return CreateEventW(lpEventAttributes, bManualReset, bInitialState, NULL);
}
HANDLE CreateEventExW(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCWSTR lpName, DWORD dwFlags, DWORD dwDesiredAccess)
{
return NULL;
}
HANDLE CreateEventExA(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCSTR lpName, DWORD dwFlags, DWORD dwDesiredAccess)
{
return NULL;
}
HANDLE OpenEventW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName)
{
return NULL;
}
2012-05-29 22:14:26 +04:00
HANDLE OpenEventA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName)
{
return NULL;
}
#ifdef HAVE_EVENTFD_H
#if defined(__UCLIBC__)
static int eventfd_read(int fd, eventfd_t* value)
{
return (read(fd, value, sizeof(*value)) == sizeof(*value)) ? 0 : -1;
}
static int eventfd_write(int fd, eventfd_t value)
{
return (write(fd, &value, sizeof(value)) == sizeof(value)) ? 0 : -1;
}
#endif
#endif
BOOL SetEvent(HANDLE hEvent)
{
ULONG Type;
PVOID Object;
int length;
BOOL status;
2014-08-18 21:34:47 +04:00
WINPR_EVENT* event;
status = FALSE;
if (winpr_Handle_GetInfo(hEvent, &Type, &Object))
{
2014-08-18 21:34:47 +04:00
event = (WINPR_EVENT*) Object;
2013-01-24 00:59:01 +04:00
#ifdef HAVE_EVENTFD_H
eventfd_t val = 1;
2013-02-10 02:13:53 +04:00
do
{
length = eventfd_write(event->pipe_fd[0], val);
}
2013-02-10 02:13:53 +04:00
while ((length < 0) && (errno == EINTR));
status = (length == 0) ? TRUE : FALSE;
2013-01-24 00:59:01 +04:00
#else
2014-08-18 19:22:22 +04:00
if (WaitForSingleObject(hEvent, 0) != WAIT_OBJECT_0)
{
length = write(event->pipe_fd[1], "-", 1);
if (length == 1)
status = TRUE;
}
else
{
status = TRUE;
}
2014-08-18 19:22:22 +04:00
#endif
}
return status;
}
2012-05-29 22:14:26 +04:00
BOOL ResetEvent(HANDLE hEvent)
{
ULONG Type;
PVOID Object;
int length;
BOOL status;
2014-08-18 21:34:47 +04:00
WINPR_EVENT* event;
status = FALSE;
if (winpr_Handle_GetInfo(hEvent, &Type, &Object))
{
2014-08-18 21:34:47 +04:00
event = (WINPR_EVENT*) Object;
while (WaitForSingleObject(hEvent, 0) == WAIT_OBJECT_0)
{
2013-01-24 00:59:01 +04:00
#ifdef HAVE_EVENTFD_H
eventfd_t value;
2013-02-10 02:13:53 +04:00
2013-01-24 00:59:01 +04:00
do
{
length = eventfd_read(event->pipe_fd[0], &value);
}
2013-02-10 02:13:53 +04:00
while ((length < 0) && (errno == EINTR));
2013-01-24 00:59:01 +04:00
if ((length > 0) && (!status))
status = TRUE;
2014-08-18 19:22:22 +04:00
2013-01-24 00:59:01 +04:00
#else
length = read(event->pipe_fd[0], &length, 1);
2013-02-10 02:13:53 +04:00
if ((length == 1) && (!status))
status = TRUE;
2014-08-18 19:22:22 +04:00
2013-01-24 00:59:01 +04:00
#endif
}
}
return status;
}
#endif
HANDLE CreateFileDescriptorEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, int FileDescriptor)
{
2012-11-27 05:34:36 +04:00
#ifndef _WIN32
2014-08-18 21:34:47 +04:00
WINPR_EVENT* event;
HANDLE handle = NULL;
2014-08-18 21:34:47 +04:00
event = (WINPR_EVENT*) malloc(sizeof(WINPR_EVENT));
if (event)
{
event->bAttached = TRUE;
event->bManualReset = bManualReset;
event->pipe_fd[0] = FileDescriptor;
event->pipe_fd[1] = -1;
WINPR_HANDLE_SET_TYPE(event, HANDLE_TYPE_EVENT);
handle = (HANDLE) event;
}
return handle;
2012-11-27 05:34:36 +04:00
#else
return NULL;
#endif
}
HANDLE CreateFileDescriptorEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, int FileDescriptor)
{
return CreateFileDescriptorEventW(lpEventAttributes, bManualReset, bInitialState, FileDescriptor);
}
/**
* Returns an event based on the handle returned by GetEventWaitObject()
*/
HANDLE CreateWaitObjectEvent(LPSECURITY_ATTRIBUTES lpEventAttributes,
2014-08-18 21:34:47 +04:00
BOOL bManualReset, BOOL bInitialState, void* pObject)
{
#ifndef _WIN32
2014-08-18 19:22:22 +04:00
return CreateFileDescriptorEventW(lpEventAttributes, bManualReset, bInitialState, (int)(ULONG_PTR) pObject);
#else
HANDLE hEvent = NULL;
DuplicateHandle(GetCurrentProcess(), pObject, GetCurrentProcess(), &hEvent, 0, FALSE, DUPLICATE_SAME_ACCESS);
return hEvent;
#endif
}
/*
* Returns inner file descriptor for usage with select()
* This file descriptor is not usable on Windows
*/
int GetEventFileDescriptor(HANDLE hEvent)
{
2012-11-27 05:34:36 +04:00
#ifndef _WIN32
ULONG Type;
PVOID Object;
2014-08-18 21:34:47 +04:00
WINPR_EVENT* event;
if (!winpr_Handle_GetInfo(hEvent, &Type, &Object))
return -1;
2014-08-18 21:34:47 +04:00
event = (WINPR_EVENT*) Object;
2014-08-18 19:22:22 +04:00
if (Type == HANDLE_TYPE_NAMED_PIPE)
{
2014-08-18 21:34:47 +04:00
WINPR_NAMED_PIPE* named = (WINPR_NAMED_PIPE*)hEvent;
2014-08-18 19:22:22 +04:00
if (named->ServerMode)
{
return named->serverfd;
2014-08-18 19:22:22 +04:00
}
else
{
return named->clientfd;
}
}
return event->pipe_fd[0];
2012-11-27 05:34:36 +04:00
#else
return -1;
#endif
}
/*
* Set inner file descriptor for usage with select()
* This file descriptor is not usable on Windows
*/
int SetEventFileDescriptor(HANDLE hEvent, int FileDescriptor)
{
#ifndef _WIN32
ULONG Type;
PVOID Object;
2014-08-18 21:34:47 +04:00
WINPR_EVENT* event;
if (!winpr_Handle_GetInfo(hEvent, &Type, &Object))
return -1;
2014-08-18 21:34:47 +04:00
event = (WINPR_EVENT*) Object;
event->pipe_fd[0] = FileDescriptor;
return 0;
#else
return -1;
#endif
}
/**
* Returns platform-specific wait object as a void pointer
*
* On Windows, the returned object is the same as the hEvent
* argument and is an event HANDLE usable in WaitForMultipleObjects
*
* On other platforms, the returned object can be cast to an int
* to obtain a file descriptor usable in select()
*/
2014-08-18 21:34:47 +04:00
void* GetEventWaitObject(HANDLE hEvent)
{
#ifndef _WIN32
int fd;
2014-08-18 21:34:47 +04:00
void* obj;
fd = GetEventFileDescriptor(hEvent);
2014-08-18 21:34:47 +04:00
obj = ((void*)(long) fd);
return obj;
#else
return hEvent;
#endif
}
2012-12-21 21:22:37 +04:00