<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <signal.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/syscall.h>

#include <haka/timer.h>
#include <haka/error.h>
#include <haka/thread.h>
#include <haka/log.h>
#include <haka/compiler.h>
#include <haka/container/list2.h>


struct timer {
	struct list2_elem       list;
	bool                    armed:1;
	bool                    repeat:1;
	struct time             trigger_time;
	struct time             delay;
	timer_callback          callback;
	void                   *data;
	struct time_realm      *realm;
};

struct time_realm_state {
	struct time             time;
	timer_t                 timer;
	struct list2            sorted_timer;
	bool                    check_timer;
	struct time_realm      *realm;
};

static bool _time_realm_check(struct time_realm_state *state);

static void timer_handler(int sig, siginfo_t *si, void *uc)
{
	struct time_realm_state *state = (struct time_realm_state *)si->si_value.sival_ptr;
	if (state) {
		state->check_timer = true;
	}
}

static struct time_realm_state *create_time_realm_state(struct time_realm *realm)
{
	struct sigevent sev;

	struct time_realm_state *state = malloc(sizeof(struct time_realm_state));
	if (!state) {
		error("memory error");
		return NULL;
	}

	list2_init(&state->sorted_timer);

	if (realm->mode == TIME_REALM_REALTIME) {
		memset(&sev, 0, sizeof(sev));
		sev.sigev_notify = SIGEV_THREAD_ID;
		sev.sigev_signo = SIGALRM;
		sev.sigev_value.sival_ptr = state;
		sev._sigev_un._tid = syscall(SYS_gettid);

		if (timer_create(CLOCK_MONOTONIC, &sev, &state->timer)) {
			free(state);
			error("timer creation error: %s", errno_error(errno));
			return NULL;
		}
	}<commit_msg>Cygwin support: Fix compilation for timer.c with sigevent struct<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <signal.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>

#ifndef __CYGWIN__
#include <sys/syscall.h>
#endif

#include <haka/timer.h>
#include <haka/error.h>
#include <haka/thread.h>
#include <haka/log.h>
#include <haka/compiler.h>
#include <haka/container/list2.h>


struct timer {
	struct list2_elem       list;
	bool                    armed:1;
	bool                    repeat:1;
	struct time             trigger_time;
	struct time             delay;
	timer_callback          callback;
	void                   *data;
	struct time_realm      *realm;
};

struct time_realm_state {
	struct time             time;
	timer_t                 timer;
	struct list2            sorted_timer;
	bool                    check_timer;
	struct time_realm      *realm;
};

static bool _time_realm_check(struct time_realm_state *state);

static void timer_handler(int sig, siginfo_t *si, void *uc)
{
	struct time_realm_state *state = (struct time_realm_state *)si->si_value.sival_ptr;
	if (state) {
		state->check_timer = true;
	}
}

static struct time_realm_state *create_time_realm_state(struct time_realm *realm)
{
	struct sigevent sev;

	struct time_realm_state *state = malloc(sizeof(struct time_realm_state));
	if (!state) {
		error("memory error");
		return NULL;
	}

	list2_init(&state->sorted_timer);

	if (realm->mode == TIME_REALM_REALTIME) {
		memset(&sev, 0, sizeof(sev));
		sev.sigev_signo = SIGALRM;
		sev.sigev_value.sival_ptr = state;

#if defined(__CYGWIN__)
		sev.sigev_notify = 4; // Fake value for SIGEV_THREAD_ID
#else
		sev._sigev_un._tid = syscall(SYS_gettid);
		sev.sigev_notify = SIGEV_THREAD_ID;
#endif

		if (timer_create(CLOCK_MONOTONIC, &sev, &state->timer)) {
			free(state);
			error("timer creation error: %s", errno_error(errno));
			return NULL;
		}
	}
{'fraction_non_alphanumeric': 0.11506582155046319, 'fraction_numerical': 0.00560702096538274, 'mean_word_length': 3.2038934426229506, 'pattern_counts': {'":': 0, '<': 31, '<?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': '7439151', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1453, 'n_words': 361}