<commit_before>#include "mupdf/helpers/mu-threads.h"

#ifdef DISABLE_MUTHREADS

#include <stdlib.h>

/* Null implementation. Just error out. */

int mu_create_semaphore(mu_semaphore *sem)
{
	return 1; /* Just Error */
}

void mu_destroy_semaphore(mu_semaphore *sem)
{
}

int mu_trigger_semaphore(mu_semaphore *sem)
{
	abort();
	return 1;
}

int mu_wait_semaphore(mu_semaphore *sem)
{
	abort();
	return 1;
}

int mu_create_thread(mu_thread *th, mu_thread_fn *fn, void *arg)
{
	return 1;
}

void mu_destroy_thread(mu_thread *th)
{
}

int mu_create_mutex(mu_mutex *mutex)
{
	return 1;
}

void mu_destroy_mutex(mu_mutex *mutex)
{
}

void mu_lock_mutex(mu_mutex *mutex)
{
	abort();
}

void mu_unlock_mutex(mu_mutex *mutex)
{
	abort();
}

#elif MU_THREAD_IMPL_TYPE == 1

/* Windows threads */
int mu_create_semaphore(mu_semaphore *sem)
{
	sem->handle = CreateSemaphore(NULL, 0, 1, NULL);
	return (sem->handle != NULL);
}

void mu_destroy_semaphore(mu_semaphore *sem)
{
	if (sem->handle == NULL)
		return;
	/* We can't sensibly handle this failing */
	(void)CloseHandle(sem->handle);
}

int mu_trigger_semaphore(mu_semaphore *sem)
{
	if (sem->handle == NULL)
		return 0;
	/* We can't sensibly handle this failing */
	return !ReleaseSemaphore(sem->handle, 1, NULL);
}

int mu_wait_semaphore(mu_semaphore *sem)
{
	if (sem->handle == NULL)
		return 0;
	/* We can't sensibly handle this failing */
	return !WaitForSingleObject(sem->handle, INFINITE);
}

static DWORD WINAPI thread_starter(LPVOID arg)
{
	mu_thread *th = (mu_thread *)arg;

	th->fn(th->arg);

	return 0;
}

int mu_create_thread(mu_thread *th, mu_thread_fn *fn, void *arg)
{
	th->fn = fn;
	th->arg = arg;
	th->handle = CreateThread(NULL, 0, thread_starter, th, 0, NULL);

	return (th->handle != NULL);
}

void mu_destroy_thread(mu_thread *th)
{
	if (th->handle == NULL)
		return;
	/* We can't sensibly handle this failing */
	(void)WaitForSingleObject(th->handle, INFINITE);
	(void)CloseHandle(th->handle);
	th->handle = NULL;<commit_msg>mu-threads: Fix windows thread and semaphore creation<commit_after>#include "mupdf/helpers/mu-threads.h"

#ifdef DISABLE_MUTHREADS

#include <stdlib.h>

/* Null implementation. Just error out. */

int mu_create_semaphore(mu_semaphore *sem)
{
	return 1; /* Just Error */
}

void mu_destroy_semaphore(mu_semaphore *sem)
{
}

int mu_trigger_semaphore(mu_semaphore *sem)
{
	abort();
	return 1;
}

int mu_wait_semaphore(mu_semaphore *sem)
{
	abort();
	return 1;
}

int mu_create_thread(mu_thread *th, mu_thread_fn *fn, void *arg)
{
	return 1;
}

void mu_destroy_thread(mu_thread *th)
{
}

int mu_create_mutex(mu_mutex *mutex)
{
	return 1;
}

void mu_destroy_mutex(mu_mutex *mutex)
{
}

void mu_lock_mutex(mu_mutex *mutex)
{
	abort();
}

void mu_unlock_mutex(mu_mutex *mutex)
{
	abort();
}

#elif MU_THREAD_IMPL_TYPE == 1

/* Windows threads */
int mu_create_semaphore(mu_semaphore *sem)
{
	sem->handle = CreateSemaphore(NULL, 0, 1, NULL);
	return (sem->handle == NULL);
}

void mu_destroy_semaphore(mu_semaphore *sem)
{
	if (sem->handle == NULL)
		return;
	/* We can't sensibly handle this failing */
	(void)CloseHandle(sem->handle);
}

int mu_trigger_semaphore(mu_semaphore *sem)
{
	if (sem->handle == NULL)
		return 0;
	/* We can't sensibly handle this failing */
	return !ReleaseSemaphore(sem->handle, 1, NULL);
}

int mu_wait_semaphore(mu_semaphore *sem)
{
	if (sem->handle == NULL)
		return 0;
	/* We can't sensibly handle this failing */
	return !WaitForSingleObject(sem->handle, INFINITE);
}

static DWORD WINAPI thread_starter(LPVOID arg)
{
	mu_thread *th = (mu_thread *)arg;

	th->fn(th->arg);

	return 0;
}

int mu_create_thread(mu_thread *th, mu_thread_fn *fn, void *arg)
{
	th->fn = fn;
	th->arg = arg;
	th->handle = CreateThread(NULL, 0, thread_starter, th, 0, NULL);

	return (th->handle == NULL);
}

void mu_destroy_thread(mu_thread *th)
{
	if (th->handle == NULL)
		return;
	/* We can't sensibly handle this failing */
	(void)WaitForSingleObject(th->handle, INFINITE);
	(void)CloseHandle(th->handle);
	th->handle = NULL;
{'fraction_non_alphanumeric': 0.14221780208851317, 'fraction_numerical': 0.0069617105917454, 'mean_word_length': 4.864431486880466, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3732669', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1638, 'n_words': 359}