diff --git a/os/include/dde_kit/spin_lock.h b/os/include/dde_kit/spin_lock.h new file mode 100644 index 000000000..543f83a30 --- /dev/null +++ b/os/include/dde_kit/spin_lock.h @@ -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_ */ diff --git a/os/lib/mk/dde_kit.mk b/os/lib/mk/dde_kit.mk index f02fcdc14..7e47c8806 100644 --- a/os/lib/mk/dde_kit.mk +++ b/os/lib/mk/dde_kit.mk @@ -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 diff --git a/os/src/lib/dde_kit/spin_lock.cc b/os/src/lib/dde_kit/spin_lock.cc new file mode 100644 index 000000000..bbb052728 --- /dev/null +++ b/os/src/lib/dde_kit/spin_lock.cc @@ -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 +#include +#include + +extern "C" { +#include +} + + +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; +} +