<commit_before>/**
 * @file
 * @brief TODO
 *
 * @date 07.09.2012
 * @author <NAME>
 */

#include <kernel/thread/event.h>
#include <kernel/thread/sched.h>

#define EVENT_STATUS_NONE     0
#define EVENT_STATUS_WAITING  1
#define EVENT_STATUS_HAPPEND  2

void event_init(struct event *e, const char *name) {
	sleepq_init(&e->sleepq);
	e->status = EVENT_STATUS_NONE;
	e->name = name;
}

int event_wait(struct event *e, unsigned long timeout) {
	int res = 0;

	assert(!critical_inside(__CRITICAL_HARDER(CRITICAL_SCHED_LOCK)));

	sched_lock();
	{
		if (e->status == EVENT_STATUS_HAPPEND) {
			e->status = EVENT_STATUS_NONE;
		} else {
			e->status = EVENT_STATUS_WAITING;
			res = sched_sleep_locked(&e->sleepq, timeout);
		}
	}
	sched_unlock();

	return res;
}

void event_notify(struct event *e) {
	sched_lock();
	{
		if (e->status == EVENT_STATUS_WAITING) {
			sched_wake_all(&e->sleepq);
		} else {
			e->status = EVENT_STATUS_HAPPEND;
		}
	}
	sched_unlock();
}

void event_clear(struct event *e) {
	sched_lock();
	{
		if (e->status == EVENT_STATUS_HAPPEND) {
			e->status = EVENT_STATUS_NONE;
		}
	}
	sched_unlock();
}

#if 0


static inline const char *event_name(struct event *e) {
	return e->name;
}<commit_msg>event: Fix bug in event<commit_after>/**
 * @file
 * @brief TODO
 *
 * @date 07.09.2012
 * @author <NAME>
 */

#include <kernel/thread/event.h>
#include <kernel/thread/sched.h>

#define EVENT_STATUS_NONE     0
#define EVENT_STATUS_WAITING  1
#define EVENT_STATUS_HAPPEND  2

void event_init(struct event *e, const char *name) {
	sleepq_init(&e->sleepq);
	e->status = EVENT_STATUS_NONE;
	e->name = name;
}

int event_wait(struct event *e, unsigned long timeout) {
	int res = 0;

	assert(!critical_inside(__CRITICAL_HARDER(CRITICAL_SCHED_LOCK)));

	sched_lock();
	{
		if (e->status == EVENT_STATUS_HAPPEND) {
			e->status = EVENT_STATUS_NONE;
		} else {
			e->status = EVENT_STATUS_WAITING;
			res = sched_sleep_locked(&e->sleepq, timeout);
		}
	}
	sched_unlock();

	return res;
}

void event_notify(struct event *e) {
	sched_lock();
	{
		if (e->status == EVENT_STATUS_WAITING) {
			e->status = EVENT_STATUS_NONE;
			sched_wake_all(&e->sleepq);
		} else {
			e->status = EVENT_STATUS_HAPPEND;
		}
	}
	sched_unlock();
}

void event_clear(struct event *e) {
	sched_lock();
	{
		if (e->status == EVENT_STATUS_HAPPEND) {
			e->status = EVENT_STATUS_NONE;
		}
	}
	sched_unlock();
}

#if 0


static inline const char *event_name(struct event *e) {
	return e->name;
}
{'fraction_non_alphanumeric': 0.14441747572815533, 'fraction_numerical': 0.010517799352750809, 'mean_word_length': 4.109504132231405, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 36, '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': '2819160', 'n_tokens_mistral': 1028, 'n_tokens_neox': 958, 'n_words': 194}