Sweden-Number/scheduler/event.c

230 lines
6.4 KiB
C

/*
* Win32 events
*
* Copyright 1998 Alexandre Julliard
*/
#include <assert.h>
#include <string.h>
#include "winerror.h"
#include "wine/unicode.h"
#include "syslevel.h"
#include "server.h"
/***********************************************************************
* CreateEventA (KERNEL32.156)
*/
HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
BOOL initial_state, LPCSTR name )
{
HANDLE ret;
DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
if (len >= MAX_PATH)
{
SetLastError( ERROR_FILENAME_EXCED_RANGE );
return 0;
}
SERVER_START_REQ
{
struct create_event_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
req->manual_reset = manual_reset;
req->initial_state = initial_state;
req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
SetLastError(0);
server_call( REQ_CREATE_EVENT );
ret = req->handle;
}
SERVER_END_REQ;
if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
return ret;
}
/***********************************************************************
* CreateEventW (KERNEL32.157)
*/
HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
BOOL initial_state, LPCWSTR name )
{
HANDLE ret;
DWORD len = name ? strlenW(name) : 0;
if (len >= MAX_PATH)
{
SetLastError( ERROR_FILENAME_EXCED_RANGE );
return 0;
}
SERVER_START_REQ
{
struct create_event_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
req->manual_reset = manual_reset;
req->initial_state = initial_state;
req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
SetLastError(0);
server_call( REQ_CREATE_EVENT );
ret = req->handle;
}
SERVER_END_REQ;
if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
return ret;
}
/***********************************************************************
* WIN16_CreateEvent (KERNEL.457)
*/
HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
{
return CreateEventA( NULL, manual_reset, initial_state, NULL );
}
/***********************************************************************
* OpenEventA (KERNEL32.536)
*/
HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
{
HANDLE ret;
DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
if (len >= MAX_PATH)
{
SetLastError( ERROR_FILENAME_EXCED_RANGE );
return 0;
}
SERVER_START_REQ
{
struct open_event_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
req->access = access;
req->inherit = inherit;
if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
server_call( REQ_OPEN_EVENT );
ret = req->handle;
}
SERVER_END_REQ;
if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
return ret;
}
/***********************************************************************
* OpenEventW (KERNEL32.537)
*/
HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
{
HANDLE ret;
DWORD len = name ? strlenW(name) : 0;
if (len >= MAX_PATH)
{
SetLastError( ERROR_FILENAME_EXCED_RANGE );
return 0;
}
SERVER_START_REQ
{
struct open_event_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
req->access = access;
req->inherit = inherit;
memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
server_call( REQ_OPEN_EVENT );
ret = req->handle;
}
SERVER_END_REQ;
if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
return ret;
}
/***********************************************************************
* EVENT_Operation
*
* Execute an event operation (set,reset,pulse).
*/
static BOOL EVENT_Operation( HANDLE handle, enum event_op op )
{
BOOL ret;
SERVER_START_REQ
{
struct event_op_request *req = server_alloc_req( sizeof(*req), 0 );
req->handle = handle;
req->op = op;
ret = !server_call( REQ_EVENT_OP );
}
SERVER_END_REQ;
return ret;
}
/***********************************************************************
* PulseEvent (KERNEL32.557)
*/
BOOL WINAPI PulseEvent( HANDLE handle )
{
return EVENT_Operation( handle, PULSE_EVENT );
}
/***********************************************************************
* SetEvent (KERNEL32.644)
*/
BOOL WINAPI SetEvent( HANDLE handle )
{
return EVENT_Operation( handle, SET_EVENT );
}
/***********************************************************************
* ResetEvent (KERNEL32.586)
*/
BOOL WINAPI ResetEvent( HANDLE handle )
{
return EVENT_Operation( handle, RESET_EVENT );
}
/***********************************************************************
* NOTE: The Win95 VWin32_Event routines given below are really low-level
* routines implemented directly by VWin32. The user-mode libraries
* implement Win32 synchronisation routines on top of these low-level
* primitives. We do it the other way around here :-)
*/
/***********************************************************************
* VWin32_EventCreate (KERNEL.442)
*/
HANDLE WINAPI VWin32_EventCreate(VOID)
{
HANDLE hEvent = CreateEventA( NULL, FALSE, 0, NULL );
return ConvertToGlobalHandle( hEvent );
}
/***********************************************************************
* VWin32_EventDestroy (KERNEL.443)
*/
VOID WINAPI VWin32_EventDestroy(HANDLE event)
{
CloseHandle( event );
}
/***********************************************************************
* VWin32_EventWait (KERNEL.450)
*/
VOID WINAPI VWin32_EventWait(HANDLE event)
{
SYSLEVEL_ReleaseWin16Lock();
WaitForSingleObject( event, INFINITE );
SYSLEVEL_RestoreWin16Lock();
}
/***********************************************************************
* VWin32_EventSet (KERNEL.451)
*/
VOID WINAPI VWin32_EventSet(HANDLE event)
{
SetEvent( event );
}