Skip to content
Snippets Groups Projects
Commit 8fcad509 authored by Nadav Har'El's avatar Nadav Har'El Committed by Pekka Enberg
Browse files

Exile spinlock to a separate file


In very early OSv history, the spinlock was used in the mutex's
implementation so it made sense to put it in mutex.cc and mutex.h.

But now that the spinlock is all that's left in mutex.cc (the real mutex
is in lfmutex.cc), rename this file spinlock.cc. Also, move the spinlock
definitions from <osv/mutex.h> to a new <osv/spinlock.h>, so if someone
wants to make the grave mistake of using a spinlock - they will at least
need to explicitly include this header file.

Currently, the only remaining user of the spinlock is the console.
Using a spinlock (and not a mutex) in the console allows printing debug
messages while preemption is disabled. Arguably, this use-case is no
longer important (we have tracepoints), so in the future we can consider
dropping the spinlock completely.

Signed-off-by: default avatarNadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: default avatarPekka Enberg <penberg@cloudius-systems.com>
parent 86391f96
No related branches found
No related tags found
No related merge requests found
...@@ -5,12 +5,8 @@ ...@@ -5,12 +5,8 @@
* BSD license as described in the LICENSE file in the top-level directory. * BSD license as described in the LICENSE file in the top-level directory.
*/ */
#include <osv/mutex.h> #include <osv/spinlock.h>
#include <sched.hh> #include <sched.hh>
#include "arch.hh"
#include <pthread.h>
#include <cassert>
#include "osv/trace.hh"
void spin_lock(spinlock_t *sl) void spin_lock(spinlock_t *sl)
{ {
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "console.hh" #include "console.hh"
#include <osv/mutex.h> #include <osv/mutex.h>
#include <osv/spinlock.h>
// Wrap a Console with a spinlock, used for debugging // Wrap a Console with a spinlock, used for debugging
// (we can't use a mutex, since we might want to debug the scheduler) // (we can't use a mutex, since we might want to debug the scheduler)
......
...@@ -66,39 +66,7 @@ static inline void mutex_init(mutex_t* m) { memset(m, 0, sizeof(mutex_t)); } ...@@ -66,39 +66,7 @@ static inline void mutex_init(mutex_t* m) { memset(m, 0, sizeof(mutex_t)); }
static inline void mutex_destroy(mutex_t* m) { } static inline void mutex_destroy(mutex_t* m) { }
#define MUTEX_INITIALIZER {} #define MUTEX_INITIALIZER {}
__BEGIN_DECLS
// Spin lock. Use mutex instead, except where impossible:
typedef struct spinlock {
bool _lock;
#ifdef __cplusplus
// additional convenience methods for C++
inline constexpr spinlock() : _lock(false) { }
inline void lock();
inline void unlock();
#endif
} spinlock_t;
static inline void spinlock_init(spinlock_t *sl)
{
sl->_lock = false;
}
void spin_lock(spinlock_t *sl);
void spin_unlock(spinlock_t *sl);
__END_DECLS
#ifdef __cplusplus #ifdef __cplusplus
void spinlock::lock()
{
spin_lock(this);
}
void spinlock::unlock()
{
spin_unlock(this);
}
#include <mutex> #include <mutex>
#include <cstdlib> #include <cstdlib>
......
/*
* Copyright (C) 2013 Cloudius Systems, Ltd.
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
#ifndef OSV_SPINLOCK_H_
#define OSV_SPINLOCK_H_
#include <sys/cdefs.h>
__BEGIN_DECLS
// Spin lock. Use mutex instead, except where impossible:
typedef struct spinlock {
bool _lock;
#ifdef __cplusplus
// additional convenience methods for C++
inline constexpr spinlock() : _lock(false) { }
inline void lock();
inline void unlock();
#endif
} spinlock_t;
static inline void spinlock_init(spinlock_t *sl)
{
sl->_lock = false;
}
void spin_lock(spinlock_t *sl);
void spin_unlock(spinlock_t *sl);
__END_DECLS
#ifdef __cplusplus
void spinlock::lock()
{
spin_lock(this);
}
void spinlock::unlock()
{
spin_unlock(this);
}
#endif
#endif /* OSV_SPINLOCK_H_ */
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "debug.hh" #include "debug.hh"
#include "lockfree/mutex.hh" #include "lockfree/mutex.hh"
#include <osv/mutex.h> #include <osv/mutex.h>
#include <osv/spinlock.h>
#include "drivers/clock.hh" #include "drivers/clock.hh"
#include <string.h> #include <string.h>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment