<commit_before>#include "lib/int.h"
#include "common/list.h"
#include "sync/spinlock.h"
#include "sync/semaphore.h"
#include "sched/sched.h"

void semaphore_down(semaphore_t *lock) {
    uint32_t flags;
    spin_lock_irqsave(&lock->lock, &flags);

    if(lock->count) {
        lock->count--;

        spin_unlock_irqstore(&lock->lock, flags);
    } else {
        list_add(&current->wait_list, &lock->waiters);

        spin_unlock_irqstore(&lock->lock, flags);

        task_sleep_current();
    }
}

void semaphore_up(semaphore_t *lock) {
    uint32_t flags;
    spin_lock_irqsave(&lock->lock, &flags);

    if(list_empty(&lock->waiters)) {
        lock->count++;
    } else {
        task_t *waiting = list_first(&lock->waiters, task_t, wait_list);

        task_wake(waiting);<commit_msg>Fix race condition in semaphore.c<commit_after>#include "lib/int.h"
#include "common/list.h"
#include "sync/spinlock.h"
#include "sync/semaphore.h"
#include "sched/sched.h"

void semaphore_down(semaphore_t *lock) {
    uint32_t flags;
    spin_lock_irqsave(&lock->lock, &flags);

    if(lock->count) {
        lock->count--;

        spin_unlock_irqstore(&lock->lock, flags);
    } else {
        list_add(&current->wait_list, &lock->waiters);

        task_sleep(current);

        spin_unlock_irqstore(&lock->lock, flags);

        sched_try_resched();
    }
}

void semaphore_up(semaphore_t *lock) {
    uint32_t flags;
    spin_lock_irqsave(&lock->lock, &flags);

    if(list_empty(&lock->waiters)) {
        lock->count++;
    } else {
        task_t *waiting = list_first(&lock->waiters, task_t, wait_list);

        task_wake(waiting);
{'fraction_non_alphanumeric': 0.1529051987767584, 'fraction_numerical': 0.004892966360856269, 'mean_word_length': 3.2604166666666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '2731959', 'n_tokens_mistral': 611, 'n_tokens_neox': 595, 'n_words': 90}