Android 中的锁Mutex和条件Condition

Mutex

Mutex是互斥类,用于多线程访问同一个资源的时候,保证一次只有一个线程能访问该资源

Mutex的声明和实现

//Mutex.h
 class Mutex {
48  public:
49      enum {
50          PRIVATE = 0,
51          SHARED = 1
52      };
53  
54                  Mutex();
55      explicit    Mutex(const char* name);
56      explicit    Mutex(int type, const char* name = NULL);
57                  ~Mutex();
58  
59      // lock or unlock the mutex
60      status_t    lock();
61      void        unlock();
62  
63      // lock if possible; returns 0 on success, error otherwise
64      status_t    tryLock();
65  
66  #if defined(__ANDROID__)
67      // Lock the mutex, but don't wait longer than timeoutNs (relative time).
68      // Returns 0 on success, TIMED_OUT for failure due to timeout expiration.
69      //
70      // OSX doesn't have pthread_mutex_timedlock() or equivalent. To keep
71      // capabilities consistent across host OSes, this method is only available
72      // when building Android binaries.
73      //
74      // FIXME?: pthread_mutex_timedlock is based on CLOCK_REALTIME,
75      // which is subject to NTP adjustments, and includes time during suspend,
76      // so a timeout may occur even though no processes could run.
77      // Not holding a partial wakelock may lead to a system suspend.
78      status_t    timedLock(nsecs_t timeoutNs);
79  #endif
80  
81      // Manages the mutex automatically. It'll be locked when Autolock is
82      // constructed and released when Autolock goes out of scope.
83      class Autolock {
84      public:
85          inline explicit Autolock(Mutex& mutex) : mLock(mutex)  { mLock.lock(); }
86          inline explicit Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); }
87          inline ~Autolock() { mLock.unlock(); }
88      private:
89          Mutex& mLock;
90      };
91  
92  private:
93      friend class Condition;
94  
95      // A mutex cannot be copied
96                  Mutex(const Mutex&);
97      Mutex&      operator = (const Mutex&);
98  
99  #if !defined(_WIN32)
100      pthread_mutex_t mMutex;
101  #else
102      void    _init();
103      void*   mState;
104  #endif
105  };
106  
107  // ---------------------------------------------------------------------------
108  
109  #if !defined(_WIN32)
110  
111  inline Mutex::Mutex() {
112      pthread_mutex_init(&mMutex, NULL);
113  }
114  inline Mutex::Mutex(__attribute__((unused)) const char* name) {
115      pthread_mutex_init(&mMutex, NULL);
116  }
117  inline Mutex::Mutex(int type, __attribute__((unused)) const char* name) {
118      if (type == SHARED) {
119          pthread_mutexattr_t attr;
120          pthread_mutexattr_init(&attr);
121          pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
122          pthread_mutex_init(&mMutex, &attr);
123          pthread_mutexattr_destroy(&attr);
124      } else {
125          pthread_mutex_init(&mMutex, NULL);
126      }
127  }
128  inline Mutex::~Mutex() {
129      pthread_mutex_destroy(&mMutex);
130  }
131  inline status_t Mutex::lock() {
132      return -pthread_mutex_lock(&mMutex);
133  }
134  inline void Mutex::unlock() {
135      pthread_mutex_unlock(&mMutex);
136  }
137  inline status_t Mutex::tryLock() {
138      return -pthread_mutex_trylock(&mMutex);
139  }
140  #if defined(__ANDROID__)
141  inline status_t Mutex::timedLock(nsecs_t timeoutNs) {
142      timeoutNs += systemTime(SYSTEM_TIME_REALTIME);
143      const struct timespec ts = {
144          /* .tv_sec = */ static_cast<time_t>(timeoutNs / 1000000000),
145          /* .tv_nsec = */ static_cast<long>(timeoutNs % 1000000000),
146      };
147      return -pthread_mutex_timedlock(&mMutex, &ts);
148  }
149  #endif
150  
151  #endif // !defined(_WIN32)
152  
153  // ---------------------------------------------------------------------------
154  
155  /*
156   * Automatic mutex.  Declare one of these at the top of a function.
157   * When the function returns, it will go out of scope, and release the
158   * mutex.
159   */
160  
161  typedef Mutex::Autolock AutoMutex;
162  
163  // ---------------------------------------------------------------------------
164  }; // namespace android
165  // ---------------------------------------------------------------------------
166  
167  #endif // _LIBS_UTILS_MUTEX_H

Mutex最重要的是lock和unlock函数
lock函数:调用后加锁,如果这块区域之前已被别人锁住,lock函数则会等待,直到可以进入这块区域为止。系统保证一次只有一个线程能lock成功。
unlock函数:调用Mutex的unlock以释放互斥区域。这样,其他的lock才可以成功返回。
trylock函数:该函数只是尝试去锁住该区域,使用者需要根据trylock的返回值来判断是否成功锁住了该区域。

AutoLock

AutoLock类是定义在Mutex内部的一个类,利用了C++的构造和析构函数,实现在AutoLock对象生命周期内自动加锁以及释放锁的操作。

class Autolock {
    public:
        //构造的时候调用lock。
        inline Autolock(Mutex& mutex) : mLock(mutex)  { mLock.lock(); }
        inline Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); }
        //析构的时候调用unlock。
        inline ~Autolock() { mLock.unlock(); }
    private:
        Mutex& mLock;
    };

当Autolock构造时,主动调用内部成员变量mLock的lock()方法,而在析构时正好相反,调用它的unlock()方法释放锁。这样的话,假如一个Autolock对象是局部变量,则在生命周期结束时就自动的把资源锁解了。

Condition

主要函数:
wait(Mutex& mutex) 线程B和C等待事件
waitRelative(Mutex& mutex, nsecs_t reltime) 线程B和C的超时等待,B和C可以指定等待时间,当超过这个时间,条件却还不满足,则退出等待。
signal() 触发者A用来通知条件已经满足,但是B和C只有一个会被唤醒。
broadcast() 触发者A用来通知条件已经满足,所有等待者都会被唤醒。(广播)

Condition需要配合Mutex使用,它是条件,也是一种标志,一个与线程相互关联的对象


class Condition {
public:
    enum {
        PRIVATE = 0,
        SHARED = 1
    };


    Condition();
    Condition(int type);//如果type是SHARED,表示支持跨进程的条件同步
    ~Condition();

    status_t wait(Mutex& mutex);

    status_t waitRelative(Mutex& mutex, nsecs_t reltime);

    void signal();

    void broadcast();


private:
#if defined(HAVE_PTHREADS)
    pthread_cond_t mCond;
#else
    void*   mState;
#endif
}

比如:

//android/frameworks/av/services/audioflinger/Threads.h
class HenrySenseThread : public Thread {
public:
    MaxxSenseThread(const sp<AudioFlinger>& audioFlinger);

    void signal();

    // Thread virtuals
    bool threadLoop();

    void exit();

private:
    void activeTracksChanged();

    status_t getActiveTracksPid(int *pSize, int *pActiveClientPid);

    sp<IMediaPlayerService> sMediaPlayerService;

    const sp<IMediaPlayerService>& getMediaPlayerService();

    status_t GetActivePidsFromMediaPlayerService(int *size, int *pids);

    const sp<AudioFlinger>  mAudioFlinger;

    Condition               mWaitWorkCV;
    Mutex                   mLock;

    bool                    mNeedToCheck;

    effect_descriptor_t     mDescriptor;
};
#endif

在类HenrySenseThread 中声明了Condition 变量 mWaitWorkCV ;

//android/frameworks/av/services/audioflinger/Threads.cpp
void AudioFlinger::HenrySenseThread::signal()
{
    ALOGV("signal()");

    mLock.lock();
    mNeedToCheck = true;
    mWaitWorkCV.signal(); //调用唤醒处于等待状态mWaitWorkCV.wait(mlock)的地方继续执行
    mLock.unlock();
}

bool AudioFlinger::MaxxSenseThread::threadLoop()
{
    ALOGV("threadLoop()");
    if (exitPending()) {
        return false;
    }

    mLock.lock();
    if (!mNeedToCheck) {
        mWaitWorkCV.wait(mLock);//当前线程在条件mWaitWorkCV下处于等待状态,调用 mWaitWorkCV.signal()后将被唤醒
    }
    mNeedToCheck = false;
    mLock.unlock();

    if (exitPending()) {
        return false;
    }

    activeTracksChanged();
    return true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安德路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值