Add spin lock to DDE Kit

Linux DDE used to implement Linux spin locks based on 'dde_kit_lock'.
This works fine if a spin lock is initialized only once and used
infinitely. But if spin locks are initialized on-the-fly at a high rate,
each initialization causes the allocation of a new 'dde_kit_lock'.
Because in contrast to normal locks, spinlocks cannot be explicitly
destroyed, the spin-lock emulating locks are never freed. To solve the
leakage of locks, there seems to be no other way than to support the
semantics as expected by the Linux drivers. Hence, this patch introduces
a DDE Kit API for spin locks.
This commit is contained in:
Norman Feske 2012-01-27 02:01:07 +01:00
parent a107c89a8e
commit 48ac5143a2
3 changed files with 108 additions and 1 deletions

View File

@ -0,0 +1,50 @@
/*
* \brief Spin lock
* \author Norman Feske
* \date 2012-01-27
*/
/*
* Copyright (C) 2012 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
#ifndef _INCLUDE__DDE_KIT__SPIN_LOCK_H_
#define _INCLUDE__DDE_KIT__SPIN_LOCK_H_
/**
* Private spin lock type
*/
typedef volatile int dde_kit_spin_lock;
enum { DDE_KIT_SPIN_LOCK_LOCKED, DDE_KIT_SPIN_LOCK_UNLOCKED };
/**
* Initialize spin lock
*
* \param out_lock lock handle (output parameter)
*
* The created lock is free (not acquired) after initialization.
*/
void dde_kit_spin_lock_init(dde_kit_spin_lock *spin_lock);
/**
* Acquire spin lock
*/
void dde_kit_spin_lock_lock(dde_kit_spin_lock *spin_lock);
/**
* Try to acquire a lock (non-blocking)
*/
int dde_kit_spin_lock_try_lock(dde_kit_spin_lock *spin_lock);
/**
* Release spin lock
*/
void dde_kit_spin_lock_unlock(dde_kit_spin_lock *spin_lock);
#endif /* _INCLUDE__DDE_KIT__SPIN_LOCK_H_ */

View File

@ -1,6 +1,6 @@
SRC_C = lock.cc semaphore.cc panic.cc printf.cc interrupt.cc pgtab.cc \
memory.cc thread.cc pci_tree.cc pci.cc resources.cc timer.cc \
dde_kit.cc
dde_kit.cc spin_lock.cc
LIBS = thread alarm
vpath % $(REP_DIR)/src/lib/dde_kit

View File

@ -0,0 +1,57 @@
/*
* \brief Spin lock
* \author Norman Feske
* \date 2012-01-27
*/
/*
* Copyright (C) 2012 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
#include <base/env.h>
#include <cpu/atomic.h>
#include <base/printf.h>
extern "C" {
#include <dde_kit/spin_lock.h>
}
static inline void spinlock_lock(volatile int *lock_variable)
{
while (!Genode::cmpxchg(lock_variable, DDE_KIT_SPIN_LOCK_UNLOCKED,
DDE_KIT_SPIN_LOCK_LOCKED));
}
extern "C" void dde_kit_spin_lock_init(dde_kit_spin_lock *spin_lock)
{
*spin_lock = DDE_KIT_SPIN_LOCK_UNLOCKED;
}
extern "C" void dde_kit_spin_lock_lock(dde_kit_spin_lock *spin_lock)
{
spinlock_lock(spin_lock);
}
extern "C" int dde_kit_spin_lock_try_lock(dde_kit_spin_lock *spin_lock)
{
PERR("not implemented - will potentially block");
spinlock_lock(spin_lock);
/* success */
return 0;
}
extern "C" void dde_kit_spin_lock_unlock(dde_kit_spin_lock *spin_lock)
{
*spin_lock = DDE_KIT_SPIN_LOCK_UNLOCKED;
}