shared: move ScopeExit from riscv64 arch kernel code
this file is c++11 only. Change-Id: Ibff7acec00337a9f56f9b8e29ea262c8d64c2446 Reviewed-on: https://review.haiku-os.org/c/haiku/+/5292 Tested-by: Commit checker robot <[email protected]> Reviewed-by: Fredrik Holmqvist <[email protected]> Reviewed-by: Jérôme Duval <[email protected]>
This commit is contained in:
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2022, Haiku, Inc. All Rights Reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
#ifndef _SCOPE_EXIT_H
|
||||||
|
#define _SCOPE_EXIT_H
|
||||||
|
|
||||||
|
|
||||||
|
#if __cplusplus < 201103L
|
||||||
|
#error This file requires compiler support for the C++11 standard.
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
|
||||||
|
template<typename F>
|
||||||
|
class ScopeExit
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit ScopeExit(F&& fn) : fFn(fn)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~ScopeExit()
|
||||||
|
{
|
||||||
|
fFn();
|
||||||
|
}
|
||||||
|
|
||||||
|
ScopeExit(ScopeExit&& other) : fFn(std::move(other.fFn))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
ScopeExit(const ScopeExit&);
|
||||||
|
ScopeExit& operator=(const ScopeExit&);
|
||||||
|
|
||||||
|
private:
|
||||||
|
F fFn;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _SCOPE_EXIT_H
|
||||||
@@ -21,6 +21,7 @@
|
|||||||
#include <Plic.h>
|
#include <Plic.h>
|
||||||
#include <Clint.h>
|
#include <Clint.h>
|
||||||
#include <AutoDeleterDrivers.h>
|
#include <AutoDeleterDrivers.h>
|
||||||
|
#include <ScopeExit.h>
|
||||||
#include "RISCV64VMTranslationMap.h"
|
#include "RISCV64VMTranslationMap.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@@ -354,40 +355,6 @@ SetAccessedFlags(addr_t addr, bool isWrite)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO: needs moved into an arch-agnostic location?
|
|
||||||
|
|
||||||
template<typename F>
|
|
||||||
class ScopeExit
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
explicit ScopeExit(F&& fn) : fFn(fn)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
~ScopeExit()
|
|
||||||
{
|
|
||||||
fFn();
|
|
||||||
}
|
|
||||||
|
|
||||||
ScopeExit(ScopeExit&& other) : fFn(std::move(other.fFn))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
ScopeExit(const ScopeExit&);
|
|
||||||
ScopeExit& operator=(const ScopeExit&);
|
|
||||||
|
|
||||||
private:
|
|
||||||
F fFn;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename F>
|
|
||||||
ScopeExit<F> MakeScopeExit(F&& fn)
|
|
||||||
{
|
|
||||||
return ScopeExit<F>(std::move(fn));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
extern "C" void
|
extern "C" void
|
||||||
STrap(iframe* frame)
|
STrap(iframe* frame)
|
||||||
{
|
{
|
||||||
@@ -428,7 +395,7 @@ STrap(iframe* frame)
|
|||||||
thread_get_current_thread()->arch_info.oldA0 = frame->a0;
|
thread_get_current_thread()->arch_info.oldA0 = frame->a0;
|
||||||
thread_at_kernel_entry(system_time());
|
thread_at_kernel_entry(system_time());
|
||||||
}
|
}
|
||||||
const auto& kernelExit = MakeScopeExit([&]() {
|
const auto& kernelExit = ScopeExit([&]() {
|
||||||
if (SstatusReg(frame->status).spp == modeU) {
|
if (SstatusReg(frame->status).spp == modeU) {
|
||||||
disable_interrupts();
|
disable_interrupts();
|
||||||
atomic_and(&thread_get_current_thread()->flags, ~THREAD_FLAGS_SYSCALL_RESTARTED);
|
atomic_and(&thread_get_current_thread()->flags, ~THREAD_FLAGS_SYSCALL_RESTARTED);
|
||||||
|
|||||||
Reference in New Issue
Block a user