Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / cpus / vonk / bl / lib / utl / src / BL_Mutex.h
/*
* ========== Copyright Header Begin ==========================================
*
* OpenSPARC T2 Processor File: BL_Mutex.h
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
*
* The above named program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software Foundation.
*
* The above named program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this work; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ========== Copyright Header End ============================================
*/
#ifndef __BL_Mutex_h__
#define __BL_Mutex_h__
//#include <thread.h>
//#include <synch.h>
//#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
class BL_CondVar
{
public:
BL_CondVar() { pthread_cond_init(&cond,NULL); }
~BL_CondVar() { pthread_cond_destroy(&cond); }
int signal() { return pthread_cond_signal(&cond); }
int broadcast() { return pthread_cond_broadcast(&cond); }
private:
BL_CondVar( const BL_CondVar& m ) {}
friend class BL_Mutex;
pthread_cond_t cond;
};
class BL_Mutex
{
public:
BL_Mutex() { pthread_mutex_init(&mutex,NULL); }
~BL_Mutex() { pthread_mutex_destroy(&mutex); }
BL_Mutex( const BL_Mutex& m )
: mutex(m.mutex)
{
// this code guarantees that the mutex is unlock when BL_Mutex is
// copied
if (trylock())
{
fprintf(stderr, "BL_Mutex( const BL_Mutex& m ): mutex at %p is held\n",
&m.mutex);
exit(1);
}
else
unlock();
}
int lock() { return pthread_mutex_lock(&mutex); }
int unlock() { return pthread_mutex_unlock(&mutex); }
int trylock() { return pthread_mutex_trylock(&mutex); }
int wait( BL_CondVar& cv ) { return pthread_cond_wait(&cv.cond,&mutex); }
protected:
pthread_mutex_t mutex;
};
// BL_RdWrLock is a wrapper around the solaris pthread_rwlock.
// Be carefull when and where you use this as this lock is at
// least 4.5 times more expensive as a mutex. So don't use this
// to saveguard tlb access for example. Actually avoid using it.
class BL_RdWrLock
{
public:
BL_RdWrLock() { pthread_rwlock_init(&rw,NULL); }
~BL_RdWrLock() { pthread_rwlock_destroy(&rw); }
int rdlock() { return pthread_rwlock_rdlock(&rw); }
int rdunlock() { return pthread_rwlock_unlock(&rw); }
int tryrdlock() { return pthread_rwlock_tryrdlock(&rw); }
int wrlock() { return pthread_rwlock_wrlock(&rw); }
int wrunlock() { return pthread_rwlock_unlock(&rw); }
int trywrlock() { return pthread_rwlock_trywrlock(&rw); }
private:
BL_RdWrLock( const BL_RdWrLock& ) {}
pthread_rwlock_t rw;
};
#endif