2012-05-29 22:14:26 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Synchronization Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2017-02-20 20:31:58 +03:00
|
|
|
* Copyright 2017 Armin Novak <armin.novak@thincast.com>
|
|
|
|
* Copyright 2017 Thincast Technologies GmbH
|
2012-05-29 22:14:26 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:20:53 +04:00
|
|
|
#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>
|
2012-09-18 20:57:19 +04:00
|
|
|
|
2012-09-19 02:36:13 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
#include "synch.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2012-09-18 20:57:19 +04:00
|
|
|
|
2018-02-05 01:16:12 +03:00
|
|
|
#ifdef HAVE_SYS_EVENTFD_H
|
2013-01-24 00:59:01 +04:00
|
|
|
#include <sys/eventfd.h>
|
|
|
|
#endif
|
|
|
|
|
2015-06-17 15:13:28 +03:00
|
|
|
#include <errno.h>
|
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
#include "../handle/handle.h"
|
2014-02-07 18:18:17 +04:00
|
|
|
#include "../pipe/pipe.h"
|
2013-05-17 01:32:58 +04:00
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#include "../log.h"
|
2014-08-18 20:57:08 +04:00
|
|
|
#define TAG WINPR_TAG("synch.event")
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2015-03-11 17:11:11 +03:00
|
|
|
static BOOL EventCloseHandle(HANDLE handle);
|
|
|
|
|
|
|
|
static BOOL EventIsHandled(HANDLE handle)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_TIMER* pEvent = (WINPR_TIMER*)handle;
|
2015-03-11 17:11:11 +03:00
|
|
|
|
2015-03-24 14:13:41 +03:00
|
|
|
if (!pEvent || (pEvent->Type != HANDLE_TYPE_EVENT))
|
2015-03-11 17:11:11 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-02-20 20:31:58 +03:00
|
|
|
static int EventGetFd(HANDLE handle)
|
|
|
|
{
|
|
|
|
WINPR_EVENT* event = (WINPR_EVENT*)handle;
|
|
|
|
|
2015-03-11 19:57:01 +03:00
|
|
|
if (!EventIsHandled(handle))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return event->pipe_fd[0];
|
2015-03-11 17:11:11 +03:00
|
|
|
}
|
|
|
|
|
2019-02-08 11:05:59 +03:00
|
|
|
static BOOL EventCloseHandle_(WINPR_EVENT* event)
|
2017-02-20 20:31:58 +03:00
|
|
|
{
|
2019-02-08 11:05:59 +03:00
|
|
|
if (!event)
|
2015-03-11 17:11:11 +03:00
|
|
|
return FALSE;
|
|
|
|
|
2015-07-03 10:25:41 +03:00
|
|
|
if (!event->bAttached)
|
|
|
|
{
|
|
|
|
if (event->pipe_fd[0] != -1)
|
|
|
|
{
|
|
|
|
close(event->pipe_fd[0]);
|
|
|
|
event->pipe_fd[0] = -1;
|
|
|
|
}
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-07-03 10:25:41 +03:00
|
|
|
if (event->pipe_fd[1] != -1)
|
|
|
|
{
|
|
|
|
close(event->pipe_fd[1]);
|
|
|
|
event->pipe_fd[1] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 11:05:59 +03:00
|
|
|
free(event->name);
|
2015-07-03 10:25:41 +03:00
|
|
|
free(event);
|
|
|
|
return TRUE;
|
2015-03-11 17:11:11 +03:00
|
|
|
}
|
|
|
|
|
2019-02-08 11:05:59 +03:00
|
|
|
static BOOL EventCloseHandle(HANDLE handle)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_EVENT* event = (WINPR_EVENT*)handle;
|
2019-02-08 11:05:59 +03:00
|
|
|
|
|
|
|
if (!EventIsHandled(handle))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return EventCloseHandle_(event);
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static HANDLE_OPS ops = { EventIsHandled, EventCloseHandle,
|
|
|
|
EventGetFd, NULL, /* CleanupHandle */
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL };
|
2015-03-16 02:29:37 +03:00
|
|
|
|
2017-02-20 20:31:58 +03:00
|
|
|
HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState,
|
|
|
|
LPCWSTR lpName)
|
2019-02-07 18:43:55 +03:00
|
|
|
{
|
|
|
|
HANDLE handle;
|
|
|
|
char* name = NULL;
|
|
|
|
|
|
|
|
if (lpName)
|
|
|
|
{
|
|
|
|
int rc = ConvertFromUnicode(CP_UTF8, 0, lpName, -1, &name, 0, NULL, NULL);
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle = CreateEventA(lpEventAttributes, bManualReset, bInitialState, name);
|
|
|
|
free(name);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE CreateEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState,
|
|
|
|
LPCSTR lpName)
|
2012-09-18 20:57:19 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_EVENT* event = (WINPR_EVENT*)calloc(1, sizeof(WINPR_EVENT));
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2019-02-07 18:43:55 +03:00
|
|
|
if (lpEventAttributes)
|
|
|
|
WLog_WARN(TAG, "%s [%s] does not support lpEventAttributes", __FUNCTION__, lpName);
|
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!event)
|
|
|
|
return NULL;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2019-02-08 11:05:59 +03:00
|
|
|
if (lpName)
|
|
|
|
event->name = strdup(lpName);
|
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
event->bAttached = FALSE;
|
|
|
|
event->bManualReset = bManualReset;
|
|
|
|
event->ops = &ops;
|
2015-07-03 10:25:41 +03:00
|
|
|
WINPR_HANDLE_SET_TYPE_AND_MODE(event, HANDLE_TYPE_EVENT, FD_READ);
|
2015-04-03 17:21:01 +03:00
|
|
|
|
|
|
|
if (!event->bManualReset)
|
|
|
|
WLog_ERR(TAG, "auto-reset events not yet implemented");
|
2012-09-19 03:24:03 +04:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
event->pipe_fd[0] = -1;
|
|
|
|
event->pipe_fd[1] = -1;
|
2018-02-05 01:16:12 +03:00
|
|
|
#ifdef HAVE_SYS_EVENTFD_H
|
2015-04-03 17:21:01 +03:00
|
|
|
event->pipe_fd[0] = eventfd(0, EFD_NONBLOCK);
|
2013-03-28 04:06:10 +04:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (event->pipe_fd[0] < 0)
|
2017-02-20 20:31:58 +03:00
|
|
|
goto fail;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2013-01-24 00:59:01 +04:00
|
|
|
#else
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (pipe(event->pipe_fd) < 0)
|
2017-02-20 20:31:58 +03:00
|
|
|
goto fail;
|
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#endif
|
2013-07-29 21:07:39 +04:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (bInitialState)
|
2019-02-21 14:20:06 +03:00
|
|
|
{
|
|
|
|
if (!SetEvent(event))
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2014-11-16 19:06:42 +03:00
|
|
|
return (HANDLE)event;
|
2017-02-20 20:31:58 +03:00
|
|
|
fail:
|
2019-02-08 11:05:59 +03:00
|
|
|
EventCloseHandle_(event);
|
2017-02-20 20:31:58 +03:00
|
|
|
return NULL;
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
|
|
|
|
2017-02-20 20:31:58 +03:00
|
|
|
HANDLE CreateEventExW(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCWSTR lpName, DWORD dwFlags,
|
|
|
|
DWORD dwDesiredAccess)
|
2012-09-18 20:57:19 +04:00
|
|
|
{
|
2019-02-07 18:43:55 +03:00
|
|
|
BOOL initial = FALSE;
|
|
|
|
BOOL manual = FALSE;
|
|
|
|
|
|
|
|
if (dwFlags & CREATE_EVENT_INITIAL_SET)
|
|
|
|
initial = TRUE;
|
|
|
|
|
|
|
|
if (dwFlags & CREATE_EVENT_MANUAL_RESET)
|
|
|
|
manual = TRUE;
|
|
|
|
|
|
|
|
if (dwDesiredAccess != 0)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "%s [%s] does not support dwDesiredAccess 0x%08" PRIx32, __FUNCTION__,
|
|
|
|
lpName, dwDesiredAccess);
|
2019-02-07 18:43:55 +03:00
|
|
|
|
|
|
|
return CreateEventW(lpEventAttributes, manual, initial, lpName);
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
|
|
|
|
2017-02-20 20:31:58 +03:00
|
|
|
HANDLE CreateEventExA(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCSTR lpName, DWORD dwFlags,
|
|
|
|
DWORD dwDesiredAccess)
|
2012-09-18 20:57:19 +04:00
|
|
|
{
|
2019-02-07 18:43:55 +03:00
|
|
|
BOOL initial = FALSE;
|
|
|
|
BOOL manual = FALSE;
|
|
|
|
|
|
|
|
if (dwFlags & CREATE_EVENT_INITIAL_SET)
|
|
|
|
initial = TRUE;
|
|
|
|
|
|
|
|
if (dwFlags & CREATE_EVENT_MANUAL_RESET)
|
|
|
|
manual = TRUE;
|
|
|
|
|
|
|
|
if (dwDesiredAccess != 0)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "%s [%s] does not support dwDesiredAccess 0x%08" PRIx32, __FUNCTION__,
|
|
|
|
lpName, dwDesiredAccess);
|
2019-02-07 18:43:55 +03:00
|
|
|
|
|
|
|
return CreateEventA(lpEventAttributes, manual, initial, lpName);
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE OpenEventW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName)
|
|
|
|
{
|
2019-02-07 18:43:55 +03:00
|
|
|
/* TODO: Implement */
|
|
|
|
WINPR_UNUSED(dwDesiredAccess);
|
|
|
|
WINPR_UNUSED(bInheritHandle);
|
|
|
|
WINPR_UNUSED(lpName);
|
|
|
|
WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
|
2012-09-18 20:57:19 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-05-29 22:14:26 +04:00
|
|
|
|
2012-09-19 02:36:13 +04:00
|
|
|
HANDLE OpenEventA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName)
|
|
|
|
{
|
2019-02-07 18:43:55 +03:00
|
|
|
/* TODO: Implement */
|
|
|
|
WINPR_UNUSED(dwDesiredAccess);
|
|
|
|
WINPR_UNUSED(bInheritHandle);
|
|
|
|
WINPR_UNUSED(lpName);
|
|
|
|
WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
|
2012-09-19 02:36:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-05 01:16:12 +03:00
|
|
|
#ifdef HAVE_SYS_EVENTFD_H
|
2015-04-28 09:30:45 +03:00
|
|
|
#if !defined(WITH_EVENTFD_READ_WRITE)
|
2014-08-07 06:06:01 +04:00
|
|
|
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
|
|
|
|
|
2012-09-18 20:57:19 +04:00
|
|
|
BOOL SetEvent(HANDLE hEvent)
|
|
|
|
{
|
2012-09-19 02:36:13 +04:00
|
|
|
ULONG Type;
|
2015-07-03 10:25:41 +03:00
|
|
|
WINPR_HANDLE* Object;
|
2012-09-19 02:36:13 +04:00
|
|
|
int length;
|
2012-12-03 23:57:15 +04:00
|
|
|
BOOL status;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_EVENT* event;
|
2012-12-03 23:57:15 +04:00
|
|
|
status = FALSE;
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2012-12-03 23:57:15 +04:00
|
|
|
if (winpr_Handle_GetInfo(hEvent, &Type, &Object))
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
event = (WINPR_EVENT*)Object;
|
2019-02-21 14:20:06 +03:00
|
|
|
|
2018-02-05 01:16:12 +03:00
|
|
|
#ifdef HAVE_SYS_EVENTFD_H
|
2013-02-01 03:47:44 +04:00
|
|
|
eventfd_t val = 1;
|
2013-02-10 02:13:53 +04:00
|
|
|
|
2013-02-01 03:47:44 +04:00
|
|
|
do
|
|
|
|
{
|
|
|
|
length = eventfd_write(event->pipe_fd[0], val);
|
2019-11-06 17:24:51 +03:00
|
|
|
} while ((length < 0) && (errno == EINTR));
|
2013-02-10 02:13:53 +04:00
|
|
|
|
2013-02-01 03:47:44 +04:00
|
|
|
status = (length == 0) ? TRUE : FALSE;
|
2013-01-24 00:59:01 +04:00
|
|
|
#else
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2013-02-01 03:47:44 +04:00
|
|
|
if (WaitForSingleObject(hEvent, 0) != WAIT_OBJECT_0)
|
|
|
|
{
|
2012-12-03 23:57:15 +04:00
|
|
|
length = write(event->pipe_fd[1], "-", 1);
|
|
|
|
|
|
|
|
if (length == 1)
|
|
|
|
status = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = TRUE;
|
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2013-02-01 03:47:44 +04:00
|
|
|
#endif
|
2012-12-03 23:57:15 +04:00
|
|
|
}
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2012-12-03 23:57:15 +04:00
|
|
|
return status;
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
2012-05-29 22:14:26 +04:00
|
|
|
|
2012-09-18 20:57:19 +04:00
|
|
|
BOOL ResetEvent(HANDLE hEvent)
|
|
|
|
{
|
2012-09-19 02:36:13 +04:00
|
|
|
ULONG Type;
|
2015-07-03 10:25:41 +03:00
|
|
|
WINPR_HANDLE* Object;
|
2012-09-19 02:36:13 +04:00
|
|
|
int length;
|
2015-06-17 15:13:28 +03:00
|
|
|
BOOL status = TRUE;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_EVENT* event;
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2015-06-17 15:13:28 +03:00
|
|
|
if (!winpr_Handle_GetInfo(hEvent, &Type, &Object))
|
|
|
|
return FALSE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
event = (WINPR_EVENT*)Object;
|
2012-12-03 23:57:15 +04:00
|
|
|
|
2015-06-17 15:13:28 +03:00
|
|
|
while (status && WaitForSingleObject(hEvent, 0) == WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
do
|
2012-12-03 23:57:15 +04:00
|
|
|
{
|
2018-02-05 01:16:12 +03:00
|
|
|
#ifdef HAVE_SYS_EVENTFD_H
|
2013-01-24 00:59:01 +04:00
|
|
|
eventfd_t value;
|
2015-06-17 15:13:28 +03:00
|
|
|
length = eventfd_read(event->pipe_fd[0], &value);
|
2013-01-24 00:59:01 +04:00
|
|
|
#else
|
2012-12-03 23:57:15 +04:00
|
|
|
length = read(event->pipe_fd[0], &length, 1);
|
2013-01-24 00:59:01 +04:00
|
|
|
#endif
|
2019-11-06 17:24:51 +03:00
|
|
|
} while ((length < 0) && (errno == EINTR));
|
2015-06-17 15:13:28 +03:00
|
|
|
|
|
|
|
if (length < 0)
|
|
|
|
status = FALSE;
|
2012-09-19 02:36:13 +04:00
|
|
|
}
|
|
|
|
|
2012-12-03 23:57:15 +04:00
|
|
|
return status;
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
2012-09-19 02:36:13 +04:00
|
|
|
|
|
|
|
#endif
|
2012-11-27 03:02:41 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
HANDLE CreateFileDescriptorEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
|
|
|
|
BOOL bInitialState, int FileDescriptor, ULONG mode)
|
2012-11-27 03:02:41 +04:00
|
|
|
{
|
2012-11-27 05:34:36 +04:00
|
|
|
#ifndef _WIN32
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_EVENT* event;
|
2012-11-27 03:02:41 +04:00
|
|
|
HANDLE handle = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
event = (WINPR_EVENT*)calloc(1, sizeof(WINPR_EVENT));
|
2012-11-27 03:02:41 +04:00
|
|
|
|
|
|
|
if (event)
|
|
|
|
{
|
|
|
|
event->bAttached = TRUE;
|
|
|
|
event->bManualReset = bManualReset;
|
|
|
|
event->pipe_fd[0] = FileDescriptor;
|
|
|
|
event->pipe_fd[1] = -1;
|
2015-03-16 02:29:37 +03:00
|
|
|
event->ops = &ops;
|
2015-07-03 10:25:41 +03:00
|
|
|
WINPR_HANDLE_SET_TYPE_AND_MODE(event, HANDLE_TYPE_EVENT, mode);
|
2019-11-06 17:24:51 +03:00
|
|
|
handle = (HANDLE)event;
|
2012-11-27 03:02:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return handle;
|
2012-11-27 05:34:36 +04:00
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2012-11-27 03:02:41 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
HANDLE CreateFileDescriptorEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
|
|
|
|
BOOL bInitialState, int FileDescriptor, ULONG mode)
|
2012-11-27 03:02:41 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return CreateFileDescriptorEventW(lpEventAttributes, bManualReset, bInitialState,
|
|
|
|
FileDescriptor, mode);
|
2012-11-27 03:02:41 +04:00
|
|
|
}
|
|
|
|
|
2012-12-21 07:32:02 +04:00
|
|
|
/**
|
|
|
|
* Returns an event based on the handle returned by GetEventWaitObject()
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
HANDLE CreateWaitObjectEvent(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
|
|
|
|
BOOL bInitialState, void* pObject)
|
2012-12-21 07:32:02 +04:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
2019-11-06 17:24:51 +03:00
|
|
|
return CreateFileDescriptorEventW(lpEventAttributes, bManualReset, bInitialState,
|
|
|
|
(int)(ULONG_PTR)pObject, WINPR_FD_READ);
|
2012-12-21 07:32:02 +04:00
|
|
|
#else
|
|
|
|
HANDLE hEvent = NULL;
|
2017-02-20 20:31:58 +03:00
|
|
|
DuplicateHandle(GetCurrentProcess(), pObject, GetCurrentProcess(), &hEvent, 0, FALSE,
|
|
|
|
DUPLICATE_SAME_ACCESS);
|
2012-12-21 07:32:02 +04:00
|
|
|
return hEvent;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-12-19 21:16:39 +04:00
|
|
|
/*
|
|
|
|
* Returns inner file descriptor for usage with select()
|
|
|
|
* This file descriptor is not usable on Windows
|
|
|
|
*/
|
|
|
|
|
2012-11-27 03:02:41 +04:00
|
|
|
int GetEventFileDescriptor(HANDLE hEvent)
|
|
|
|
{
|
2012-11-27 05:34:36 +04:00
|
|
|
#ifndef _WIN32
|
2019-10-02 15:58:32 +03:00
|
|
|
return winpr_Handle_getFd(hEvent);
|
2012-11-27 05:34:36 +04:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2012-11-27 03:02:41 +04:00
|
|
|
}
|
2012-12-19 21:16:39 +04:00
|
|
|
|
2013-05-01 01:16:38 +04:00
|
|
|
/*
|
|
|
|
* Set inner file descriptor for usage with select()
|
|
|
|
* This file descriptor is not usable on Windows
|
|
|
|
*/
|
|
|
|
|
2015-07-03 10:25:41 +03:00
|
|
|
int SetEventFileDescriptor(HANDLE hEvent, int FileDescriptor, ULONG mode)
|
2013-05-01 01:16:38 +04:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
ULONG Type;
|
2015-07-03 10:25:41 +03:00
|
|
|
WINPR_HANDLE* Object;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_EVENT* event;
|
2013-05-01 01:16:38 +04:00
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hEvent, &Type, &Object))
|
|
|
|
return -1;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
event = (WINPR_EVENT*)Object;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2020-01-10 19:11:50 +03:00
|
|
|
if (!event->bAttached && event->pipe_fd[0] >= 0 && event->pipe_fd[0] != FileDescriptor)
|
2016-05-23 14:04:44 +03:00
|
|
|
close(event->pipe_fd[0]);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-07-03 13:31:29 +03:00
|
|
|
event->bAttached = TRUE;
|
2015-07-03 10:25:41 +03:00
|
|
|
event->Mode = mode;
|
2013-05-01 01:16:38 +04:00
|
|
|
event->pipe_fd[0] = FileDescriptor;
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-12-19 21:16:39 +04:00
|
|
|
/**
|
|
|
|
* 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)
|
2012-12-19 21:16:39 +04:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
int fd;
|
2014-08-18 21:34:47 +04:00
|
|
|
void* obj;
|
2012-12-19 21:16:39 +04:00
|
|
|
fd = GetEventFileDescriptor(hEvent);
|
2019-11-06 17:24:51 +03:00
|
|
|
obj = ((void*)(long)fd);
|
2013-03-28 04:06:10 +04:00
|
|
|
return obj;
|
2012-12-19 21:16:39 +04:00
|
|
|
#else
|
|
|
|
return hEvent;
|
|
|
|
#endif
|
|
|
|
}
|