Commit 43b1af72 authored by Cholerae Hu's avatar Cholerae Hu

mutex: throw system_error when constuctor failed and lock failed

Signed-off-by: 's avatarCholerae Hu <choleraehyq@gmail.com>
parent f7aab8c9
...@@ -42,10 +42,20 @@ namespace bthread { ...@@ -42,10 +42,20 @@ namespace bthread {
class Mutex { class Mutex {
public: public:
typedef bthread_mutex_t* native_handler_type; typedef bthread_mutex_t* native_handler_type;
Mutex() { CHECK_EQ(0, bthread_mutex_init(&_mutex, NULL)); } Mutex() {
int ec = bthread_mutex_init(&_mutex, NULL);
if (ec != 0) {
throw std::system_error(std::error_code(ec, std::system_category()), "Mutex constructor failed");
}
}
~Mutex() { CHECK_EQ(0, bthread_mutex_destroy(&_mutex)); } ~Mutex() { CHECK_EQ(0, bthread_mutex_destroy(&_mutex)); }
native_handler_type native_handler() { return &_mutex; } native_handler_type native_handler() { return &_mutex; }
void lock() { bthread_mutex_lock(&_mutex); } void lock() {
int ec = bthread_mutex_lock(&_mutex);
if (ec != 0) {
throw std::system_error(std::error_code(ec, std::system_category()), "Mutex lock failed");
}
}
void unlock() { bthread_mutex_unlock(&_mutex); } void unlock() { bthread_mutex_unlock(&_mutex); }
bool try_lock() { return !bthread_mutex_trylock(&_mutex); } bool try_lock() { return !bthread_mutex_trylock(&_mutex); }
// TODO(chenzhangyi01): Complement interfaces for C++11 // TODO(chenzhangyi01): Complement interfaces for C++11
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment