Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mutex function #14

Open
wants to merge 2 commits into
base: develop_v0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ target_compile_options(system PUBLIC -fcoroutines -Wall -O2)
target_compile_features(system PUBLIC cxx_std_20)
target_include_directories(system PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc)

add_library(mutex src/mutex.cpp)
target_compile_options(mutex PUBLIC -Wall -O2)
target_compile_features(mutex PUBLIC cxx_std_20)
target_include_directories(mutex PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc)

if(${PROJECT_NAME}-SYSTEM-TEST)
add_executable(${PROJECT_NAME}-system-googletest test/system-gtest.cpp src/generator.cpp src/task.cpp)
target_compile_features(${PROJECT_NAME}-system-googletest PUBLIC cxx_std_20)
Expand Down
67 changes: 67 additions & 0 deletions inc/mutex.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @file mutex.hpp
* @author Nishinaga
* @brief
* @version 0.1
* @date 2024-04-24
*
* @copyright Copyright (c) 2024
* @todo ban interrupts
*
*/

#pragma once

#include <type_traits>
/**
* @brief mutex class for single thread
*
*/
class Mutex
{
bool locked;
public:
Mutex();

bool& ImplLock() noexcept;
};

#define MUTEX_LOCK(mutex) \
do\
{\
static_assert(std::is_same<Mutex, \
std::remove_reference<std::remove_cv<decltype(mutex)>::type>::type \
>::value, \
"the mutex type is not Mutex"); \
mutex.ImplLock() = true; \
} while(0)

#define MUTEX_UNLOCK(mutex) \
do\
{\
static_assert(std::is_same<Mutex, \
std::remove_reference<std::remove_cv<decltype(mutex)>::type>::type \
>::value, \
"the mutex type is not Mutex"); \
mutex.ImplLock() = false; \
} while(0)

#define MUTEX_TRY_LOCK(mutex) \
true; \
do\
{\
static_assert(std::is_same<Mutex, \
std::remove_reference<std::remove_cv<decltype(mutex)>::type>::type \
>::value, \
"the mutex type is not Mutex"); \
mutex.ImplLock() = true; \
} while(0)

class LockGuard
{
Mutex& mutex;

public:
LockGuard(Mutex& mutex);
~LockGuard();
};
32 changes: 32 additions & 0 deletions src/mutex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @file mutex.cpp
* @author Nishinaga
* @brief implementation of mutex
* @version 0.1
* @date 2024-04-25
*
* @copyright Copyright (c) 2024
*
*/

#include "mutex.hpp"

Mutex::Mutex()
{

}

bool& Mutex::ImplLock() noexcept
{
return this->locked;
}

LockGuard::LockGuard(Mutex& mutex_) : mutex(mutex_)
{
MUTEX_LOCK(mutex);
}

LockGuard::~LockGuard()
{
MUTEX_UNLOCK(this->mutex);
}