You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.5 KiB
C

/* 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_DEFINE { \
.lock = ATOM_DEFINE(0), \
}
#define SPIN(name) struct spin name = SPIN_DEFINE
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 clist clink;
struct task *task;
};
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 = SPIN_DEFINE, \
.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.
*/