2024-06-29 02:07:35 +00:00
|
|
|
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
|
|
|
|
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
|
|
|
|
#ifndef CTL_MUTEX_H_
|
|
|
|
#define CTL_MUTEX_H_
|
|
|
|
#include "libc/thread/thread.h"
|
|
|
|
|
|
|
|
namespace ctl {
|
|
|
|
|
|
|
|
class mutex
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
mutex()
|
|
|
|
{
|
2024-07-01 10:48:28 +00:00
|
|
|
if (pthread_mutex_init(&m_, nullptr))
|
|
|
|
__builtin_trap();
|
2024-06-29 02:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~mutex()
|
|
|
|
{
|
2024-07-01 10:48:28 +00:00
|
|
|
if (pthread_mutex_destroy(&m_))
|
|
|
|
__builtin_trap();
|
2024-06-29 02:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lock()
|
|
|
|
{
|
2024-07-01 10:48:28 +00:00
|
|
|
if (pthread_mutex_lock(&m_))
|
|
|
|
__builtin_trap();
|
2024-06-29 02:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool try_lock()
|
|
|
|
{
|
|
|
|
return pthread_mutex_trylock(&m_) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock()
|
|
|
|
{
|
2024-07-01 10:48:28 +00:00
|
|
|
if (pthread_mutex_unlock(&m_))
|
|
|
|
__builtin_trap();
|
2024-06-29 02:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete copy constructor and assignment operator
|
|
|
|
mutex(const mutex&) = delete;
|
|
|
|
mutex& operator=(const mutex&) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
pthread_mutex_t m_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ctl
|
|
|
|
|
|
|
|
#endif // CTL_MUTEX_H_
|