<commit_before>/**
 * WinPR: Windows Portable Runtime
 * Synchronization Functions
 *
 * Copyright 2012 <NAME> <<EMAIL>>
 *
 * 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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <winpr/synch.h>

#ifndef _WIN32

#include "synch.h"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

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

#include <errno.h>

#include "../handle/handle.h"
#include "../pipe/pipe.h"

#include "../log.h"
#define TAG WINPR_TAG("synch.event")

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

static BOOL EventCloseHandle(HANDLE handle);

static BOOL EventIsHandled(HANDLE handle)
{
	WINPR_TIMER* pEvent = (WINPR_TIMER*) handle;

	if (!pEvent || (pEvent->Type != HANDLE_TYPE_EVENT))
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}

	return TRUE;
}

static int EventGetFd(HANDLE handle) {
	WINPR_EVENT *event = (WINPR_EVENT *)handle;
	if (!EventIsHandled(handle))
		return -1;

	return event->pipe_fd[0];
}

BOOL EventCloseHandle(HANDLE handle) {
	WINPR_EVENT* event = (WINPR_EVENT*) handle;

	if (!EventIsHandled(handle))
		return FALSE;

	if (!event->bAttached)
	{
		if (event->pipe_fd[0] != -1)
		{
			close(event->pipe_fd[0]);
			event->pipe_fd[0] = -1;
		}
		if (event->pipe_fd[1] != -1)
		{
			close(event->pipe_fd[1]);
			event->pipe_fd[1] = -1;
		}
	}

	free(event);
	return TRUE;
}

static HANDLE_OPS ops = {
		EventIsHandled,
		EventCloseHandle,
		EventGetFd,
		NULL /* CleanupHandle */
};

HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCWSTR lpName)
{
	WINPR_EVENT* event;

	event = (WINPR_EVENT*) calloc(1, sizeof(WINPR_EVENT));
	if (!event)
		return NULL;
	event->bAttached = FALSE;
	event->bManualReset = bManualReset;
	event->ops = &ops;
	WINPR_HANDLE_SET_TYPE_AND_MODE(event, HANDLE_TYPE_EVENT, FD_READ);

	if (!event->bManualReset)
	{
		WLog_ERR(TAG, "auto-reset events not yet implemented");
	}

	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)
	{
		WLog_ERR(TAG, "failed to create event");
		free(event);
		return NULL;
	}

#else
	if (pipe(event->pipe_fd) < 0)
	{
		WLog_ERR(TAG, "failed to create event");
		free(event);
		return NULL;
	}
#endif

	if (bInitialState)
		SetEvent(event);

	if (!cs.LockSemaphore && !InitializeCriticalSectionEx(&cs, 0, 0))
	{
		if (event->pipe_fd[0] != -1)
			close(event->pipe_fd[0]);
		if (event->pipe_fd[1] != -1)
			close(event->pipe_fd[1]);
		free(event);
		return NULL;
	}

	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;
}<commit_msg>winpr/event: remove critical section<commit_after>/**
 * WinPR: Windows Portable Runtime
 * Synchronization Functions
 *
 * Copyright 2012 <NAME> <<EMAIL>>
 *
 * 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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <winpr/synch.h>

#ifndef _WIN32

#include "synch.h"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

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

#include <errno.h>

#include "../handle/handle.h"
#include "../pipe/pipe.h"

#include "../log.h"
#define TAG WINPR_TAG("synch.event")

static BOOL EventCloseHandle(HANDLE handle);

static BOOL EventIsHandled(HANDLE handle)
{
	WINPR_TIMER* pEvent = (WINPR_TIMER*) handle;

	if (!pEvent || (pEvent->Type != HANDLE_TYPE_EVENT))
	{
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}

	return TRUE;
}

static int EventGetFd(HANDLE handle) {
	WINPR_EVENT *event = (WINPR_EVENT *)handle;
	if (!EventIsHandled(handle))
		return -1;

	return event->pipe_fd[0];
}

BOOL EventCloseHandle(HANDLE handle) {
	WINPR_EVENT* event = (WINPR_EVENT*) handle;

	if (!EventIsHandled(handle))
		return FALSE;

	if (!event->bAttached)
	{
		if (event->pipe_fd[0] != -1)
		{
			close(event->pipe_fd[0]);
			event->pipe_fd[0] = -1;
		}
		if (event->pipe_fd[1] != -1)
		{
			close(event->pipe_fd[1]);
			event->pipe_fd[1] = -1;
		}
	}

	free(event);
	return TRUE;
}

static HANDLE_OPS ops = {
		EventIsHandled,
		EventCloseHandle,
		EventGetFd,
		NULL /* CleanupHandle */
};

HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCWSTR lpName)
{
	WINPR_EVENT* event;

	event = (WINPR_EVENT*) calloc(1, sizeof(WINPR_EVENT));
	if (!event)
		return NULL;
	event->bAttached = FALSE;
	event->bManualReset = bManualReset;
	event->ops = &ops;
	WINPR_HANDLE_SET_TYPE_AND_MODE(event, HANDLE_TYPE_EVENT, FD_READ);

	if (!event->bManualReset)
	{
		WLog_ERR(TAG, "auto-reset events not yet implemented");
	}

	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)
	{
		WLog_ERR(TAG, "failed to create event");
		free(event);
		return NULL;
	}

#else
	if (pipe(event->pipe_fd) < 0)
	{
		WLog_ERR(TAG, "failed to create event");
		free(event);
		return NULL;
	}
#endif

	if (bInitialState)
		SetEvent(event);

	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;
}
{'fraction_non_alphanumeric': 0.12941176470588237, 'fraction_numerical': 0.010630758327427357, 'mean_word_length': 4.649319455564451, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5899919', 'n_tokens_mistral': 2733, 'n_tokens_neox': 2576, 'n_words': 643}