其他分享
首页 > 其他分享> > muduo 库解析之六:Mutex

muduo 库解析之六:Mutex

作者:互联网

源码

#pragma once
#include <pthread.h>

#include "NonCopyable.h"
#include "CurrentThread.h"

namespace muduo
{

//@ 检查返回值的宏
#ifdef CHECK_PTHREAD_RETURN_VALUE

#ifdef NDEBUG
    __BEGIN_DECLS
    extern void __assert_perror_fail(int errno, const char *file, unsigned int line, const char *function) noexcept __attribute__((__noreturn__));
    __END_DECLS

#define MCHECK(ret)                                                    \
    ({                                                                 \
        __typeof__(ret) errno = (ret);                                 \
        if (__builtin_expect(errno != 0, 0))                           \
            __assert_perror_fail(errno, __FILE__, __LINE__, __func__); \
    })
#endif

#else //@ CHECK_PTHREAD_RETURN_VALUE
#define MCHECK(ret) ({ __typeof__(ret) errno = (ret); assert(errno == 0); void(errno); })

#endif //@ CHECK_PTHREAD_RETURN_VALUE

    class MutexLock : public NonCopyable
    {
    public:
        MutexLock() : holder_(0)
        {
            MCHECK(pthread_mutex_init(&mutex_, nullptr));
        }

        ~MutexLock()
        {
            assert(holder_ == 0); //@ 保证锁不被持有
            MCHECK(pthread_mutex_destroy(&mutex_));
        }

        bool is_locked_by_this_thread() const
        {
            return holder_ == CurrentThread::tid();
        }

        void assert_locked() const
        {
            assert(is_locked_by_this_thread());
        }

        void lock()
        {
            MCHECK(pthread_mutex_lock(&mutex_));
            assign_holder();
        }

        void unlock()
        {
            unassign_holder();
            MCHECK(pthread_mutex_unlock(&mutex_));
        }

        pthread_mutex *get_mutex() const
        {
            return &mutex_;
        }

    private:
        void assign_holder()
        {
            holder_ = CurrentThread::tid();
        }

        void unassign_holder()
        {
            holder_ = 0;
        }

    private:
        class UnassignGuard : NonCopyable
        {
        public:
            explicit UnassignGuard(MutexLock &owner) : owner_(owner)
            {
                owner_.unassign_holder();
            }

            ~UnassignGuard()
            {
                owner_.assign_holder();
            }

        private:
            MutexLock &owner_;  //@ 存放引用
        };

    private:
        pthread_mutex mutex_;
        pid_t holder_;
    };

    //@ RAII 格式 MutexLock 使用
    class MutexLockGuard : public NonCopyable
    {
    public:
        explicit MutexLockGuard(MutexLock &mutex) : mutex_(mutex)
        {
            mutex_.lock();
        }

        ~MutexLockGuard()
        {
            mutex_.unlock();
        }

    private:
        MutexLock &mutex_;
    };

//@ 避免构造一个临时的对象,例如 MutexLockGuard(mutex),临时对象不能一直持有锁
#define MutexLockGuard(x) error "Missing guard object name"
}

标签:__,muduo,errno,void,之六,mutex,Mutex,holder,MutexLock
来源: https://www.cnblogs.com/xiaojianliu/p/14696717.html