Skip to content
This repository has been archived by the owner on May 31, 2021. It is now read-only.

Commit

Permalink
Improve spin locks with owner data
Browse files Browse the repository at this point in the history
  • Loading branch information
klange committed May 27, 2021
1 parent 2f72883 commit 9a44cd2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion base/usr/include/kernel/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ typedef struct image {
uintptr_t heap;
uintptr_t stack;
uintptr_t shm_heap;
volatile int lock[2];
spin_lock_t lock;
} image_t;

typedef struct file_descriptors {
Expand Down
3 changes: 2 additions & 1 deletion base/usr/include/kernel/ringbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
#include <stddef.h>
#include <kernel/list.h>
#include <kernel/vfs.h>
#include <kernel/spinlock.h>

typedef struct {
unsigned char * buffer;
size_t write_ptr;
size_t read_ptr;
size_t size;
volatile int lock[2];
spin_lock_t lock;
list_t * wait_queue_readers;
list_t * wait_queue_writers;
int internal_stop;
Expand Down
20 changes: 16 additions & 4 deletions base/usr/include/kernel/spinlock.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#pragma once

typedef volatile int spin_lock_t[2];
extern void spin_init(spin_lock_t lock);
typedef volatile struct {
volatile int latch[1];
int owner;
const char * func;
} spin_lock_t;
#define spin_init(lock) do { (lock).owner = 0; (lock).latch[0] = 0; (lock).func = NULL; } while (0)

#define spin_lock(lock) while (__sync_lock_test_and_set(lock, 0x01))
#define spin_unlock(lock) __sync_lock_release(lock)
#define DEBUG_LOCKS
#ifdef DEBUG_LOCKS
#define spin_lock(lock) do { while (__sync_lock_test_and_set((lock).latch, 0x01)); (lock).owner = this_core->cpu_id+1; (lock).func = __func__; } while (0)
#define spin_unlock(lock) do { (lock).func = NULL; (lock).owner = -1; __sync_lock_release((lock).latch); } while (0)
#else
#define spin_lock(lock) do { while (__sync_lock_test_and_set((lock).latch, 0x01)); } while(0)
#define spin_unlock(lock) __sync_lock_release((lock).latch);
#endif

#include <kernel/process.h>
1 change: 0 additions & 1 deletion kernel/sys/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ static spin_lock_t tree_lock = { 0 };
static spin_lock_t process_queue_lock = { 0 };
static spin_lock_t wait_lock_tmp = { 0 };
static spin_lock_t sleep_lock = { 0 };
static spin_lock_t talking = { 0 };

/**
* @brief Restore the context of the next available process's kernel thread.
Expand Down
4 changes: 0 additions & 4 deletions kernel/sys/spin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@
#include <kernel/spinlock.h>
#include <kernel/process.h>

void spin_init(spin_lock_t lock) {
lock[0] = 0;
lock[1] = 0;
}

0 comments on commit 9a44cd2

Please sign in to comment.