lock: merge spinlock and mtx locks into one

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-08-13 14:19:10 -04:00
commit 2076f1dae1
Signed by: michaelsshaw
GPG key ID: 5A5E0FB20708A9E7
32 changed files with 158 additions and 169 deletions

View file

@ -1,10 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _MTX_H_
#define _MTX_H_
#include <kernel/spinlock.h>
void mtx_lock(mtx_t *l);
void mtx_unlock(mtx_t *l);
#endif /* _MTX_H_ */

View file

@ -1,17 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _SPINLOCK_H_
#define _SPINLOCK_H_
#include <kernel/types.h>
#include <kernel/common.h>
#define CLEANUP_SLOCK CLEANUP(cleanup_spinlock)
typedef u32 spinlock_t;
typedef spinlock_t mtx_t;
void spinlock_acquire(spinlock_t *lock);
void spinlock_release(spinlock_t *lock);
void cleanup_spinlock(spinlock_t **lock_ptr);
#endif /* _SPINLOCK_H_ */

View file

@ -1,6 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += atomic.o
obj-y += spinlock.o
obj-y += lock.o
obj-y += thread.o
obj-y += time.o
obj-y += tsc.o

View file

@ -1,39 +1,39 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/spinlock.h>
#include <kernel/lock.h>
#include <kernel/atomic.h>
#include <kernel/thread.h>
#include <arch/proc.h>
static uint64_t spinlock_attempt_acquire(spinlock_t *lock)
static uint64_t spinlock_attempt_acquire(lock_t *lock)
{
return atomic_cmpxchg(lock, 0, 1);
}
void spinlock_acquire(spinlock_t *lock)
void spin_lock(lock_t *lock)
{
while (spinlock_attempt_acquire(lock))
;
}
void spinlock_release(spinlock_t *lock)
void spin_unlock(lock_t *lock)
{
atomic_cmpxchg(lock, 1, 0);
}
void cleanup_spinlock(spinlock_t **lock_ptr)
void cleanup_spinlock(lock_t **lock_ptr)
{
if (lock_ptr && *lock_ptr)
spinlock_release(*lock_ptr);
spin_unlock(*lock_ptr);
}
void mtx_lock(mtx_t *l)
void mtx_lock(lock_t *l)
{
while (spinlock_attempt_acquire(l))
reschedule(THREAD_STATE_WAITING);
}
void mtx_unlock(mtx_t *l)
void mtx_unlock(lock_t *l)
{
spinlock_release(l);
spin_unlock(l);
}

View file

@ -4,7 +4,7 @@
#include <kernel/common.h>
#include <kernel/block.h>
#include <kernel/spinlock.h>
#include <kernel/lock.h>
#include <fs/vfs.h>

View file

@ -7,7 +7,7 @@
#define DC_TOMBSTONE ((struct dc_entry *)1)
struct dc_entry dcache[DCACHE_SIZE];
static spinlock_t dcache_locks[DCACHE_NLOCKS];
static lock_t dcache_locks[DCACHE_NLOCKS];
static u64 dcache_hash(struct dc_entry *parent, const char *name)
{
@ -17,7 +17,7 @@ static u64 dcache_hash(struct dc_entry *parent, const char *name)
return h;
}
static spinlock_t *dcache_lock_for(struct dc_entry *parent, const char *name)
static lock_t *dcache_lock_for(struct dc_entry *parent, const char *name)
{
u64 h = dcache_hash(parent, name);
return &dcache_locks[h & (DCACHE_NLOCKS - 1)];
@ -33,10 +33,10 @@ void dcache_init(void)
void dcache_insert(struct dc_entry *parent, dirent_t *dirent, const char *name)
{
spinlock_t *lock = dcache_lock_for(parent, name);
lock_t *lock = dcache_lock_for(parent, name);
u64 h = dcache_hash(parent, name);
spinlock_acquire(lock);
spin_lock(lock);
for (int i = 0; i < DCACHE_SIZE; i++) {
u32 b = (h + i) & (DCACHE_SIZE - 1);
struct dc_entry *s = &dcache[b];
@ -47,27 +47,27 @@ void dcache_insert(struct dc_entry *parent, dirent_t *dirent, const char *name)
s->parent = parent;
s->dirent = dirent;
memcpy(s->d_name, name, FILE_NAME_MAX_LEN + 1);
spinlock_release(lock);
spin_unlock(lock);
return;
}
}
spinlock_release(lock);
spin_unlock(lock);
}
void dcache_remove(struct dc_entry *d)
{
spinlock_t *lock = dcache_lock_for(d->parent, d->d_name);
lock_t *lock = dcache_lock_for(d->parent, d->d_name);
spinlock_acquire(lock);
spin_lock(lock);
dirent_free(d->dirent);
d->dirent = NULL;
d->parent = DC_TOMBSTONE;
spinlock_release(lock);
spin_unlock(lock);
}
struct dc_entry *dcache_lookup(struct dc_entry *parent, const char *name)
{
CLEANUP_SLOCK spinlock_t *lock = dcache_lock_for(parent, name);
CLEANUP_SLOCK lock_t *lock = dcache_lock_for(parent, name);
u64 h = dcache_hash(parent, name);
struct dc_entry *result = NULL;

View file

@ -52,8 +52,7 @@ int dirent_populate_children(dirent_t *dir)
kfree(dents);
return 0;
cleanup:
{
cleanup: {
list_item_t *item = inode->dirents.head;
while (item) {
dirent_t *dent = item->ptr;
@ -64,7 +63,7 @@ cleanup:
dirent_free(dent);
item = next;
}
}
}
list_unlock(&inode->dirents);
write_unlock(&inode->rwlock);
kfree(dents);
@ -162,21 +161,21 @@ void inode_dec_ref(inode_t *inode)
void dirent_inc_ref(dirent_t *dent)
{
spinlock_acquire(&dent->lock);
spin_lock(&dent->lock);
dent->refcount += 1;
spinlock_release(&dent->lock);
spin_unlock(&dent->lock);
}
void dirent_dec_ref(dirent_t *dent)
{
spinlock_acquire(&dent->lock);
spin_lock(&dent->lock);
if (dent->refcount == 0) {
spinlock_release(&dent->lock);
spin_unlock(&dent->lock);
return;
}
dent->refcount -= 1;
bool freed = (dent->refcount == 0);
spinlock_release(&dent->lock);
spin_unlock(&dent->lock);
if (freed) {
inode_dec_ref(dent->inode);

View file

@ -8,7 +8,7 @@
#include <lib/string.h>
static struct mount *root_mount = NULL;
static spinlock_t mount_lock = 0;
static lock_t mount_lock = 0;
fs_t *vfs_get_root(void)
{
@ -24,19 +24,19 @@ struct mount *vfs_get_root_mount(void)
struct mount *mount_find_child(struct mount *parent, dirent_t *dent)
{
spinlock_acquire(&mount_lock);
spin_lock(&mount_lock);
list_item_t *item = parent->children.head;
while (item) {
struct mount *m = item->ptr;
if (m->mountpoint == dent) {
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return m;
}
item = item->next;
}
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return NULL;
}
@ -65,16 +65,16 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags)
return -EINVAL;
if (!path) {
spinlock_acquire(&mount_lock);
spin_lock(&mount_lock);
if (root_mount) {
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -EBUSY;
}
struct mount *m = kzalloc(sizeof(*m), ALLOC_KERN);
if (!m) {
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -ENOMEM;
}
@ -86,7 +86,7 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags)
list_create(&m->children);
root_mount = m;
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return 0;
}
@ -101,14 +101,14 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags)
return -ENOTDIR;
}
spinlock_acquire(&mount_lock);
spin_lock(&mount_lock);
list_item_t *item = parent->children.head;
while (item) {
struct mount *m = item->ptr;
if (m->mountpoint == mp) {
dirent_dec_ref(mp);
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -EBUSY;
}
item = item->next;
@ -117,7 +117,7 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags)
struct mount *m = kzalloc(sizeof(*m), ALLOC_KERN);
if (!m) {
dirent_dec_ref(mp);
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -ENOMEM;
}
@ -134,11 +134,11 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags)
if (!m->child_link) {
dirent_dec_ref(mp);
kfree(m);
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -ENOMEM;
}
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return 0;
}
@ -153,7 +153,7 @@ int vfs_umount(const char *path)
if (res < 0)
return res;
spinlock_acquire(&mount_lock);
spin_lock(&mount_lock);
struct mount *target = NULL;
list_item_t *item = parent->children.head;
@ -169,17 +169,17 @@ int vfs_umount(const char *path)
dirent_dec_ref(mp);
if (!target) {
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -EINVAL;
}
if (target == root_mount) {
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -EINVAL;
}
if (target->children.head) {
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -EBUSY;
}
@ -189,6 +189,6 @@ int vfs_umount(const char *path)
dirent_dec_ref(target->mountpoint);
kfree(target);
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return 0;
}

View file

@ -76,7 +76,7 @@ static int sys_open(const char *path, int flags)
if (res < 0)
return res;
spinlock_acquire(&thread->fd_lock);
spin_lock(&thread->fd_lock);
int fdno = -1;
@ -109,7 +109,7 @@ static int sys_open(const char *path, int flags)
fdno = osize;
}
spinlock_release(&thread->fd_lock);
spin_unlock(&thread->fd_lock);
return fdno;
}
@ -125,12 +125,12 @@ static int sys_close(int fd)
if (!f->dirent)
return -EBADF;
spinlock_acquire(&thread->fd_lock);
spin_lock(&thread->fd_lock);
vfs_close(f->dirent);
memset(f, 0, sizeof(struct file));
spinlock_acquire(&thread->fd_lock);
spin_lock(&thread->fd_lock);
return 0;
}

View file

@ -58,20 +58,20 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent)
if (res < 0)
return -ENOENT;
spinlock_acquire(&dirent->lock);
spin_lock(&dirent->lock);
if (!dirent->inode) {
fs_t *fs = dent->fs;
u64 inum = dirent->ino_num;
res = fs->fs_ops.open_inode(fs, inum, dirent, &dirent->inode);
if (res < 0) {
spinlock_release(&dirent->lock);
spin_unlock(&dirent->lock);
dirent_dec_ref(dirent);
return res;
}
}
spinlock_release(&dirent->lock);
spin_unlock(&dirent->lock);
*out_dirent = dirent;
return i;

View file

@ -2,7 +2,7 @@
#ifndef _DCACHE_H_
#define _DCACHE_H_
#include <kernel/spinlock.h>
#include <kernel/lock.h>
#include <fs/vfs.h>
#include <lib/list.h>
@ -16,7 +16,7 @@
struct dc_entry {
struct dc_entry *parent;
struct mount *mount;
spinlock_t lock;
lock_t lock;
list_t children;
dirent_t *dirent;
char d_name[FILE_NAME_MAX_LEN + 1];

View file

@ -49,7 +49,7 @@ struct _dirent {
u64 flags;
u64 type;
spinlock_t lock;
lock_t lock;
u64 refcount;

View file

@ -2,7 +2,7 @@
#ifndef _VFS_H_
#define _VFS_H_
#include <kernel/spinlock.h>
#include <kernel/lock.h>
#include <kernel/slab.h>
#include <fs/inode.h>
@ -79,7 +79,7 @@ struct _fs {
bool mounted;
dirent_t *mount_point;
spinlock_t lock;
lock_t lock;
struct fs_ops fs_ops;
};

View file

@ -3,7 +3,7 @@
#define _DRIVER_H_
#include <kernel/common.h>
#include <kernel/spinlock.h>
#include <kernel/lock.h>
#include <lib/list.h>
#include <drivers/pci.h>

19
include/kernel/lock.h Normal file
View file

@ -0,0 +1,19 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _LOCK_H_
#define _LOCK_H_
#include <kernel/types.h>
#include <kernel/common.h>
#define CLEANUP_SLOCK CLEANUP(cleanup_spinlock)
typedef u32 lock_t;
void spin_lock(lock_t *lock);
void spin_unlock(lock_t *lock);
void cleanup_spinlock(lock_t **lock_ptr);
void mtx_lock(lock_t *l);
void mtx_unlock(lock_t *l);
#endif /* _LOCK_H_ */

View file

@ -3,7 +3,7 @@
#define _MEM_H_
#include <kernel/common.h>
#include <kernel/spinlock.h>
#include <kernel/lock.h>
#define ALIGN(_a) __attribute__((aligned(_a)))
#define PHYSICAL_PAGE_MASK (~(hhdm_start))

View file

@ -3,7 +3,7 @@
#define _SLAB_H_
#include <kernel/common.h>
#include <kernel/spinlock.h>
#include <kernel/lock.h>
#define ALLOC_KERN 0x1
#define ALLOC_USER 0x2
@ -19,7 +19,7 @@ typedef struct _mem_cache_t {
size_t num_members;
size_t num_free;
vaddr_t next_free;
spinlock_t lock;
lock_t lock;
struct _mem_cache_t *next;
} mem_cache_t;

View file

@ -3,7 +3,7 @@
#define _THREAD_H_
#include <kernel/types.h>
#include <kernel/spinlock.h>
#include <kernel/lock.h>
#include <kernel/mem.h>
#include <kernel/page.h>
#include <fs/vfs.h>
@ -33,7 +33,7 @@ struct file {
u64 flags;
u64 offset;
u64 refcount;
spinlock_t lock;
lock_t lock;
};
struct thread {
@ -53,15 +53,15 @@ struct thread {
pgd_t *pgd;
spinlock_t mem_lock;
lock_t mem_lock;
rbtree_t memmap;
spinlock_t alloc_lock;
lock_t alloc_lock;
rbtree_t alloc_map;
uaddr_t entry;
spinlock_t fd_lock;
lock_t fd_lock;
size_t fd_array_size;
struct file *fdt;

View file

@ -1,9 +1,9 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _ht_H_
#define _ht_H_
#ifndef _HASH_H_
#define _HASH_H_
#include <kernel/common.h>
#include <kernel/mtx.h>
#include <kernel/lock.h>
typedef struct ht_entry {
char *key;
@ -19,7 +19,7 @@ typedef struct {
size_t capacity;
size_t size;
mtx_t lock;
lock_t lock;
} ht_t;
ht_t *ht_create(size_t capacity);
@ -30,4 +30,4 @@ int ht_delete(ht_t *ht, const char *key);
void ht_lock(ht_t *ht);
void ht_unlock(ht_t *ht);
#endif /* _ht_H_ */
#endif /* _HASH_H_ */

View file

@ -2,7 +2,7 @@
#ifndef _LIST_H_
#define _LIST_H_
#include <kernel/spinlock.h>
#include <kernel/lock.h>
typedef struct _list_item {
union {
@ -17,7 +17,7 @@ typedef struct _list_item {
typedef struct _list_head {
struct _list_item *head;
struct _list_item *tail;
mtx_t lock;
lock_t lock;
} list_t;
void list_create(list_t *list);

View file

@ -3,7 +3,7 @@
#define _rbtree_tH_
#include <kernel/common.h>
#include <kernel/spinlock.h>
#include <kernel/lock.h>
#define RB_RED 0
#define RB_BLACK 1
@ -26,7 +26,7 @@ typedef struct _rbnode_t {
*/
typedef struct _rbtree_t {
rbnode_t *root;
spinlock_t lock;
lock_t lock;
size_t num_nodes;
} rbtree_t;
@ -40,12 +40,12 @@ rbnode_t *rbt_minimum(rbnode_t *node);
static inline void rbt_lock(rbtree_t *tree)
{
spinlock_acquire(&tree->lock);
spin_lock(&tree->lock);
}
static inline void rbt_unlock(rbtree_t *tree)
{
spinlock_release(&tree->lock);
spin_unlock(&tree->lock);
}
void rbt_init();

View file

@ -2,11 +2,11 @@
#ifndef _RWLOCK_H_
#define _RWLOCK_H_
#include <kernel/spinlock.h>
#include <kernel/lock.h>
#include <kernel/types.h>
typedef struct {
spinlock_t lock;
lock_t lock;
u64 readers;
bool writer;
} rwlock_t;

View file

@ -2,11 +2,11 @@
#ifndef _SEM_H_
#define _SEM_H_
#include <kernel/spinlock.h>
#include <kernel/lock.h>
typedef struct _sem_t {
u64 sem;
spinlock_t lock;
lock_t lock;
} sem_t;
bool sem_trywait(sem_t *sem);

View file

@ -7,24 +7,24 @@
static struct {
struct thread *head;
struct thread *tail;
spinlock_t lock;
lock_t lock;
} sched_queue;
void sched_add_thread(struct thread *t)
{
spinlock_acquire(&sched_queue.lock);
spin_lock(&sched_queue.lock);
t->next = NULL;
if (sched_queue.tail)
sched_queue.tail->next = t;
sched_queue.tail = t;
if (!sched_queue.head)
sched_queue.head = t;
spinlock_release(&sched_queue.lock);
spin_unlock(&sched_queue.lock);
}
void sched_remove_thread(struct thread *del)
{
spinlock_acquire(&sched_queue.lock);
spin_lock(&sched_queue.lock);
struct thread *t = sched_queue.head;
struct thread *p = NULL;
while (t) {
@ -43,12 +43,12 @@ void sched_remove_thread(struct thread *del)
p = t;
t = t->next;
}
spinlock_release(&sched_queue.lock);
spin_unlock(&sched_queue.lock);
}
static struct thread *dequeue_thread()
{
spinlock_acquire(&sched_queue.lock);
spin_lock(&sched_queue.lock);
struct thread *t = sched_queue.head;
if (t) {
sched_queue.head = t->next;
@ -58,7 +58,7 @@ static struct thread *dequeue_thread()
} else {
t = tls_get()->idle_thread;
}
spinlock_release(&sched_queue.lock);
spin_unlock(&sched_queue.lock);
return t;
}

View file

@ -19,9 +19,9 @@ tid_t alloc_tid()
void thread_mmap(struct thread *thread, paddr_t p, vaddr_t v, size_t len, u64 flags)
{
spinlock_acquire(&thread->mem_lock);
spin_lock(&thread->mem_lock);
page_map(thread->pgd, p, v, len, flags, &thread->memmap);
spinlock_release(&thread->mem_lock);
spin_unlock(&thread->mem_lock);
}
void *thread_alloc(struct thread *thread, size_t size)
@ -31,8 +31,8 @@ void *thread_alloc(struct thread *thread, size_t size)
static int copy_parent_memmap(struct thread *child, struct thread *parent)
{
spinlock_acquire(&parent->mem_lock);
spinlock_acquire(&child->mem_lock);
spin_lock(&parent->mem_lock);
spin_lock(&child->mem_lock);
rbnode_t *node = parent->memmap.root;
if (!node)
@ -46,8 +46,8 @@ static int copy_parent_memmap(struct thread *child, struct thread *parent)
void *pages = kalloc(area->len, ALLOC_USER);
if (!pages) {
spinlock_release(&child->mem_lock);
spinlock_release(&parent->mem_lock);
spin_unlock(&child->mem_lock);
spin_unlock(&parent->mem_lock);
return -ENOMEM;
}
@ -58,8 +58,8 @@ static int copy_parent_memmap(struct thread *child, struct thread *parent)
}
done:
spinlock_release(&child->mem_lock);
spinlock_release(&parent->mem_lock);
spin_unlock(&child->mem_lock);
spin_unlock(&parent->mem_lock);
return 0;
}
@ -119,14 +119,14 @@ struct thread *thread_fork(struct thread *parent)
return NULL;
}
spinlock_acquire(&parent->fd_lock);
spin_lock(&parent->fd_lock);
if (parent->fd_array_size) {
child->fd_array_size = parent->fd_array_size;
child->fdt = kalloc(child->fd_array_size * sizeof(struct file), ALLOC_KERN);
if (child->fdt)
memcpy(child->fdt, parent->fdt, child->fd_array_size * sizeof(struct file));
}
spinlock_release(&parent->fd_lock);
spin_unlock(&parent->fd_lock);
child->group = child->tid;

View file

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/slab.h>
#include <kernel/mtx.h>
#include <lib/hashchain.h>
#include <lib/math.h>
#include <lib/string.h>

View file

@ -1,5 +1,4 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/mtx.h>
#include <kernel/types.h>
#include <kernel/slab.h>
#include <lib/list.h>

View file

@ -40,7 +40,7 @@
#include <drivers/serial.h>
#include <kernel/tty.h>
#include <kernel/spinlock.h>
#include <kernel/lock.h>
// define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the
// printf_config.h header file
@ -941,14 +941,14 @@ static int _vsnprintf(out_fct_type out, char *buffer, const size_t maxlen, const
int printf_(const char *format, ...)
{
static spinlock_t lock = 0;
spinlock_acquire(&lock);
static lock_t lock = 0;
spin_lock(&lock);
va_list va;
va_start(va, format);
char buffer[1];
const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va);
va_end(va);
spinlock_release(&lock);
spin_unlock(&lock);
return ret;
}

View file

@ -4,41 +4,41 @@
void read_lock(rwlock_t *lock)
{
while (1) {
spinlock_acquire(&lock->lock);
spin_lock(&lock->lock);
if (!lock->writer) {
lock->readers++;
spinlock_release(&lock->lock);
spin_unlock(&lock->lock);
return;
}
spinlock_release(&lock->lock);
spin_unlock(&lock->lock);
}
}
void read_unlock(rwlock_t *lock)
{
spinlock_acquire(&lock->lock);
spin_lock(&lock->lock);
lock->readers--;
spinlock_release(&lock->lock);
spin_unlock(&lock->lock);
}
void write_lock(rwlock_t *lock)
{
while (1) {
spinlock_acquire(&lock->lock);
spin_lock(&lock->lock);
if (!lock->writer && lock->readers == 0) {
lock->writer = true;
spinlock_release(&lock->lock);
spin_unlock(&lock->lock);
return;
}
spinlock_release(&lock->lock);
spin_unlock(&lock->lock);
}
}
void write_unlock(rwlock_t *lock)
{
spinlock_acquire(&lock->lock);
spin_lock(&lock->lock);
lock->writer = false;
spinlock_release(&lock->lock);
spin_unlock(&lock->lock);
}
void rwlock_init(rwlock_t *lock)

View file

@ -5,12 +5,12 @@ bool sem_trywait(sem_t *sem)
{
bool ret = false;
spinlock_acquire(&sem->lock);
spin_lock(&sem->lock);
if (sem->sem > 0) {
sem->sem -= 1;
ret = true;
}
spinlock_release(&sem->lock);
spin_unlock(&sem->lock);
return ret;
}
@ -23,9 +23,9 @@ void sem_wait(sem_t *sem)
void sem_post(sem_t *sem)
{
spinlock_acquire(&sem->lock);
spin_lock(&sem->lock);
sem->sem += 1;
spinlock_release(&sem->lock);
spin_unlock(&sem->lock);
}
void sem_init(sem_t *sem, size_t n_res)

View file

@ -2,7 +2,7 @@
#include <kernel/common.h>
#include <kernel/buddy.h>
#include <kernel/mem.h>
#include <kernel/spinlock.h>
#include <kernel/lock.h>
#include <arch/page.h>
#include <lib/stdio.h>
#include <lib/math.h>
@ -36,7 +36,7 @@ struct buddy_zone {
vaddr_t usable_base;
size_t usable_len;
spinlock_t lock;
lock_t lock;
};
static struct buddy_zone buddy_zones[128] = {0};
@ -112,7 +112,7 @@ static void alloc_split(struct buddy_zone *bzone, void *alloc, size_t cur_order,
static void *try_alloc(struct buddy_zone *bzone, size_t order)
{
void *ret = NULL;
spinlock_acquire(&bzone->lock);
spin_lock(&bzone->lock);
for (size_t i = order; i <= bzone->max_order; i++) {
if (bzone->order_freelists[i]) {
@ -126,7 +126,7 @@ static void *try_alloc(struct buddy_zone *bzone, size_t order)
if (ret)
bzone->space_free -= PAGE_SIZE << order;
spinlock_release(&bzone->lock);
spin_unlock(&bzone->lock);
return ret;
}
@ -276,9 +276,9 @@ void buddy_free(void *ptr)
struct buddy_zone *zone = buddy_zones + i;
vaddr_t p = (vaddr_t)ptr;
if (p >= zone->usable_base && p < zone->usable_base + zone->usable_len) {
spinlock_acquire(&zone->lock);
spin_lock(&zone->lock);
buddy_zone_free(zone, ptr);
spinlock_release(&zone->lock);
spin_unlock(&zone->lock);
}
}
}

View file

@ -2,7 +2,7 @@
#include <kernel/mem.h>
#include <kernel/buddy.h>
#include <kernel/slab.h>
#include <kernel/spinlock.h>
#include <kernel/lock.h>
#include <lib/string.h>
#include <lib/stdio.h>
@ -50,7 +50,7 @@ void *slab_alloc(mem_cache_t *cache)
return NULL;
void *ret = NULL;
spinlock_acquire(&cache->lock);
spin_lock(&cache->lock);
if (cache->num_free > 0) {
u64 *next = (u64 *)cache->next_free;
@ -67,7 +67,7 @@ void *slab_alloc(mem_cache_t *cache)
ret = slab_alloc(cache->next);
}
spinlock_release(&cache->lock);
spin_unlock(&cache->lock);
return ret;
}
@ -83,7 +83,7 @@ void slab_free(mem_cache_t *cache, void *ptr)
vaddr_t pcache = (vaddr_t)cache;
vaddr_t pcache_start = pcache + cache->offset;
spinlock_acquire(&cache->lock);
spin_lock(&cache->lock);
if (pptr >= pcache_start && pptr <= pcache_start + (cache->num_members - 1) * cache->item_size) {
if ((pptr - pcache_start) % cache->item_size) {
@ -103,7 +103,7 @@ void slab_free(mem_cache_t *cache, void *ptr)
}
}
spinlock_release(&cache->lock);
spin_unlock(&cache->lock);
}
void cleanup_kfree(void **ptr)