universal7885: parts-aidl: Refactor swap impl

This commit is contained in:
roynatech2544 2022-10-10 22:16:39 +09:00
commit 50f3bfa31a
9 changed files with 48 additions and 24 deletions

View file

@ -1 +1 @@
330aae72d01c9055be476b739e8c01df433f0ea2
d1cebfb943ba21a23e9c502b43d6c2766f6e608d

View file

@ -26,5 +26,5 @@ interface ISwapOnData {
oneway void setSwapOn();
oneway void setSwapSize(in int size);
oneway void makeSwapFile(in int size);
}

View file

@ -37,5 +37,5 @@ interface ISwapOnData {
oneway void removeSwapFile();
oneway void setSwapOff();
oneway void setSwapOn();
oneway void setSwapSize(in int size);
oneway void makeSwapFile(in int size);
}

View file

@ -20,7 +20,6 @@
#include <thread>
#include <unistd.h>
static int mSwapSize = 100;
extern int mkswap(const char *filename);
extern void mkfile(int filesize, const char *name);
@ -32,41 +31,58 @@ static std::mutex thread_lock;
static bool swapOnRes = false;
::ndk::ScopedAStatus SwapOnData::setSwapSize(int32_t size) {
mSwapSize = size;
static inline bool swapfile_exist(void) {
return access(SWAP_PATH, F_OK) == 0;
}
static void makeFile(int32_t mSwapSize) {
const std::lock_guard<std::mutex> lock(thread_lock);
mkfile(mSwapSize * 10, SWAP_PATH);
mkswap(SWAP_PATH);
}
::ndk::ScopedAStatus SwapOnData::makeSwapFile(int32_t size) {
if (swapfile_exist()) return ::ndk::ScopedAStatus::ok();
std::thread makefile_thread(makeFile, size);
makefile_thread.detach();
return ::ndk::ScopedAStatus::ok();
}
static void rmswap(void) {
const std::lock_guard<std::mutex> lock(thread_lock);
std::remove(SWAP_PATH);
}
::ndk::ScopedAStatus SwapOnData::removeSwapFile(void) {
const std::lock_guard<std::mutex> lock(thread_lock);
std::remove(SWAP_PATH);
if (!swapfile_exist()) return ::ndk::ScopedAStatus::ok();
std::thread rmswap_thread(rmswap);
rmswap_thread.detach();
return ::ndk::ScopedAStatus::ok();
}
static void mkfile_swapon_thread(void) {
static void swapon_func(void) {
const std::lock_guard<std::mutex> lock(thread_lock);
if (access(SWAP_PATH, F_OK) != 0) {
mkfile(mSwapSize * 10, SWAP_PATH);
mkswap(SWAP_PATH);
}
int res = swapon(SWAP_PATH, (10 << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK);
swapOnRes = res == 0;
}
::ndk::ScopedAStatus SwapOnData::setSwapOn() {
const std::lock_guard<std::mutex> lock(thread_lock);
std::thread mkswapfile(mkfile_swapon_thread);
if (!swapfile_exist()) return ::ndk::ScopedAStatus::ok();
std::thread swapon_thread(swapon_func);
swapon_thread.detach();
return ::ndk::ScopedAStatus::ok();
}
static void swapoff_thread(void) {
static void swapoff_func(void) {
if (!swapfile_exist()) return;
const std::lock_guard<std::mutex> lock(thread_lock);
swapoff(SWAP_PATH);
}
::ndk::ScopedAStatus SwapOnData::setSwapOff() {
const std::lock_guard<std::mutex> lock(thread_lock);
std::thread swapoff(swapoff_thread);
std::thread swapoff_thread(swapoff_func);
swapoff_thread.detach();
return ::ndk::ScopedAStatus::ok();
}

View file

@ -21,7 +21,7 @@ namespace aidl::vendor::eureka::hardware::parts {
struct SwapOnData : public BnSwapOnData {
// Methods from ::aidl::vendor::eureka::hardware::parts::ISwapOnData
// follow.
::ndk::ScopedAStatus setSwapSize(int32_t size);
::ndk::ScopedAStatus makeSwapFile(int32_t size);
::ndk::ScopedAStatus setSwapOn(void);
::ndk::ScopedAStatus removeSwapFile(void);
::ndk::ScopedAStatus setSwapOff(void);

View file

@ -26,5 +26,5 @@ interface ISwapOnData {
oneway void setSwapOn();
oneway void setSwapSize(in int size);
oneway void makeSwapFile(in int size);
}