mtx: add basic synchronization primitives
This commit is contained in:
parent
582758e868
commit
ad422894f2
7 changed files with 193 additions and 9 deletions
|
|
@ -18,16 +18,17 @@ struct clist {
|
|||
struct clist *prev;
|
||||
};
|
||||
|
||||
#define CLIST_DEFINE(name) { \
|
||||
.next = &(name), \
|
||||
.prev = &(name), \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Declare and define a clist.
|
||||
* @brief Statically declare and define a clist.
|
||||
*
|
||||
* @param name Name of the list
|
||||
*/
|
||||
#define CLIST(name) \
|
||||
struct clist name = { \
|
||||
.next = &name, \
|
||||
.prev = &name, \
|
||||
}
|
||||
#define CLIST(name) struct clist name = CLIST_DEFINE(name)
|
||||
|
||||
/**
|
||||
* @brief Initialize a new clist.
|
||||
|
|
|
|||
61
include/gay/mutex.h
Normal file
61
include/gay/mutex.h
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/* See the end of this file for copyright and license terms. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gay/clist.h>
|
||||
#include <gay/types.h>
|
||||
|
||||
struct task;
|
||||
|
||||
typedef struct {
|
||||
atom_t lock;
|
||||
} spin_t;
|
||||
|
||||
#define SPIN(name) struct spin name = { \
|
||||
.lock = { ._value = 0 }, \
|
||||
}
|
||||
|
||||
void spin_init(spin_t *spin);
|
||||
void spin_lock(spin_t *spin);
|
||||
int spin_trylock(spin_t *spin);
|
||||
void spin_unlock(spin_t *spin);
|
||||
|
||||
struct lock_waiter {
|
||||
struct task *task;
|
||||
struct clist clink;
|
||||
};
|
||||
|
||||
struct mtx {
|
||||
atom_t lock;
|
||||
spin_t wait_queue_lock;
|
||||
struct clist wait_queue; /* -> struct lock_waiter::clink */
|
||||
};
|
||||
|
||||
#define MTX_DEFINE(name) { \
|
||||
.lock = ATOM_DEFINE(0), \
|
||||
.wait_queue_lock = { \
|
||||
.lock = ATOM_DEFINE(0) \
|
||||
}, \
|
||||
.wait_queue = CLIST_DEFINE(&(name).wait_queue), \
|
||||
}
|
||||
|
||||
#define MTX(name) struct mtx name = MTX_DEFINE(name)
|
||||
|
||||
void mtx_init(struct mtx *mutex);
|
||||
void mtx_lock(struct mtx *mutex);
|
||||
void mtx_unlock(struct mtx *mutex);
|
||||
int mtx_trylock(struct mtx *mutex);
|
||||
|
||||
/*
|
||||
* This file is part of GayBSD.
|
||||
* Copyright (c) 2021 fef <owo@fef.moe>.
|
||||
*
|
||||
* GayBSD is nonviolent software: you may only use, redistribute, and/or
|
||||
* modify it under the terms of the Cooperative Nonviolent Public License
|
||||
* (CNPL) as found in the LICENSE file in the source code root directory
|
||||
* or at <https://git.pixie.town/thufie/npl-builder>; either version 7
|
||||
* of the license, or (at your option) any later version.
|
||||
*
|
||||
* GayBSD comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPL for details.
|
||||
*/
|
||||
|
|
@ -44,7 +44,10 @@ extern struct task *current;
|
|||
* @brief Update the `current` task.
|
||||
* You will almost never need this because `switch_to()` does it automatically.
|
||||
*/
|
||||
#define set_current(task) (current = (task))
|
||||
__always_inline void set_current(struct task *task)
|
||||
{
|
||||
current = task;
|
||||
}
|
||||
|
||||
extern struct task kernel_task;
|
||||
|
||||
|
|
|
|||
|
|
@ -91,6 +91,17 @@ typedef __register_t register_t;
|
|||
typedef __u_register_t u_register_t;
|
||||
#endif /* not _U_REGISTER_T_DECLARED */
|
||||
|
||||
/**
|
||||
* @brief The primitive atomic integral type.
|
||||
* For use with the APIs defined in `arch/atomic.h`.
|
||||
*/
|
||||
typedef struct {
|
||||
volatile int _value;
|
||||
} atom_t;
|
||||
|
||||
/** @brief Atom definition body for static initialization of higher level components. */
|
||||
#define ATOM_DEFINE(val) { ._value = (val) }
|
||||
|
||||
#ifndef _PID_T_DECLARED
|
||||
#define _PID_T_DECLARED 1
|
||||
typedef int pid_t;
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ target_sources(gay_kernel PRIVATE
|
|||
irq.c
|
||||
kprintf.c
|
||||
main.c
|
||||
mutex.c
|
||||
sched.c
|
||||
util.c
|
||||
)
|
||||
|
|
|
|||
107
kernel/mutex.c
Normal file
107
kernel/mutex.c
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
/* See the end of this file for copyright and license terms. */
|
||||
|
||||
#include <arch/atom.h>
|
||||
|
||||
#include <gay/clist.h>
|
||||
#include <gay/mutex.h>
|
||||
#include <gay/sched.h>
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
void spin_init(spin_t *spin)
|
||||
{
|
||||
atom_write(&spin->lock, 0);
|
||||
}
|
||||
|
||||
void spin_lock(spin_t *spin)
|
||||
{
|
||||
while (atom_write(&spin->lock, 1) != 0);
|
||||
/* nothing */
|
||||
}
|
||||
|
||||
int spin_trylock(spin_t *spin)
|
||||
{
|
||||
if (atom_write(&spin->lock, 1) != 0)
|
||||
return -EAGAIN;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void spin_unlock(spin_t *spin)
|
||||
{
|
||||
atom_write(&spin->lock, 0);
|
||||
}
|
||||
|
||||
void mtx_init(struct mtx *mtx)
|
||||
{
|
||||
atom_write(&mtx->lock, 0);
|
||||
spin_init(&mtx->wait_queue_lock);
|
||||
clist_init(&mtx->wait_queue);
|
||||
}
|
||||
|
||||
void mtx_lock(struct mtx *mtx)
|
||||
{
|
||||
while (1) {
|
||||
/*
|
||||
* acquire a lock on the wait queue before trying to claim the
|
||||
* mutex itself to make sure the mutex isn't released while we
|
||||
* are inserting ourselves into the wait queue
|
||||
*/
|
||||
spin_lock(&mtx->wait_queue_lock);
|
||||
if (atom_write(&mtx->lock, 1) == 0) {
|
||||
spin_unlock(&mtx->wait_queue_lock);
|
||||
break;
|
||||
} else {
|
||||
struct task *cur = current;
|
||||
/*
|
||||
* It might not be the smartest idea to allocate this thing on
|
||||
* the stack because it's gonna blow up if the task somehow dies
|
||||
* before returning here. Let's see how this turns out.
|
||||
*/
|
||||
struct lock_waiter waiter = {
|
||||
.task = cur,
|
||||
};
|
||||
clist_add(&mtx->wait_queue, &waiter.clink);
|
||||
spin_unlock(&mtx->wait_queue_lock);
|
||||
|
||||
cur->state = TASK_BLOCKED;
|
||||
schedule();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int mtx_trylock(struct mtx *mtx)
|
||||
{
|
||||
if (atom_write(&mtx->lock, 1) != 0)
|
||||
return -EAGAIN;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mtx_unlock(struct mtx *mtx)
|
||||
{
|
||||
spin_lock(&mtx->wait_queue_lock);
|
||||
if (!clist_is_empty(&mtx->wait_queue)) {
|
||||
struct lock_waiter *waiter =
|
||||
clist_first_entry(&mtx->wait_queue, typeof(*waiter), clink);
|
||||
clist_del(&waiter->clink);
|
||||
spin_unlock(&mtx->wait_queue_lock);
|
||||
waiter->task->state = TASK_READY;
|
||||
switch_to(waiter->task, current);
|
||||
} else {
|
||||
atom_write(&mtx->lock, 0);
|
||||
spin_unlock(&mtx->wait_queue_lock);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This file is part of GayBSD.
|
||||
* Copyright (c) 2021 fef <owo@fef.moe>.
|
||||
*
|
||||
* GayBSD is nonviolent software: you may only use, redistribute, and/or
|
||||
* modify it under the terms of the Cooperative Nonviolent Public License
|
||||
* (CNPL) as found in the LICENSE file in the source code root directory
|
||||
* or at <https://git.pixie.town/thufie/npl-builder>; either version 7
|
||||
* of the license, or (at your option) any later version.
|
||||
*
|
||||
* GayBSD comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPL for details.
|
||||
*/
|
||||
|
|
@ -74,9 +74,9 @@ try_again:
|
|||
void switch_to(struct task *new, struct task *old)
|
||||
{
|
||||
disable_intr();
|
||||
current = new;
|
||||
set_current(new);
|
||||
arch_switch_to(&new->tcb, &old->tcb);
|
||||
current = old;
|
||||
set_current(old);
|
||||
enable_intr();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue