metalsvm/include/metalsvm/spinlocks.h
2010-08-04 17:21:25 +00:00

108 lines
2 KiB
C

/*
* Copyright 2010 Stefan Lankes, Chair for Operating Systems,
* RWTH Aachen University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of MetalSVM.
*/
#ifndef __SPINLOCKS_H__
#define __SPINLOCKS_H__
#include <metalsvm/config.h>
#include <metalsvm/tasks.h>
#include <asm/irqflags.h>
#include <asm/atomic.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
atomic_uint32_t lock;
} spinlock_t;
#define SPINLOCK_INIT { 0 }
inline static int spinlock_init(spinlock_t* s) {
if (BUILTIN_EXPECT(!s, 0))
return -1;
s->lock = 0;
return 0;
}
inline static int spinlock_destroy(spinlock_t* s) {
if (BUILTIN_EXPECT(!s, 0))
return -1;
s->lock = 0;
return 0;
}
inline static int spinlock_lock(spinlock_t* s) {
if (BUILTIN_EXPECT(!s, 0))
return -1;
while (atomic_uint32_test_and_set(&(s->lock)))
schedule();
return 0;
}
inline static int spinlock_trylock(spinlock_t* s) {
if (BUILTIN_EXPECT(!s, 0))
return -1;
return !atomic_uint32_test_and_set(&(s->lock));
}
inline static int spinlock_unlock(spinlock_t* s) {
if (BUILTIN_EXPECT(!s, 0))
return -1;
if (BUILTIN_EXPECT(!(s->lock), 0))
return -1;
s->lock = 0;
return 0;
}
inline static int spinlock_lock_irqsave(spinlock_t* s) {
if (BUILTIN_EXPECT(!s, 0))
return -1;
irq_disable();
return spinlock_lock(s);
}
inline static int spinlock_unlock_irqsave(spinlock_t* s) {
int ret;
if (BUILTIN_EXPECT(!s, 0))
return -1;
ret = spinlock_unlock(s);
irq_enable();
return ret;
}
#ifdef __cplusplus
}
#endif
#endif