1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
class Mutex { public: typedef bthread_mutex_t* native_handler_type; 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)); } native_handler_type native_handler() { return &_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); } bool try_lock() { return !bthread_mutex_trylock(&_mutex); } private: DISALLOW_COPY_AND_ASSIGN(Mutex); bthread_mutex_t _mutex; };
typedef struct { unsigned* butex; bthread_contention_site_t csite; } bthread_mutex_t;
|