Line 1... |
Line 1... |
/* $Id: thread.hh,v 1.9 2008-04-27 16:33:42 sybreon Exp $
|
/* $Id: thread.hh,v 1.10 2008-04-28 20:29:15 sybreon Exp $
|
**
|
**
|
** AEMB2 HI-PERFORMANCE CPU
|
** AEMB2 HI-PERFORMANCE CPU
|
** Copyright (C) 2004-2007 Shawn Tan Ser Ngiap
|
** Copyright (C) 2004-2007 Shawn Tan Ser Ngiap
|
**
|
**
|
** This file is part of AEMB.
|
** This file is part of AEMB.
|
Line 27... |
Line 27... |
provides simple mechanisms for toggling semaphores.
|
provides simple mechanisms for toggling semaphores.
|
*/
|
*/
|
|
|
#include "aemb/msr.hh"
|
#include "aemb/msr.hh"
|
|
|
#ifndef AEMB_THREAD_HH
|
#ifndef _AEMB_THREAD_HH
|
#define AEMB_THREAD_HH
|
#define _AEMB_THREAD_HH
|
|
|
#ifdef __cplusplus
|
#ifdef __cplusplus
|
namespace aemb {
|
extern "C" {
|
#endif
|
#endif
|
|
|
/**
|
/**
|
Checks to see if currently executing Thread 1
|
Checks to see if currently executing Thread 1
|
@return true if is Thread 1
|
@return true if is Thread 1
|
*/
|
*/
|
|
|
inline int isThread1()
|
inline int aembIsThread1()
|
{
|
{
|
int rmsr = getMSR();
|
int rmsr = aembGetMSR();
|
return ((rmsr & MSR_HTX) && (rmsr & MSR_PHA));
|
return ((rmsr & AEMB_MSR_HTX) && (rmsr & AEMB_MSR_PHA));
|
}
|
}
|
|
|
/**
|
/**
|
Checks to see if currently executing Thread 0
|
Checks to see if currently executing Thread 0
|
@return true if is Thread 0
|
@return true if is Thread 0
|
*/
|
*/
|
|
|
inline int isThread0()
|
inline int aembIsThread0()
|
{
|
{
|
int rmsr = getMSR();
|
int rmsr = aembGetMSR();
|
return ((rmsr & MSR_HTX) && (!(rmsr & MSR_PHA)));
|
return ((rmsr & AEMB_MSR_HTX) && (!(rmsr & AEMB_MSR_PHA)));
|
}
|
}
|
|
|
/**
|
/**
|
Checks to see if it is multi-threaded or not.
|
Checks to see if it is multi-threaded or not.
|
@return true if thread capable
|
@return true if thread capable
|
*/
|
*/
|
inline int isThreaded()
|
inline int aembIsThreaded()
|
{
|
{
|
int rmsr = getMSR();
|
int rmsr = aembGetMSR();
|
return (rmsr & MSR_HTX);
|
return (rmsr & AEMB_MSR_HTX);
|
}
|
}
|
|
|
/**
|
/**
|
Hardware Mutex Signal.
|
Hardware Mutex Signal.
|
Unlock the hardware mutex, which is unlocked on reset.
|
Unlock the hardware mutex, which is unlocked on reset.
|
*/
|
*/
|
inline void _mtx_free()
|
inline void _aembFreeMTX()
|
{
|
{
|
int tmp;
|
int tmp;
|
asm volatile ("msrclr %0, %1":"=r"(tmp):"K"(MSR_MTX));
|
asm volatile ("msrclr %0, %1":"=r"(tmp):"K"(AEMB_MSR_MTX));
|
}
|
}
|
|
|
/**
|
/**
|
Hardware Mutex Wait.
|
Hardware Mutex Wait.
|
|
|
Waits until the hardware mutex is unlocked. This should be used
|
Waits until the hardware mutex is unlocked. This should be used
|
as part of a larger software mutex mechanism.
|
as part of a larger software mutex mechanism.
|
*/
|
*/
|
inline void _mtx_lock()
|
inline void _aembLockMTX()
|
{
|
{
|
int rmsr;
|
int rmsr;
|
do
|
do
|
{
|
{
|
asm volatile ("msrset %0, %1":"=r"(rmsr):"K"(MSR_MTX));
|
asm volatile ("msrset %0, %1":"=r"(rmsr):"K"(AEMB_MSR_MTX));
|
}
|
|
while (rmsr & MSR_MTX);
|
|
}
|
|
|
|
// TODO: Extend this library to include threading mechanisms such as
|
|
// semaphores, mutexes and such.
|
|
|
|
/**
|
|
Semaphore struct.
|
|
Presently implemented as software solution but a hardware one may be
|
|
required as the threads are hardware.
|
|
*/
|
|
|
|
typedef int semaphore;
|
|
|
|
/**
|
|
Software Semaphore Signal.
|
|
|
|
Increment the semaphore and run. This is a software mechanism.
|
|
*/
|
|
inline void signal(volatile semaphore _sem)
|
|
{
|
|
_mtx_lock();
|
|
_sem++;
|
|
_mtx_free();
|
|
}
|
|
|
|
/**
|
|
Software Semaphore Wait.
|
|
|
|
Decrement the semaphore and block if < 0. This is a software
|
|
mechanism.
|
|
*/
|
|
inline void wait(volatile semaphore _sem)
|
|
{
|
|
_mtx_lock();
|
|
_sem--;
|
|
_mtx_free();
|
|
while (_sem < 0);
|
|
}
|
|
|
|
semaphore __mutex_rendezvous0 = 0; ///< internal rendezvous mutex
|
|
semaphore __mutex_rendezvous1 = 1; ///< internal rendezvous mutex
|
|
|
|
/**
|
|
Implements a simple rendezvous mechanism
|
|
*/
|
|
|
|
inline void rendezvous()
|
|
{
|
|
if (isThread1())
|
|
{
|
|
wait(__mutex_rendezvous0);
|
|
signal(__mutex_rendezvous1);
|
|
}
|
|
else
|
|
{
|
|
signal(__mutex_rendezvous0);
|
|
wait(__mutex_rendezvous1);
|
|
}
|
}
|
|
while (rmsr & AEMB_MSR_MTX);
|
}
|
}
|
|
|
#ifdef __cplusplus
|
#ifdef __cplusplus
|
}
|
}
|
#endif
|
#endif
|
|
|
#endif
|
#endif
|
|
|
/*
|
/*
|
$Log: not supported by cvs2svn $
|
$Log: not supported by cvs2svn $
|
|
Revision 1.9 2008/04/27 16:33:42 sybreon
|
|
License change to GPL3.
|
|
|
Revision 1.8 2008/04/26 19:31:35 sybreon
|
Revision 1.8 2008/04/26 19:31:35 sybreon
|
Made headers C compatible.
|
Made headers C compatible.
|
|
|
Revision 1.7 2008/04/26 18:05:22 sybreon
|
Revision 1.7 2008/04/26 18:05:22 sybreon
|
Minor cosmetic changes.
|
Minor cosmetic changes.
|