metalsvm/include/metalsvm/spinlock.h
2011-09-20 13:23:39 -07:00

233 lines
4.6 KiB
C

/*
* Copyright 2010-2011 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.
*/
/**
* @author Stefan Lankes
* @file include/metalsvm/spinlock.h
* @brief Spinlock functions
*/
#ifndef __SPINLOCK_H__
#define __SPINLOCK_H__
#include <metalsvm/stddef.h>
#include <metalsvm/spinlock_types.h>
#include <metalsvm/tasks_types.h>
#include <metalsvm/errno.h>
#include <asm/irqflags.h>
#include <asm/atomic.h>
#include <asm/processor.h>
#ifdef __cplusplus
extern "C" {
#endif
/** @brief Initialization of a spinlock
*
* Initialize each spinlock before use!
*
* @param s Pointer to the spinlock structure to initialize.
* @return
* - 0 on success
* - -EINVAL (-22) on failure
*/
inline static int spinlock_init(spinlock_t* s) {
if (BUILTIN_EXPECT(!s, 0))
return -EINVAL;
atomic_int32_set(&s->queue, 0);
atomic_int32_set(&s->dequeue, 1);
s->owner = MAX_TASKS;
s->counter = 0;
return 0;
}
/** @brief Destroy spinlock after use
* @return
* - 0 on success
* - -EINVAL (-22) on failure
*/
inline static int spinlock_destroy(spinlock_t* s) {
if (BUILTIN_EXPECT(!s, 0))
return -EINVAL;
s->owner = MAX_TASKS;
s->counter = 0;
return 0;
}
/** @brief Lock spinlock at entry of critical section
* @return
* - 0 on success
* - -EINVAL (-22) on failure
*/
inline static int spinlock_lock(spinlock_t* s) {
int32_t ticket;
task_t* curr_task;
if (BUILTIN_EXPECT(!s, 0))
return -EINVAL;
curr_task = per_core(current_task);
if (s->owner == curr_task->id) {
s->counter++;
return 0;
}
#if 1
ticket = atomic_int32_inc(&s->queue);
while(atomic_int32_read(&s->dequeue) != ticket) {
NOP1;
}
s->owner = curr_task->id;
s->counter = 1;
#else
while( atomic_int32_test_and_set(&s->dequeue,0) );
#endif
return 0;
}
/** @brief Unlock spinlock on exit of critical section
* @return
* - 0 on success
* - -EINVAL (-22) on failure
*/
inline static int spinlock_unlock(spinlock_t* s) {
if (BUILTIN_EXPECT(!s, 0))
return -EINVAL;
s->counter--;
if (!s->counter) {
s->owner = MAX_TASKS;
#if 1
atomic_int32_inc(&s->dequeue);
#else
atomic_int32_set(&s->dequeue,1);
#endif
}
return 0;
}
/** @brief Initialization of a irqsave spinlock
*
* Initialize each irqsave spinlock before use!
*
* @return
* - 0 on success
* - -EINVAL (-22) on failure
*/
inline static int spinlock_irqsave_init(spinlock_irqsave_t* s) {
if (BUILTIN_EXPECT(!s, 0))
return -EINVAL;
atomic_int32_set(&s->queue, 0);
atomic_int32_set(&s->dequeue, 1);
s->flags = 0;
s->coreid = (uint32_t)-1;
s->counter = 0;
return 0;
}
/** @brief Destroy irqsave spinlock after use
* @return
* - 0 on success
* - -EINVAL (-22) on failure
*/
inline static int spinlock_irqsave_destroy(spinlock_irqsave_t* s) {
if (BUILTIN_EXPECT(!s, 0))
return -EINVAL;
s->flags = 0;
s->coreid = (uint32_t)-1;
s->counter = 0;
return 0;
}
/** @brief Unlock an irqsave spinlock on exit of critical section
* @return
* - 0 on success
* - -EINVAL (-22) on failure
*/
inline static int spinlock_irqsave_lock(spinlock_irqsave_t* s) {
uint32_t flags;
int32_t ticket;
if (BUILTIN_EXPECT(!s, 0))
return -EINVAL;
flags = irq_nested_disable();
if (s->coreid == CORE_ID) {
s->counter++;
return 0;
}
#if 1
ticket = atomic_int32_inc(&s->queue);
while (atomic_int32_read(&s->dequeue) != ticket) {
NOP1;
}
#else
while( atomic_int32_test_and_set(&s->dequeue,0) );
#endif
s->coreid = CORE_ID;
s->flags = flags;
s->counter = 1;
return 0;
}
/** @brief Unlock irqsave spinlock on exit of critical section and re-enable interrupts
* @return
* - 0 on success
* - -EINVAL (-22) on failure
*/
inline static int spinlock_irqsave_unlock(spinlock_irqsave_t* s) {
uint32_t flags;
if (BUILTIN_EXPECT(!s, 0))
return -EINVAL;
s->counter--;
if (!s->counter) {
flags = s->flags;
s->coreid = (uint32_t) -1;
s->flags = 0;
#if 1
atomic_int32_inc(&s->dequeue);
#else
atomic_int32_set(&s->dequeue,1);
#endif
irq_nested_enable(flags);
}
return 0;
}
#ifdef __cplusplus
}
#endif
#endif