Cleanup common POSIX functionality.

The Mutex implementation is the same for all 6 POSIX platformats, just
like of them use the sched_yield() to implement Thread::YieldCPU().

R=dslomov@chromium.org

Review URL: https://codereview.chromium.org/18335008

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@15623 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
bmeurer@chromium.org 2013-07-11 11:37:08 +00:00
parent 6170afb734
commit e3676e9135
7 changed files with 53 additions and 281 deletions

View File

@ -574,57 +574,6 @@ void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
}
void Thread::YieldCPU() {
sched_yield();
}
class CygwinMutex : public Mutex {
public:
CygwinMutex() {
pthread_mutexattr_t attrs;
memset(&attrs, 0, sizeof(attrs));
int result = pthread_mutexattr_init(&attrs);
ASSERT(result == 0);
result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE);
ASSERT(result == 0);
result = pthread_mutex_init(&mutex_, &attrs);
ASSERT(result == 0);
}
virtual ~CygwinMutex() { pthread_mutex_destroy(&mutex_); }
virtual int Lock() {
int result = pthread_mutex_lock(&mutex_);
return result;
}
virtual int Unlock() {
int result = pthread_mutex_unlock(&mutex_);
return result;
}
virtual bool TryLock() {
int result = pthread_mutex_trylock(&mutex_);
// Return false if the lock is busy and locking failed.
if (result == EBUSY) {
return false;
}
ASSERT(result == 0); // Verify no other errors.
return true;
}
private:
pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.
};
Mutex* OS::CreateMutex() {
return new CygwinMutex();
}
class CygwinSemaphore : public Semaphore {
public:
explicit CygwinSemaphore(int count) { sem_init(&sem_, 0, count); }

View File

@ -568,56 +568,6 @@ void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
}
void Thread::YieldCPU() {
sched_yield();
}
class FreeBSDMutex : public Mutex {
public:
FreeBSDMutex() {
pthread_mutexattr_t attrs;
int result = pthread_mutexattr_init(&attrs);
ASSERT(result == 0);
result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE);
ASSERT(result == 0);
result = pthread_mutex_init(&mutex_, &attrs);
ASSERT(result == 0);
USE(result);
}
virtual ~FreeBSDMutex() { pthread_mutex_destroy(&mutex_); }
virtual int Lock() {
int result = pthread_mutex_lock(&mutex_);
return result;
}
virtual int Unlock() {
int result = pthread_mutex_unlock(&mutex_);
return result;
}
virtual bool TryLock() {
int result = pthread_mutex_trylock(&mutex_);
// Return false if the lock is busy and locking failed.
if (result == EBUSY) {
return false;
}
ASSERT(result == 0); // Verify no other errors.
return true;
}
private:
pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.
};
Mutex* OS::CreateMutex() {
return new FreeBSDMutex();
}
class FreeBSDSemaphore : public Semaphore {
public:
explicit FreeBSDSemaphore(int count) { sem_init(&sem_, 0, count); }

View File

@ -893,56 +893,6 @@ void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
}
void Thread::YieldCPU() {
sched_yield();
}
class LinuxMutex : public Mutex {
public:
LinuxMutex() {
pthread_mutexattr_t attrs;
int result = pthread_mutexattr_init(&attrs);
ASSERT(result == 0);
result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE);
ASSERT(result == 0);
result = pthread_mutex_init(&mutex_, &attrs);
ASSERT(result == 0);
USE(result);
}
virtual ~LinuxMutex() { pthread_mutex_destroy(&mutex_); }
virtual int Lock() {
int result = pthread_mutex_lock(&mutex_);
return result;
}
virtual int Unlock() {
int result = pthread_mutex_unlock(&mutex_);
return result;
}
virtual bool TryLock() {
int result = pthread_mutex_trylock(&mutex_);
// Return false if the lock is busy and locking failed.
if (result == EBUSY) {
return false;
}
ASSERT(result == 0); // Verify no other errors.
return true;
}
private:
pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.
};
Mutex* OS::CreateMutex() {
return new LinuxMutex();
}
class LinuxSemaphore : public Semaphore {
public:
explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); }

View File

@ -652,45 +652,6 @@ void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
}
void Thread::YieldCPU() {
sched_yield();
}
class MacOSMutex : public Mutex {
public:
MacOSMutex() {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex_, &attr);
}
virtual ~MacOSMutex() { pthread_mutex_destroy(&mutex_); }
virtual int Lock() { return pthread_mutex_lock(&mutex_); }
virtual int Unlock() { return pthread_mutex_unlock(&mutex_); }
virtual bool TryLock() {
int result = pthread_mutex_trylock(&mutex_);
// Return false if the lock is busy and locking failed.
if (result == EBUSY) {
return false;
}
ASSERT(result == 0); // Verify no other errors.
return true;
}
private:
pthread_mutex_t mutex_;
};
Mutex* OS::CreateMutex() {
return new MacOSMutex();
}
class MacOSSemaphore : public Semaphore {
public:
explicit MacOSSemaphore(int count) {

View File

@ -607,56 +607,6 @@ void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
}
void Thread::YieldCPU() {
sched_yield();
}
class OpenBSDMutex : public Mutex {
public:
OpenBSDMutex() {
pthread_mutexattr_t attrs;
int result = pthread_mutexattr_init(&attrs);
ASSERT(result == 0);
result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE);
ASSERT(result == 0);
result = pthread_mutex_init(&mutex_, &attrs);
ASSERT(result == 0);
USE(result);
}
virtual ~OpenBSDMutex() { pthread_mutex_destroy(&mutex_); }
virtual int Lock() {
int result = pthread_mutex_lock(&mutex_);
return result;
}
virtual int Unlock() {
int result = pthread_mutex_unlock(&mutex_);
return result;
}
virtual bool TryLock() {
int result = pthread_mutex_trylock(&mutex_);
// Return false if the lock is busy and locking failed.
if (result == EBUSY) {
return false;
}
ASSERT(result == 0); // Verify no other errors.
return true;
}
private:
pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.
};
Mutex* OS::CreateMutex() {
return new OpenBSDMutex();
}
class OpenBSDSemaphore : public Semaphore {
public:
explicit OpenBSDSemaphore(int count) { sem_init(&sem_, 0, count); }

View File

@ -31,6 +31,8 @@
#include "platform-posix.h"
#include <pthread.h>
#include <sched.h> // for sched_yield
#include <unistd.h>
#include <errno.h>
#include <time.h>
@ -398,6 +400,57 @@ void OS::StrNCpy(Vector<char> dest, const char* src, size_t n) {
}
// ----------------------------------------------------------------------------
// POSIX thread support.
//
void Thread::YieldCPU() {
sched_yield();
}
class POSIXMutex : public Mutex {
public:
POSIXMutex() {
pthread_mutexattr_t attr;
memset(&attr, 0, sizeof(attr));
int result = pthread_mutexattr_init(&attr);
ASSERT(result == 0);
result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
ASSERT(result == 0);
result = pthread_mutex_init(&mutex_, &attr);
ASSERT(result == 0);
result = pthread_mutexattr_destroy(&attr);
ASSERT(result == 0);
USE(result);
}
virtual ~POSIXMutex() { pthread_mutex_destroy(&mutex_); }
virtual int Lock() { return pthread_mutex_lock(&mutex_); }
virtual int Unlock() { return pthread_mutex_unlock(&mutex_); }
virtual bool TryLock() {
int result = pthread_mutex_trylock(&mutex_);
// Return false if the lock is busy and locking failed.
if (result == EBUSY) {
return false;
}
ASSERT(result == 0); // Verify no other errors.
return true;
}
private:
pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.
};
Mutex* OS::CreateMutex() {
return new POSIXMutex();
}
// ----------------------------------------------------------------------------
// POSIX socket support.
//

View File

@ -38,7 +38,6 @@
#include <ucontext.h> // walkstack(), getcontext()
#include <dlfcn.h> // dladdr
#include <pthread.h>
#include <sched.h> // for sched_yield
#include <semaphore.h>
#include <time.h>
#include <sys/time.h> // gettimeofday(), timeradd()
@ -539,46 +538,6 @@ void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
}
void Thread::YieldCPU() {
sched_yield();
}
class SolarisMutex : public Mutex {
public:
SolarisMutex() {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex_, &attr);
}
~SolarisMutex() { pthread_mutex_destroy(&mutex_); }
int Lock() { return pthread_mutex_lock(&mutex_); }
int Unlock() { return pthread_mutex_unlock(&mutex_); }
virtual bool TryLock() {
int result = pthread_mutex_trylock(&mutex_);
// Return false if the lock is busy and locking failed.
if (result == EBUSY) {
return false;
}
ASSERT(result == 0); // Verify no other errors.
return true;
}
private:
pthread_mutex_t mutex_;
};
Mutex* OS::CreateMutex() {
return new SolarisMutex();
}
class SolarisSemaphore : public Semaphore {
public:
explicit SolarisSemaphore(int count) { sem_init(&sem_, 0, count); }