Skip to content
Snippets Groups Projects
Commit c8ff89a5 authored by Guy Zana's avatar Guy Zana
Browse files

Added rwlock stub, which is basically a mutex that can be taken recursively

parent 0264797f
No related branches found
No related tags found
No related merge requests found
#include <assert.h>
#include <stdint.h>
#include <osv/mutex.h>
#include <bsd/porting/netport.h>
#include <bsd/porting/rwlock.h>
void rw_init_flags(struct rwlock *rw, const char *name, int opts)
{
u_int flags = 0;
}
void rw_destroy(struct rwlock *rw)
{
}
void rw_sysinit(void *arg)
{
if (opts & RW_RECURSE)
flags |= LO_RECURSABLE;
mutex_init(&rw->_mutex);
rw->_rw_recurse = 0;
rw->_lo_flags = flags | LO_INITIALIZED;
}
void rw_sysinit_flags(void *arg)
void rw_destroy(struct rwlock *rw)
{
assert(rw->_rw_recurse == 0);
mutex_destroy(&rw->_mutex);
}
int rw_wowned(struct rwlock *rw)
{
return (0);
/* Is owned by current thread */
return (mutex_owned(&rw->_mutex));
}
void _rw_wlock(struct rwlock *rw, const char *file, int line)
{
_rw_rlock(rw, file, line);
}
int _rw_try_wlock(struct rwlock *rw, const char *file, int line)
{
return (0);
return(_rw_try_rlock(rw, file, line));
}
void _rw_wunlock(struct rwlock *rw, const char *file, int line)
{
_rw_runlock(rw, file, line);
}
void _rw_rlock(struct rwlock *rw, const char *file, int line)
{
if (rw_wowned(rw)) {
rw->_rw_recurse++;
} else {
mutex_lock(&rw->_mutex);
}
}
int _rw_try_rlock(struct rwlock *rw, const char *file, int line)
{
return (0);
}
void _rw_runlock(struct rwlock *rw, const char *file, int line)
{
if (rw_wowned(rw)) {
rw->_rw_recurse++;
return (1);
}
return (mutex_trylock(&rw->_mutex));
}
void _rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
{
}
void _rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
void _rw_runlock(struct rwlock *rw, const char *file, int line)
{
if (rw->_rw_recurse > 0) {
rw->_rw_recurse--;
}
if (rw->_rw_recurse == 0) {
mutex_unlock(&rw->_mutex);
}
}
int _rw_try_upgrade(struct rwlock *rw, const char *file, int line)
{
return (0);
/* The lock is already exclusive... */
return (1);
}
void _rw_downgrade(struct rwlock *rw, const char *file, int line)
{
/* No-op */
}
......@@ -35,8 +35,14 @@
#include <bsd/porting/netport.h>
#include <osv/mutex.h>
#define LO_INITIALIZED 0x00010000 /* Lock has been initialized. */
#define LO_RECURSABLE 0x00080000 /* Lock may recurse. */
struct rwlock {
mutex_t _osv_mtx;
mutex_t _mutex; /* Use a sleeping mutex */
u_int _rw_recurse; /* Counter for recursion support */
u_int _lo_flags; /* Allow threads to recursively acquire
exclusive locks for rw */
};
#define LOCK_FILE __FILE__
......@@ -51,8 +57,6 @@ struct rwlock {
#define rw_init(rw, name) rw_init_flags((rw), (name), 0)
void rw_init_flags(struct rwlock *rw, const char *name, int opts);
void rw_destroy(struct rwlock *rw);
void rw_sysinit(void *arg);
void rw_sysinit_flags(void *arg);
int rw_wowned(struct rwlock *rw);
void _rw_wlock(struct rwlock *rw, const char *file, int line);
int _rw_try_wlock(struct rwlock *rw, const char *file, int line);
......@@ -60,10 +64,6 @@ void _rw_wunlock(struct rwlock *rw, const char *file, int line);
void _rw_rlock(struct rwlock *rw, const char *file, int line);
int _rw_try_rlock(struct rwlock *rw, const char *file, int line);
void _rw_runlock(struct rwlock *rw, const char *file, int line);
void _rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file,
int line);
void _rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file,
int line);
int _rw_try_upgrade(struct rwlock *rw, const char *file, int line);
void _rw_downgrade(struct rwlock *rw, const char *file, int line);
......@@ -86,17 +86,7 @@ void _rw_downgrade(struct rwlock *rw, const char *file, int line);
rw_runlock(rw); \
} while (0)
struct rw_args {
struct rwlock *ra_rw;
const char *ra_desc;
};
struct rw_args_flags {
struct rwlock *ra_rw;
const char *ra_desc;
int ra_flags;
};
/* Disable static initialization */
#define RW_SYSINIT(name, rw, desc)
#define RW_SYSINIT_FLAGS(name, rw, desc, flags)
......
......@@ -60,6 +60,11 @@ extern "C" void mutex_unlock(mutex_t *mutex)
spin_unlock(&mutex->_wait_lock);
}
extern "C" bool mutex_owned(mutex_t* mutex)
{
return (mutex->_owner == sched::thread::current());
}
void mutex::lock()
{
mutex_lock(&_mutex);
......
......@@ -36,6 +36,8 @@ typedef struct cmutex mutex_t;
void mutex_lock(mutex_t* m);
bool mutex_trylock(mutex_t* m);
void mutex_unlock(mutex_t* m);
/* Is owned by current thread */
bool mutex_owned(mutex_t* m);
static __always_inline void mutex_init(mutex_t* m)
{
......
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