android bionic mutex分析

1. 先从mutex的构造开始

/*
 * Simple mutex class.  The implementation is system-dependent.
 *
 * The mutex must be unlocked by the thread that locked it.  They are not
 * recursive, i.e. the same thread can't lock it multiple times.
 */
class Mutex {
public:
    enum {
        PRIVATE = 0,
        SHARED = 1
    };
    
                Mutex();
                Mutex(const char* name);
                Mutex(int type, const char* name = NULL);
#if defined(HAVE_PTHREADS)
                virtual
#endif
                ~Mutex();

    // lock or unlock the mutex
    status_t    lock();
    // gaia add +
    status_t    lockTimeout(unsigned msecs);
    // gaia add -
    void        unlock();

    // lock if possible; returns 0 on success, error otherwise
    status_t    tryLock();

    // Manages the mutex automatically. It'll be locked when Autolock is
    // constructed and released when Autolock goes out of scope.
    class Autolock {
    public:
    	// gaia add +
#if 1
        inline Autolock(Mutex& mutex) : mLock(mutex)  { mStatus = mLock.lock(); }
        inline Autolock(Mutex* mutex) : mLock(*mutex) { mStatus = mLock.lock(); }
        inline Autolock(Mutex& mutex, unsigned msec) : mLock(mutex), mMsec(msec) {
            msec ? (mStatus = mLock.lockTimeout(msec)) : (mStatus = mLock.lock()); }
        inline ~Autolock() { mLock.unlock(); }
        status_t getStatus() { return mStatus; }
#else
        inline Autolock(Mutex& mutex) : mLock(mutex)  { mLock.lock(); }
        inline Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); }
        inline ~Autolock() { mLock.unlock(); }
#endif
   		// gaia add -
    private:
        Mutex& mLock;
	    // gaia add +
        unsigned mMsec;
        status_t mStatus;
	    // gaia add -
    };

private:
    friend class Condition;
    // GAIA ADD for RecursiveMutex
   friend class RecursiveMutex;
    
    // A mutex cannot be copied
                Mutex(const Mutex&);
    Mutex&      operator = (const Mutex&);
    
#if defined(HAVE_PTHREADS)
    pthread_mutex_t mMutex;
    //GAIA ADD
//protected:
    Mutex(bool recursive, int type = 0, const char* name = NULL);
#else
    void    _init();
    void*   mState;
#endif
};

这是Mutex的头文件定义, 根据系统不同而不同。

#if defined(HAVE_PTHREADS)

inline Mutex::Mutex() {
    pthread_mutex_init(&mMutex, NULL);
}
inline Mutex::Mutex(const char* name) {
    pthread_mutex_init(&mMutex, NULL);
}
inline Mutex::Mutex(int type, const char* name) {
    if (type == SHARED) {
        pthread_mutexattr_t attr;
        pthread_mutexattr_init(&attr);
        pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
        pthread_mutex_init(&mMutex, &attr);
        pthread_mutexattr_destroy(&attr);
    } else {
        pthread_mutex_init(&mMutex, NULL);
    }
}
//GAIA ADD
#if defined(HAVE_PTHREADS)
inline Mutex::Mutex(bool recursive, int type, const char* name) {
   (void)name;
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    if (type == SHARED) {
        pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
    }
    if (recursive) {
       pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    }
    pthread_mutex_init(&mMutex, &attr);
    pthread_mutexattr_destroy(&attr);}
#endif
inline Mutex::~Mutex() {
    pthread_mutex_destroy(&mMutex);
}
inline status_t Mutex::lock() {
    return -pthread_mutex_lock(&mMutex);
}
// gaia add +
inline status_t Mutex::lockTimeout(unsigned msec) {
#if HOST_BUILD
    (void)msec;
    return -pthread_mutex_lock(&mMutex);
#else
    return -pthread_mutex_lock_timeout_np(&mMutex, msec);
#endif
}
// gaia add -
inline void Mutex::unlock() {
    pthread_mutex_unlock(&mMutex);
}
inline status_t Mutex::tryLock() {
    return -pthread_mutex_trylock(&mMutex);
}

#endif // HAVE_PTHREADS

这段代码不复杂,粗看基本能看懂。

那么接下来详细介绍。

从构造函数入手:

                Mutex();
                Mutex(const char* name);
                Mutex(int type, const char* name = NULL);
注意第三个构造函数:
inline Mutex::Mutex(int type, const char* name) {
    if (type == SHARED) {
        pthread_mutexattr_t attr;
        pthread_mutexattr_init(&attr);
        pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
        pthread_mutex_init(&mMutex, &attr);
        pthread_mutexattr_destroy(&attr);
    } else {
        pthread_mutex_init(&mMutex, NULL);
    }
}
这里可以看到Mutex分为两中类型:

    enum {
        PRIVATE = 0,
        SHARED = 1
    };

Mutex调用的是Bionic中的pthread:bionic/libc/bionic/pthread.c.

那么接下来深入到pthread中。


pthread针对不同Mutex设置了不同的type

/* Mutex types.  */
enum
{
  PTHREAD_MUTEX_TIMED_NP,
  PTHREAD_MUTEX_RECURSIVE_NP,
  PTHREAD_MUTEX_ERRORCHECK_NP,
  PTHREAD_MUTEX_ADAPTIVE_NP
#if defined __USE_UNIX98 || defined __USE_XOPEN2K8
  ,
  PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_TIMED_NP,
  PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
  PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
  PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
#endif
#ifdef __USE_GNU
  /* For compatibility.  */
  , PTHREAD_MUTEX_FAST_NP = PTHREAD_MUTEX_TIMED_NP
#endif
};
mutex的type分为3类: Normal, Recursive, Errorcheck,Recursive就是递归锁;

相关的API罗列如下:

    //pthread mutexattr 操作  
    int pthread_mutexattr_init(pthread_mutexattr_t *attr);  
    int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);  
      
    int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);  
    int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);  
      
    int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int  pshared);  
    int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared);  
      
      
      
      
    //pthread mutex 操作  
    int pthread_mutex_init(pthread_mutex_t *mutex,  
                           const pthread_mutexattr_t *attr);  
    int pthread_mutex_destroy(pthread_mutex_t *mutex);  
      
    int pthread_mutex_lock(pthread_mutex_t *mutex);  
    int pthread_mutex_unlock(pthread_mutex_t *mutex);  
    int pthread_mutex_trylock(pthread_mutex_t *mutex);  
    int pthread_mutex_timedlock(pthread_mutex_t *mutex, struct timespec*  ts);  



基础结构

pthread_mutexattr_t,

pthread_mutex_t

typedef long pthread_mutexattr_t;
 a mutex attribute holds the following fields
  bits:     name       description
  0-3       type       type of mutex
  4         shared     process-shared flag
 

typedef struct
{
    int volatile value;
} pthread_mutex_t;

 a mutex is implemented as a 32-bit integer holding the following fields
 
  bits:     name     description
  31-16     tid      owner thread's kernel id (recursive and errorcheck only)
  15-14     type     mutex type
  13        shared   process-shared flag
  12-2      counter  counter of recursive mutexes
  1-0       state    lock state (0, 1 or 2)



先把这个解释下,在后面会用到的:/

//tid,  表示拥有这个mutex的线程kernel id

//type 表示mutex的类型 /normal : recursive: error

//process-shared flag, 是否共享锁,/private : shared

//counter主要用于递归mutex;

//state 表示锁的状态;

先解析如下这段代码:
        pthread_mutexattr_t attr;
        pthread_mutexattr_init(&attr);
        pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
        pthread_mutex_init(&mMutex, &attr);
        pthread_mutexattr_destroy(&attr);


锁的初始化分两步:

a. 初始化mutexattr;

pthread_mutexattr_t用来描述mutex的属性:

a mutex attribute holds the following fields
 *
 * bits:     name       description
 * 0-3       type       type of mutex
 * 4         shared     process-shared flag
 

对应的掩码:

#define  MUTEXATTR_TYPE_MASK   0x000f
#define  MUTEXATTR_SHARED_MASK 0x0010

shared: Flage为

#define PTHREAD_PROCESS_PRIVATE  0
#define PTHREAD_PROCESS_SHARED   1

pthread_mutexattr_t对应于pthread_mutex_t

 * 15-14     type     mutex type

 * 13           shared   process-shared flag

a. 初始化attr源码

int pthread_mutexattr_init(pthread_mutexattr_t *attr)
{
    if (attr) {
        *attr = PTHREAD_MUTEX_DEFAULT;
        return 0;
    } else {
        return EINVAL;
    }
}

PTHREAD_MUTEX_DEFAULT其实就是前面的

PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL

接下来设置flag:

int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int  pshared)
{
    if (!attr)
        return EINVAL;

    switch (pshared) {
    case PTHREAD_PROCESS_PRIVATE:
        *attr &= ~MUTEXATTR_SHARED_MASK;
        return 0;

    case PTHREAD_PROCESS_SHARED:
        /* our current implementation of pthread actually supports shared
         * mutexes but won't cleanup if a process dies with the mutex held.
         * Nevertheless, it's better than nothing. Shared mutexes are used
         * by surfaceflinger and audioflinger.
         */
        *attr |= MUTEXATTR_SHARED_MASK;
        return 0;
    }
    return EINVAL;
}

b. 初始化mutex;


#define  MUTEX_TYPE_SHIFT      14
#define  MUTEX_TYPE_LEN        2
#define  MUTEX_TYPE_MASK       FIELD_MASK(MUTEX_TYPE_SHIFT,MUTEX_TYPE_LEN)

#define  MUTEX_TYPE_NORMAL          0  /* Must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */
#define  MUTEX_TYPE_RECURSIVE       1
#define  MUTEX_TYPE_ERRORCHECK      2

#define  MUTEX_TYPE_TO_BITS(t)       FIELD_TO_BITS(t, MUTEX_TYPE_SHIFT, MUTEX_TYPE_LEN)

#define  MUTEX_TYPE_BITS_NORMAL      MUTEX_TYPE_TO_BITS(MUTEX_TYPE_NORMAL)
#define  MUTEX_TYPE_BITS_RECURSIVE   MUTEX_TYPE_TO_BITS(MUTEX_TYPE_RECURSIVE)
#define  MUTEX_TYPE_BITS_ERRORCHECK  MUTEX_TYPE_TO_BITS(MUTEX_TYPE_ERRORCHECK)


int pthread_mutex_init(pthread_mutex_t *mutex,
                       const pthread_mutexattr_t *attr)
{
    int value = 0;

    if (mutex == NULL)
        return EINVAL;

    if (__likely(attr == NULL)) {
        mutex->value = MUTEX_TYPE_BITS_NORMAL;
        return 0;
    }

    if ((*attr & MUTEXATTR_SHARED_MASK) != 0)
        value |= MUTEX_SHARED_MASK;

    switch (*attr & MUTEXATTR_TYPE_MASK) {
    case PTHREAD_MUTEX_NORMAL:
        value |= MUTEX_TYPE_BITS_NORMAL;//设置相应位置
        break;
    case PTHREAD_MUTEX_RECURSIVE:
        value |= MUTEX_TYPE_BITS_RECURSIVE;
        break;
    case PTHREAD_MUTEX_ERRORCHECK:
        value |= MUTEX_TYPE_BITS_ERRORCHECK;
        break;
    default:
        return EINVAL;
    }

    mutex->value = value;
    return 0;
}



那么Mutex就创建好了。
2. 接下来看看如何进行lock

inline status_t Mutex::lock() {
    return -pthread_mutex_lock(&mMutex);
}

进入pthread

int pthread_mutex_lock(pthread_mutex_t *mutex) {
    if (!__g_enable_deadlock_detection) {
        int err = pthread_mutex_lock_impl(mutex);
        return err;
    }
    int64_t startTime = uptime_millis();
    int err = pthread_mutex_trylock_impl(mutex);
    int wait_tid = gettid();
    if (err) {
        // two cases:
        // 1. mutex hold by other thread
        // 2. normal mutex acquired again
        int mvalue = mutex->value;
        int mtype = mvalue & MUTEX_TYPE_MASK;//设置type
        int tid = MUTEX_OWNER_FROM_BITS(mvalue);
#if NONRECURSIVE_REENTRANT_DETECT
        if (mtype == MUTEX_TYPE_BITS_NORMAL) {
            if (tid != 0 && wait_tid == tid) { // 2
                __dump_me("nonrecursive-mutex-lock-twice", tid, tid);
            }
        }
#endif
#if MUTEX_WARNING_ON
        int i = 0;
        for (i = 0; i < DEADLOCK_TIMEOUT/DEADLOCK_WARNING_PERIOD; ++i) {
            err = pthread_mutex_lock_timeout_np_impl(mutex, DEADLOCK_WARNING_PERIOD);
            if (!err)
                break;
            mvalue = mutex->value;
            tid = MUTEX_OWNER_FROM_BITS(mvalue);
            __show_mutex_info(mutex, uptime_millis() - startTime, tid);
        }
#else
        err = pthread_mutex_lock_timeout_np_impl(mutex, DEADLOCK_TIMEOUT);
#endif
        if (err) { // 1
            __dump_me("mutex-acquire-lock-timeout", tid, wait_tid);
        } else {
            while (1) {
                int newval = mvalue;
                newval |= (mvalue & FIELD_MASK(0, MUTEX_OWNER_SHIFT)) | MUTEX_OWNER_TO_BITS(wait_tid);
                if (__unlikely(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
                    mvalue = mutex->value;
                    continue;
                }
                ANDROID_MEMBAR_FULL();
                return 0;
            }
        }
    } else {
        int mvalue = mutex->value;
        int mtype = mutex->value & MUTEX_TYPE_MASK;
        if (mtype == MUTEX_TYPE_BITS_NORMAL) {
            while (1) {
                int newval = mvalue;
                newval |= (mvalue & FIELD_MASK(0, MUTEX_OWNER_SHIFT)) | MUTEX_OWNER_TO_BITS(wait_tid);
                //表示本线程已经持有了Mutex,那么更新其tid
                if (__unlikely(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
                    mvalue = mutex->value;
                    continue;
                }
                ANDROID_MEMBAR_FULL();
                return 0;
            }
        }
    }
    return err;
}



__LIBC_HIDDEN__
int pthread_mutex_lock_impl(pthread_mutex_t *mutex)
{
    int mvalue, mtype, tid, new_lock_type, shared;

    if (__unlikely(mutex == NULL))
        return EINVAL;

    mvalue = mutex->value;
    mtype = (mvalue & MUTEX_TYPE_MASK);
    shared = (mvalue & MUTEX_SHARED_MASK);

    /* Handle normal case first */
//情况1
    if ( __likely(mtype == MUTEX_TYPE_BITS_NORMAL) ) {//normal type调用
        _normal_lock(mutex, shared);
        return 0;
    }
//情况2
    /* Do we already own this recursive or error-check mutex ? */
    tid = __get_thread()->kernel_id;
    if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
        return _recursive_increment(mutex, mvalue, mtype);

    /* Add in shared state to avoid extra 'or' operations below */
    mtype |= shared;
//情况3
    /* First, if the mutex is unlocked, try to quickly acquire it.
     * In the optimistic case where this works, set the state to 1 to
     * indicate locked with no contention */
    if (mvalue == mtype) {
        int newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
        if (__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0) {
            ANDROID_MEMBAR_FULL();
            return 0;
        }
        /* argh, the value changed, reload before entering the loop */
        mvalue = mutex->value;
    }
//情况4
    for (;;) {
        int newval;

        /* if the mutex is unlocked, its value should be 'mtype' and
         * we try to acquire it by setting its owner and state atomically.
         * NOTE: We put the state to 2 since we _know_ there is contention
         * when we are in this loop. This ensures all waiters will be
         * unlocked.
         */
        if (mvalue == mtype) {
            newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
            /* TODO: Change this to __bionic_cmpxchg_acquire when we
             *        implement it to get rid of the explicit memory
             *        barrier below.
             */
            if (__unlikely(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
                mvalue = mutex->value;
                continue;
            }
            ANDROID_MEMBAR_FULL();
            return 0;
        }
//情况5
        /* the mutex is already locked by another thread, if its state is 1
         * we will change it to 2 to indicate contention. */
        if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
            newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); /* locked state 1 => state 2 */
            if (__unlikely(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
                mvalue = mutex->value;
                continue;
            }
            mvalue = newval;
        }

        /* wait until the mutex is unlocked */
        __futex_wait_ex(&mutex->value, shared, mvalue, NULL);

        mvalue = mutex->value;
    }
    /* NOTREACHED */
}

情况1:Normal。调用_normal_lock 

static __inline__ void
_normal_lock(pthread_mutex_t*  mutex, int shared)
{
    /* convenience shortcuts */
    const int unlocked           = shared | MUTEX_STATE_BITS_UNLOCKED;
    const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
    取得locked状态,
//锁状态宏如下:

下面是锁的三种状态:
无锁
非竞争锁
竞争锁
后面两中情况应该是这样理解,如果这个锁无持有这,无锁状态,那么当第一个持有这,持有它的时候,将它设为uncontended状态
如果非本线程持有,不管是否获取到了该锁,都将它设置为contended状态,表示这个锁是几个线程竞争的(待验证)

//#define  MUTEX_STATE_BITS_UNLOCKED            MUTEX_STATE_TO_BITS(MUTEX_STATE_UNLOCKED)
//#define  MUTEX_STATE_BITS_LOCKED_UNCONTENDED  MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_UNCONTENDED)
//#define  MUTEX_STATE_BITS_LOCKED_CONTENDED    MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_CONTENDED)


/* * The common case is an unlocked mutex, so we begin by trying to * change the lock's state from 0 (UNLOCKED) to 1 (LOCKED). * __bionic_cmpxchg() returns 0 if it made the swap successfully. * If the result is nonzero, this lock is already held by another thread.
翻译为:
尝试去将lock状态从unlocked转换为locked,如果转换成功则返回0
其实际就是将Mutex->value与unlocked比较,如果两个值相同,那么将value设置为locked_uncontended 
*/ if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) != 0) { const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
 /*返回非0,说明mutex->value不是unlocked状态,那么应该被其他线程占用着
 * We want to go to sleep until the mutex is available, which * requires promoting it to state 2 (CONTENDED). We need to * swap in the new state value and then wait until somebody wakes us up. * * __bionic_swap() returns the previous value. We swap 2 in and * see if we got zero back; if so, we have acquired the lock. If * not, another thread still holds the lock and we wait again. * * The second argument to the __futex_wait() call is compared * against the current value. If it doesn't match, __futex_wait() * returns immediately (otherwise, it sleeps for a time specified * by the third argument; 0 means sleep forever). This ensures * that the mutex is in state 2 when we go to sleep on it, which * guarantees a wake-up call. */
//循环等待其他线程释放这个mutex
 while (__bionic_swap(locked_contended, &mutex->value) != unlocked) __futex_wait_ex(&mutex->value, shared, locked_contended, 0); } ANDROID_MEMBAR_FULL();}

__ATOMIC_INLINE__ int
__bionic_cmpxchg(int32_t old_value, int32_t new_value, volatile int32_t* ptr)
{
    /* We must return 0 on success */
    return __sync_bool_compare_and_swap(ptr, old_value, new_value) == 0;
//也就是如果*ptr==old_value,那么将*ptr设置为new_value,返回true;否则返回false;

}

__ATOMIC_INLINE__ int32_t
__bionic_swap(int32_t new_value, volatile int32_t* ptr)
{
    int32_t prev;
    do {
        prev = *ptr;
        status = __sync_val_compare_and_swap(ptr, prev, new_value);
    } while (status == 0);//如果prev == ptr那么将new_value设置到ptr, 返回prev;
    return prev;
}
情况2:当tid == MUTEX_OWNER_FROM_BITS(mvalue) 说明。本线程已经拥有它了,那么只是将计数+1;

static __inline__ __attribute__((always_inline)) 
int _recursive_increment(pthread_mutex_t* mutex, int mvalue, int mtype) 
{ 
	if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) { 
	/* trying to re-lock a mutex we already acquired */ 
		return EDEADLK; 
		} 
		/* Detect recursive lock overflow and return EAGAIN. 
		* This is safe because only the owner thread can modify the 
		* counter bits in the mutex value. */ 
	if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(mvalue)) { 
	return EAGAIN; 
	} 
	/* We own the mutex, but other threads are able to change 
	* the lower bits (e.g. promoting it to "contended"), so we 
	* need to use an atomic cmpxchg loop to update the counter. 
	*/
	for (;;) { 
	/* increment counter, overflow was already checked */ 
	int newval = mvalue + MUTEX_COUNTER_BITS_ONE; 
	if (__likely(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) { 
	/* mutex is still locked, not need for a memory barrier */ 
	return 0; 
	} 
	/* the value was changed, this happens when another thread changes 
	* the lower state bits from 1 to 2 to indicate contention. This 
	* cannot change the counter, so simply reload and try again. 
	*/ 
	mvalue = mutex->value; 
	} 
 }

情况3: 没怎么弄懂,应该是一种优化的情况。

mtype |= shared;

    /* First, if the mutex is unlocked, try to quickly acquire it.
     * In the optimistic case where this works, set the state to 1 to
     * indicate locked with no contention 
    *///下面的值:tid拥有的mutex value|mtype|locke_uncontended竞争锁,其实也就是表示如果

      mvalue除了type和shared字段,其他字段都是空的,即value == mtype|shared,那么表示这个mutex没有任何线程获得,
      那么立即将它设置为1,表示其为非竞争锁

 if (mvalue == mtype) { int newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
 if (__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0) { ANDROID_MEMBAR_FULL(); return 0; } /* argh, the value changed, reload before entering the loop */ 
    mvalue = mutex->value; }

情况4: 如果情况1-2-3失败,即它不是normal锁,也不是本线程持有,也不是非竞争锁。 从结构分析来看
* bits: name description

* 31-16 tid owner thread's kernel id (recursive and errorcheck only)

* 15-14 type mutex type

* 13 shared process-shared flag

* 12-2 counter counter of recursive mutexes

* 1-0 state lock state (0, 1 or 2)

则可能如下情况: a. tid!=0 b. counter!=0; c. state!=0; 还是回到源码:

for (;;) {
        int newval;

        /* if the mutex is unlocked, its value should be 'mtype' and
         * we try to acquire it by setting its owner and state atomically.
         * NOTE: We put the state to 2 since we _know_ there is contention
         * when we are in this loop. This ensures all waiters will be
         * unlocked.
         */
        if (mvalue == mtype) {//之前的情况,其他都是0,mvalue == mtype|shared ,但是之前之所以失败,肯定其不是uncontended状态,因此现在将它设置为contended 状态
            newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
            /* TODO: Change this to __bionic_cmpxchg_acquire when we
             *        implement it to get rid of the explicit memory
             *        barrier below.
             */
            if (__unlikely(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
                mvalue = mutex->value;
                continue;
            }
            ANDROID_MEMBAR_FULL();
            return 0;
        }

        /* the mutex is already locked by another thread, if its state is 1
         * we will change it to 2 to indicate contention. */表示被其他线程持有,那么将其设置为contended,表示这个锁存在竞争了。
        if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
            newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); /* locked state 1 => state 2 */
            if (__unlikely(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
                mvalue = mutex->value;
                continue;
            }
            mvalue = newval;
        }

        /* wait until the mutex is unlocked */
        __futex_wait_ex(&mutex->value, shared, mvalue, NULL);

        mvalue = mutex->value;
    }
    /* NOTREACHED */

__futex_wait_ex相关的东西 需要以后好好研究。应该持续等待其他线程唤醒,对应的是

 __futex_wake_ex
3. 解锁

__LIBC_HIDDEN__
int pthread_mutex_unlock_impl(pthread_mutex_t *mutex)
{
    int mvalue, mtype, tid, oldv, shared;

    if (__unlikely(mutex == NULL))
        return EINVAL;

    mvalue = mutex->value;
    mtype  = (mvalue & MUTEX_TYPE_MASK);
    shared = (mvalue & MUTEX_SHARED_MASK);
//情况1.
    /* Handle common case first */
    if (__likely(mtype == MUTEX_TYPE_BITS_NORMAL)) {
        _normal_unlock(mutex, shared);
        return 0;
    }

//情况2.

 /* Do we already own this recursive or error-check mutex ? */ tid = __get_thread()->kernel_id; if ( tid != MUTEX_OWNER_FROM_BITS(mvalue) ) return EPERM;

//情况3.

 /* If the counter is > 0, we can simply decrement it atomically. * Since other threads can mutate the lower state bits (and only the * lower state bits), use a cmpxchg to do it. */ if (!MUTEX_COUNTER_BITS_IS_ZERO(mvalue)) { for (;;) { int newval = mvalue - MUTEX_COUNTER_BITS_ONE; if (__likely(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) { /* success: we still own the mutex, so no memory barrier */ return 0; } /* the value changed, so reload and loop */ mvalue = mutex->value; } } /* the counter is 0, so we're going to unlock the mutex by resetting * its value to 'unlocked'. We need to perform a swap in order * to read the current state, which will be 2 if there are waiters * to awake. * * TODO: Change this to __bionic_swap_release when we implement it * to get rid of the explicit memory barrier below. */

//情况4.


 ANDROID_MEMBAR_FULL(); /* RELEASE BARRIER */ mvalue = __bionic_swap(mtype | shared | MUTEX_STATE_BITS_UNLOCKED, &mutex->value); /* Wake one waiting thread, if any */ if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) { __futex_wake_ex(&mutex->value, shared, 1); } return 0;}


情况1:表示normal type进入到_normal_unlock

_normal_unlock(pthread_mutex_t*  mutex, int shared)
{
    ANDROID_MEMBAR_FULL();

    /*
     * The mutex state will be 1 or (rarely) 2.  We use an atomic decrement
     * to release the lock.  __bionic_atomic_dec() returns the previous value;
     * if it wasn't 1 we have to do some additional work.
     *///理解为下:

bionic_atomic_dec调用的__sync_fetch_and_add,是先获取state,然后-1,那么如果当前状态不是uncontended,则是contended,即需要唤醒其他等待的线程;
如果状态时uncontende,-1之后变为了0,表示为unlock状态。

  if (__bionic_atomic_dec(&mutex->value) != (shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED)) { /* * Start by releasing the lock. The decrement changed it from * "contended lock" to "uncontended lock", which means we still * hold it, and anybody who tries to sneak in will push it back * to state 2. * * Once we set it to zero the lock is up for grabs. We follow * this with a __futex_wake() to ensure that one of the waiting * threads has a chance to grab it. * * This doesn't cause a race with the swap/wait pair in * _normal_lock(), because the __futex_wait() call there will * return immediately if the mutex value isn't 2. */ mutex->value = shared; /* * Wake up one waiting thread. We don't know which thread will be * woken or when it'll start executing -- futexes make no guarantees * here. There may not even be a thread waiting. * * The newly-woken thread will replace the 0 we just set above * with 2, which means that when it eventually releases the mutex * it will also call FUTEX_WAKE. This results in one extra wake * call whenever a lock is contended, but lets us avoid forgetting * anyone without requiring us to track the number of sleepers. * * It's possible for another thread to sneak in and grab the lock * between the zero assignment above and the wake call below. If * the new thread is "slow" and holds the lock for a while, we'll * wake up a sleeper, which will swap in a 2 and then go back to * sleep since the lock is still held. If the new thread is "fast", * running to completion before we call wake, the thread we * eventually wake will find an unlocked mutex and will execute. * Either way we have correct behavior and nobody is orphaned on * the wait queue. */ __futex_wake_ex(&mutex->value, shared, 1); }}

情况2:既然不满足1情况,同时mutex非本线程持有,因此将其作为error处理;情况3:递归mutex

 if (!MUTEX_COUNTER_BITS_IS_ZERO(mvalue)) {//递归锁,表示计数非0,那么只是减去1
        for (;;) {
            int newval = mvalue - MUTEX_COUNTER_BITS_ONE;//将其对应位减去1
            if (__likely(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) {
                /* success: we still own the mutex, so no memory barrier */
                return 0;
            }
            /* the value changed, so reload and loop */
            mvalue = mutex->value;
        }
    }
情况4:
    /* the counter is 0, so we're going to unlock the mutex by resetting
     * its value to 'unlocked'. We need to perform a swap in order
     * to read the current state, which will be 2 if there are waiters
     * to awake.
     *
     * TODO: Change this to __bionic_swap_release when we implement it
     *        to get rid of the explicit memory barrier below.
     */
    ANDROID_MEMBAR_FULL();  /* RELEASE BARRIER */
    mvalue = __bionic_swap(mtype | shared | MUTEX_STATE_BITS_UNLOCKED, &mutex->value);
    /* Wake one waiting thread, if any */
    if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) {
        __futex_wake_ex(&mutex->value, shared, 1);
    }
    //将state设置为unlocked,然后判定mvalue是什么状态,如果是contended状态,那么唤醒其他的线程。
    return 0;

__ATOMIC_INLINE__ int32_t
__bionic_swap(int32_t new_value, volatile int32_t* ptr)
{
    int32_t prev;
    do {
        prev = *ptr;
        status = __sync_val_compare_and_swap(ptr, prev, new_value);
    } while (status == 0);
    return prev; 就是交换之后,返回先前的值
}
4. trylock
int pthread_mutex_trylock_impl(pthread_mutex_t *mutex)
{
    int mvalue, mtype, tid, oldv, shared;

    if (__unlikely(mutex == NULL))
        return EINVAL;

    mvalue = mutex->value;
    mtype  = (mvalue & MUTEX_TYPE_MASK);
    shared = (mvalue & MUTEX_SHARED_MASK);
//情况1:
    /* Handle common case first */
    if ( __likely(mtype == MUTEX_TYPE_BITS_NORMAL) )
    {
        if (__bionic_cmpxchg(shared|MUTEX_STATE_BITS_UNLOCKED,
                             shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED,
                             &mutex->value) == 0) {
            ANDROID_MEMBAR_FULL();
            return 0;
        }

        return EBUSY;
    }

//情况2:

 /* Do we already own this recursive or error-check mutex ? */ tid = __get_thread()->kernel_id; if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) ) return _recursive_increment(mutex, mvalue, mtype);

//情况3:

 /* Same as pthread_mutex_lock, except that we don't want to wait, and * the only operation that can succeed is a single cmpxchg to acquire the * lock if it is released / not owned by anyone. No need for a complex loop. */
     mtype |= shared | MUTEX_STATE_BITS_UNLOCKED;
    //假设其处于unlocked状态,那么设置为tid|mtype|UNcontended。 
    mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; 
    if (__likely(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) { 
        ANDROID_MEMBAR_FULL(); 
        return 0; 
    } 
    return EBUSY;
}
trylock其实就是说,我尝试去lock,如果没成功,我不会去持续等待。

情况1:NormalType 要么获取到了,要么返回ebusy

情况2:表示本线程已经持有,那么+1就ok

情况3:如果处于unlocked状态,那么果断的将其设置为uncontende,如果失败返回ebusy.

5. destory
int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
    int ret;

    /* use trylock to ensure that the mutex value is
     * valid and is not already locked. */
    ret = pthread_mutex_trylock_impl(mutex);
    if (ret != 0)
        return ret;

    mutex->value = 0xdead10cc;
    return 0;
}

进行trylock会发现没有被锁,那么就可以释放。设置为

 0xdead10cc

这有什么特殊含义?貌似就是告诉你 dead lock

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值