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.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2012-12-03 23:57:15 +04:00
|
|
|
CRITICAL_SECTION cs = { NULL, 0, 0, NULL, NULL, 0 };
|
|
|
|
|
2012-09-18 20:57:19 +04:00
|
|
|
HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCWSTR lpName)
|
|
|
|
{
|
2012-09-19 02:36:13 +04:00
|
|
|
WINPR_EVENT* event;
|
|
|
|
HANDLE handle = NULL;
|
|
|
|
|
|
|
|
event = (WINPR_EVENT*) malloc(sizeof(WINPR_EVENT));
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
if (event)
|
2012-09-19 02:36:13 +04:00
|
|
|
{
|
2012-11-27 03:02:41 +04:00
|
|
|
event->bAttached = FALSE;
|
2012-09-19 02:36:13 +04:00
|
|
|
event->bManualReset = bManualReset;
|
|
|
|
|
2012-09-19 03:24:03 +04:00
|
|
|
if (!event->bManualReset)
|
|
|
|
{
|
|
|
|
printf("CreateEventW: auto-reset events not yet implemented\n");
|
|
|
|
}
|
|
|
|
|
2012-09-19 02:36:13 +04:00
|
|
|
event->pipe_fd[0] = -1;
|
|
|
|
event->pipe_fd[1] = -1;
|
|
|
|
|
|
|
|
if (pipe(event->pipe_fd) < 0)
|
|
|
|
{
|
2012-09-23 21:54:14 +04:00
|
|
|
printf("CreateEventW: failed to create event\n");
|
2012-09-19 02:36:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle = winpr_Handle_Insert(HANDLE_TYPE_EVENT, event);
|
|
|
|
}
|
|
|
|
|
2012-12-03 23:57:15 +04:00
|
|
|
if (!cs.LockSemaphore)
|
|
|
|
InitializeCriticalSection(&cs);
|
|
|
|
|
2012-09-19 02:36:13 +04:00
|
|
|
return handle;
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
|
|
|
|
2012-09-19 02:36:13 +04:00
|
|
|
HANDLE CreateEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCSTR lpName)
|
2012-09-18 20:57:19 +04:00
|
|
|
{
|
2012-09-19 02:36:13 +04:00
|
|
|
return CreateEventW(lpEventAttributes, bManualReset, bInitialState, NULL);
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE CreateEventExW(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCWSTR lpName, DWORD dwFlags, DWORD dwDesiredAccess)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-19 02:36:13 +04:00
|
|
|
HANDLE CreateEventExA(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCSTR lpName, DWORD dwFlags, DWORD dwDesiredAccess)
|
2012-09-18 20:57:19 +04:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE OpenEventW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-18 20:57:19 +04:00
|
|
|
BOOL SetEvent(HANDLE hEvent)
|
|
|
|
{
|
2012-09-19 02:36:13 +04:00
|
|
|
ULONG Type;
|
|
|
|
PVOID Object;
|
|
|
|
int length;
|
2012-12-03 23:57:15 +04:00
|
|
|
BOOL status;
|
2012-09-19 02:36:13 +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
|
|
|
EnterCriticalSection(&cs);
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2012-12-03 23:57:15 +04:00
|
|
|
if (winpr_Handle_GetInfo(hEvent, &Type, &Object))
|
|
|
|
{
|
|
|
|
event = (WINPR_EVENT*) Object;
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2012-12-03 23:57:15 +04:00
|
|
|
if (!(WaitForSingleObject(hEvent, 0) == WAIT_OBJECT_0))
|
|
|
|
{
|
|
|
|
length = write(event->pipe_fd[1], "-", 1);
|
|
|
|
|
|
|
|
if (length == 1)
|
|
|
|
status = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2012-12-03 23:57:15 +04:00
|
|
|
LeaveCriticalSection(&cs);
|
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;
|
|
|
|
PVOID Object;
|
|
|
|
int length;
|
2012-12-03 23:57:15 +04:00
|
|
|
BOOL status;
|
2012-09-19 02:36:13 +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
|
|
|
EnterCriticalSection(&cs);
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2012-12-03 23:57:15 +04:00
|
|
|
if (winpr_Handle_GetInfo(hEvent, &Type, &Object))
|
2012-09-19 02:36:13 +04:00
|
|
|
{
|
2012-12-03 23:57:15 +04:00
|
|
|
event = (WINPR_EVENT*) Object;
|
|
|
|
|
|
|
|
while (WaitForSingleObject(hEvent, 0) == WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
length = read(event->pipe_fd[0], &length, 1);
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2012-12-03 23:57:15 +04:00
|
|
|
if (length == 1)
|
|
|
|
status = TRUE;
|
|
|
|
|
|
|
|
if (length != 1)
|
|
|
|
break;
|
|
|
|
}
|
2012-09-19 02:36:13 +04:00
|
|
|
}
|
|
|
|
|
2012-12-03 23:57:15 +04:00
|
|
|
LeaveCriticalSection(&cs);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
HANDLE CreateFileDescriptorEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, int FileDescriptor)
|
|
|
|
{
|
2012-11-27 05:34:36 +04:00
|
|
|
#ifndef _WIN32
|
2012-11-27 03:02:41 +04:00
|
|
|
WINPR_EVENT* event;
|
|
|
|
HANDLE handle = NULL;
|
|
|
|
|
|
|
|
event = (WINPR_EVENT*) malloc(sizeof(WINPR_EVENT));
|
|
|
|
|
|
|
|
if (event)
|
|
|
|
{
|
|
|
|
event->bAttached = TRUE;
|
|
|
|
event->bManualReset = bManualReset;
|
|
|
|
|
|
|
|
if (!event->bManualReset)
|
|
|
|
{
|
|
|
|
printf("CreateFileDescriptorEventW: auto-reset events not yet implemented\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
event->pipe_fd[0] = FileDescriptor;
|
|
|
|
event->pipe_fd[1] = -1;
|
|
|
|
|
|
|
|
handle = winpr_Handle_Insert(HANDLE_TYPE_EVENT, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return handle;
|
2012-11-27 05:34:36 +04:00
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2012-11-27 03:02:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE CreateFileDescriptorEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, int FileDescriptor)
|
|
|
|
{
|
|
|
|
return CreateFileDescriptorEventW(lpEventAttributes, bManualReset, bInitialState, FileDescriptor);
|
|
|
|
}
|
|
|
|
|
2012-12-21 07:32:02 +04:00
|
|
|
/**
|
|
|
|
* Returns an event based on the handle returned by GetEventWaitObject()
|
|
|
|
*/
|
|
|
|
HANDLE CreateWaitObjectEvent(LPSECURITY_ATTRIBUTES lpEventAttributes,
|
|
|
|
BOOL bManualReset, BOOL bInitialState, void* pObject)
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
2012-11-27 03:02:41 +04:00
|
|
|
ULONG Type;
|
|
|
|
PVOID Object;
|
|
|
|
WINPR_EVENT* event;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hEvent, &Type, &Object))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
event = (WINPR_EVENT*) Object;
|
|
|
|
|
|
|
|
return event->pipe_fd[0];
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
*/
|
|
|
|
|
|
|
|
void* GetEventWaitObject(HANDLE hEvent)
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = GetEventFileDescriptor(hEvent);
|
|
|
|
|
|
|
|
return ((void*) (long) fd);
|
|
|
|
#else
|
|
|
|
return hEvent;
|
|
|
|
#endif
|
|
|
|
}
|