Commit c16c13b1 authored by hyatt@apple.com's avatar hyatt@apple.com

2009-06-12 Dave Hyatt <hyatt@apple.com>

        Reviewed by Anders Carlsson.

        https://bugs.webkit.org/show_bug.cgi?id=26373

        Add a new class to Threading in wtf called ReadWriteLock that handles single writer/multiple reader locking.
        Provide a pthreads-only implementation of the lock for now, as this class is only going to be used
        on Snow Leopard at first.

        * wtf/Threading.h:
        (WTF::ReadWriteLock::impl):
        * wtf/ThreadingPthreads.cpp:
        (WTF::ReadWriteLock::ReadWriteLock):
        (WTF::ReadWriteLock::~ReadWriteLock):
        (WTF::ReadWriteLock::readLock):
        (WTF::ReadWriteLock::tryReadLock):
        (WTF::ReadWriteLock::writeLock):
        (WTF::ReadWriteLock::tryWriteLock):
        (WTF::ReadWriteLock::unlock):



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@44651 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 1a5ffd8e
2009-06-12 Dave Hyatt <hyatt@apple.com>
Reviewed by Anders Carlsson.
https://bugs.webkit.org/show_bug.cgi?id=26373
Add a new class to Threading in wtf called ReadWriteLock that handles single writer/multiple reader locking.
Provide a pthreads-only implementation of the lock for now, as this class is only going to be used
on Snow Leopard at first.
* wtf/Threading.h:
(WTF::ReadWriteLock::impl):
* wtf/ThreadingPthreads.cpp:
(WTF::ReadWriteLock::ReadWriteLock):
(WTF::ReadWriteLock::~ReadWriteLock):
(WTF::ReadWriteLock::readLock):
(WTF::ReadWriteLock::tryReadLock):
(WTF::ReadWriteLock::writeLock):
(WTF::ReadWriteLock::tryWriteLock):
(WTF::ReadWriteLock::unlock):
2009-06-12 Oliver Hunt <oliver@apple.com> 2009-06-12 Oliver Hunt <oliver@apple.com>
Reviewed by Geoff Garen. Reviewed by Geoff Garen.
...@@ -126,18 +126,22 @@ void detachThread(ThreadIdentifier); ...@@ -126,18 +126,22 @@ void detachThread(ThreadIdentifier);
#if USE(PTHREADS) #if USE(PTHREADS)
typedef pthread_mutex_t PlatformMutex; typedef pthread_mutex_t PlatformMutex;
typedef pthread_rwlock_t PlatformReadWriteLock;
typedef pthread_cond_t PlatformCondition; typedef pthread_cond_t PlatformCondition;
#elif PLATFORM(GTK) #elif PLATFORM(GTK)
typedef GOwnPtr<GMutex> PlatformMutex; typedef GOwnPtr<GMutex> PlatformMutex;
typedef void* PlatformReadWriteLock; // FIXME: Implement.
typedef GOwnPtr<GCond> PlatformCondition; typedef GOwnPtr<GCond> PlatformCondition;
#elif PLATFORM(QT) #elif PLATFORM(QT)
typedef QT_PREPEND_NAMESPACE(QMutex)* PlatformMutex; typedef QT_PREPEND_NAMESPACE(QMutex)* PlatformMutex;
typedef void* PlatformReadWriteLock; // FIXME: Implement.
typedef QT_PREPEND_NAMESPACE(QWaitCondition)* PlatformCondition; typedef QT_PREPEND_NAMESPACE(QWaitCondition)* PlatformCondition;
#elif PLATFORM(WIN_OS) #elif PLATFORM(WIN_OS)
struct PlatformMutex { struct PlatformMutex {
CRITICAL_SECTION m_internalMutex; CRITICAL_SECTION m_internalMutex;
size_t m_recursionCount; size_t m_recursionCount;
}; };
typedef void* PlatformReadWriteLock; // FIXME: Implement.
struct PlatformCondition { struct PlatformCondition {
size_t m_waitersGone; size_t m_waitersGone;
size_t m_waitersBlocked; size_t m_waitersBlocked;
...@@ -151,6 +155,7 @@ struct PlatformCondition { ...@@ -151,6 +155,7 @@ struct PlatformCondition {
}; };
#else #else
typedef void* PlatformMutex; typedef void* PlatformMutex;
typedef void* PlatformReadWriteLock;
typedef void* PlatformCondition; typedef void* PlatformCondition;
#endif #endif
...@@ -171,6 +176,23 @@ private: ...@@ -171,6 +176,23 @@ private:
typedef Locker<Mutex> MutexLocker; typedef Locker<Mutex> MutexLocker;
class ReadWriteLock : Noncopyable {
public:
ReadWriteLock();
~ReadWriteLock();
void readLock();
bool tryReadLock();
void writeLock();
bool tryWriteLock();
void unlock();
private:
PlatformReadWriteLock m_readWriteLock;
};
class ThreadCondition : Noncopyable { class ThreadCondition : Noncopyable {
public: public:
ThreadCondition(); ThreadCondition();
......
...@@ -267,6 +267,61 @@ void Mutex::unlock() ...@@ -267,6 +267,61 @@ void Mutex::unlock()
ASSERT_UNUSED(result, !result); ASSERT_UNUSED(result, !result);
} }
ReadWriteLock::ReadWriteLock()
{
pthread_rwlock_init(&m_readWriteLock, NULL);
}
ReadWriteLock::~ReadWriteLock()
{
pthread_rwlock_destroy(&m_readWriteLock);
}
void ReadWriteLock::readLock()
{
int result = pthread_rwlock_rdlock(&m_readWriteLock);
ASSERT_UNUSED(result, !result);
}
bool ReadWriteLock::tryReadLock()
{
int result = pthread_rwlock_tryrdlock(&m_readWriteLock);
if (result == 0)
return true;
if (result == EBUSY || result == EAGAIN)
return false;
ASSERT_NOT_REACHED();
return false;
}
void ReadWriteLock::writeLock()
{
int result = pthread_rwlock_wrlock(&m_readWriteLock);
ASSERT_UNUSED(result, !result);
}
bool ReadWriteLock::tryWriteLock()
{
int result = pthread_rwlock_trywrlock(&m_readWriteLock);
if (result == 0)
return true;
if (result == EBUSY || result == EAGAIN)
return false;
ASSERT_NOT_REACHED();
return false;
}
void ReadWriteLock::unlock()
{
int result = pthread_rwlock_unlock(&m_readWriteLock);
ASSERT_UNUSED(result, !result);
}
ThreadCondition::ThreadCondition() ThreadCondition::ThreadCondition()
{ {
pthread_cond_init(&m_condition, NULL); pthread_cond_init(&m_condition, NULL);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment