2012-10-31 15:52:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkCondVar_DEFINED
|
|
|
|
#define SkCondVar_DEFINED
|
|
|
|
|
2012-10-31 19:29:13 +00:00
|
|
|
#ifdef SK_USE_POSIX_THREADS
|
2012-10-31 15:52:16 +00:00
|
|
|
#include <pthread.h>
|
2012-10-31 19:29:13 +00:00
|
|
|
#elif defined(SK_BUILD_FOR_WIN32)
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
2012-10-31 15:52:16 +00:00
|
|
|
|
2012-10-31 19:29:13 +00:00
|
|
|
/**
|
|
|
|
* Condition variable for blocking access to shared data from other threads and
|
|
|
|
* controlling which threads are awake.
|
|
|
|
*
|
|
|
|
* Currently only supported on platforms with posix threads and Windows Vista and
|
|
|
|
* above.
|
|
|
|
*/
|
2012-10-31 15:52:16 +00:00
|
|
|
class SkCondVar {
|
|
|
|
public:
|
|
|
|
SkCondVar();
|
|
|
|
~SkCondVar();
|
|
|
|
|
2012-10-31 19:29:13 +00:00
|
|
|
/**
|
|
|
|
* Lock a mutex. Must be done before calling the other functions on this object.
|
|
|
|
*/
|
2012-10-31 15:52:16 +00:00
|
|
|
void lock();
|
2012-10-31 19:29:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlock the mutex.
|
|
|
|
*/
|
2012-10-31 15:52:16 +00:00
|
|
|
void unlock();
|
|
|
|
|
|
|
|
/**
|
2012-10-31 19:29:13 +00:00
|
|
|
* Pause the calling thread. Will be awoken when signal() or broadcast() is called.
|
|
|
|
* Must be called while lock() is held (but gives it up while waiting). Once awoken,
|
|
|
|
* the calling thread will hold the lock once again.
|
2012-10-31 15:52:16 +00:00
|
|
|
*/
|
|
|
|
void wait();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wake one thread waiting on this condition. Must be called while lock()
|
|
|
|
* is held.
|
|
|
|
*/
|
|
|
|
void signal();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wake all threads waiting on this condition. Must be called while lock()
|
|
|
|
* is held.
|
|
|
|
*/
|
|
|
|
void broadcast();
|
|
|
|
|
|
|
|
private:
|
2012-10-31 19:29:13 +00:00
|
|
|
#ifdef SK_USE_POSIX_THREADS
|
2012-10-31 15:52:16 +00:00
|
|
|
pthread_mutex_t fMutex;
|
|
|
|
pthread_cond_t fCond;
|
2012-10-31 19:29:13 +00:00
|
|
|
#elif defined(SK_BUILD_FOR_WIN32)
|
|
|
|
CRITICAL_SECTION fCriticalSection;
|
|
|
|
CONDITION_VARIABLE fCondition;
|
|
|
|
#endif
|
2012-10-31 15:52:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|