<commit_before>#include <printk.h>
#include <kmutex.h>
#include <errno.h>
#include <scheduler.h>
#include <utils.h>
#include <thread.h>
#include <spinlock.h>
#include <export.h>
#include <syscall.h>

static void insert_waiting_thread(struct mutex *m, struct thread *t)
{
	struct thread *thread;

	if (m->waiting) {
		list_for_every_entry(&m->waiting_threads, thread, struct thread, event_node) {

#ifdef CONFIG_SCHEDULE_ROUND_ROBIN
			if (!list_next(&m->waiting_threads, &thread->event_node)) {
				list_add_after(&thread->event_node, &t->event_node);
				break;
			}
#elif defined(CONFIG_SCHEDULE_PRIORITY)
			if (t->priority > thread->priority)
				list_add_before(&thread->event_node, &t->event_node);
#endif
		}

	} else {
		list_add_head(&m->waiting_threads, &t->event_node);
	}


}

static void remove_waiting_thread(struct mutex *mutex, struct thread *t)
{
	list_delete(&t->event_node);
}

void kmutex_init(struct mutex *mutex)
{

	mutex->lock = 0;
	mutex->owner = NULL;
	mutex->waiting = 0;

	list_initialize(&mutex->waiting_threads);
}

void kmutex_lock(struct mutex *mutex)
{
	struct thread *current_thread = get_current_thread();

	if (mutex->lock) {
		debug_printk("mutex already locked\r\n");

		if (mutex->owner) {
			debug_printk("mutex has owner: %d\r\n", mutex->owner->pid);

			current_thread->state = THREAD_BLOCKED;
			remove_runnable_thread(current_thread);

			insert_waiting_thread(mutex, current_thread);
			mutex->waiting++;
<commit_msg>kernel: mutex: if locking over already locked mutex then request sched<commit_after>#include <printk.h>
#include <kmutex.h>
#include <errno.h>
#include <scheduler.h>
#include <utils.h>
#include <thread.h>
#include <spinlock.h>
#include <export.h>
#include <syscall.h>

static void insert_waiting_thread(struct mutex *m, struct thread *t)
{
	struct thread *thread;

	if (m->waiting) {
		list_for_every_entry(&m->waiting_threads, thread, struct thread, event_node) {

#ifdef CONFIG_SCHEDULE_ROUND_ROBIN
			if (!list_next(&m->waiting_threads, &thread->event_node)) {
				list_add_after(&thread->event_node, &t->event_node);
				break;
			}
#elif defined(CONFIG_SCHEDULE_PRIORITY)
			if (t->priority > thread->priority)
				list_add_before(&thread->event_node, &t->event_node);
#endif
		}

	} else {
		list_add_head(&m->waiting_threads, &t->event_node);
	}


}

static void remove_waiting_thread(struct mutex *mutex, struct thread *t)
{
	list_delete(&t->event_node);
}

void kmutex_init(struct mutex *mutex)
{

	mutex->lock = 0;
	mutex->owner = NULL;
	mutex->waiting = 0;

	list_initialize(&mutex->waiting_threads);
}

void kmutex_lock(struct mutex *mutex)
{
	struct thread *current_thread = get_current_thread();

	if (mutex->lock) {
		debug_printk("mutex already locked\r\n");

		if (mutex->owner) {
			debug_printk("mutex has owner: %d\r\n", mutex->owner->pid);

			current_thread->state = THREAD_BLOCKED;
			remove_runnable_thread(current_thread);

			insert_waiting_thread(mutex, current_thread);
			mutex->waiting++;
			arch_request_sched();

{'fraction_non_alphanumeric': 0.14471057884231536, 'fraction_numerical': 0.0013306719893546241, 'mean_word_length': 5.277661795407098, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 69, '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': '456282', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1096, 'n_words': 213}