/* $Id: semaphore.hh,v 1.1 2008-04-28 20:29:15 sybreon Exp $
|
/* $Id: semaphore.hh,v 1.1 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.
|
**
|
**
|
** AEMB is free software: you can redistribute it and/or modify it
|
** AEMB is free software: you can redistribute it and/or modify it
|
** under the terms of the GNU General Public License as published by
|
** under the terms of the GNU General Public License as published by
|
** the Free Software Foundation, either version 3 of the License, or
|
** the Free Software Foundation, either version 3 of the License, or
|
** (at your option) any later version.
|
** (at your option) any later version.
|
**
|
**
|
** AEMB is distributed in the hope that it will be useful, but WITHOUT
|
** AEMB is distributed in the hope that it will be useful, but WITHOUT
|
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
** or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
** or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
** License for more details.
|
** License for more details.
|
**
|
**
|
** You should have received a copy of the GNU General Public License
|
** You should have received a copy of the GNU General Public License
|
** along with AEMB. If not, see .
|
** along with AEMB. If not, see .
|
*/
|
*/
|
|
|
/**
|
/**
|
General semaphore library
|
General semaphore library
|
@file semaphore.hh
|
@file semaphore.hh
|
*/
|
*/
|
|
|
#include "thread.hh"
|
#include "thread.hh"
|
|
|
#ifndef _AEMB_SEMAPHORE_HH
|
#ifndef _AEMB_SEMAPHORE_HH
|
#define _AEMB_SEMAPHORE_HH
|
#define _AEMB_SEMAPHORE_HH
|
|
|
#ifdef __cplusplus
|
#ifdef __cplusplus
|
extern "C" {
|
extern "C" {
|
#endif
|
#endif
|
|
|
// TODO: Extend this library to include threading mechanisms such as
|
// TODO: Extend this library to include threading mechanisms such as
|
// semaphores, mutexes and such.
|
// semaphores, mutexes and such.
|
|
|
/**
|
/**
|
Semaphore struct.
|
Semaphore struct.
|
Presently implemented as software solution but a hardware one may be
|
Presently implemented as software solution but a hardware one may be
|
required as the threads are hardware.
|
required as the threads are hardware.
|
*/
|
*/
|
|
|
typedef int semaphore;
|
typedef int semaphore;
|
|
|
/**
|
/**
|
Software Semaphore Signal.
|
Software Semaphore Signal.
|
|
|
Increment the semaphore and run. This is a software mechanism.
|
Increment the semaphore and run. This is a software mechanism.
|
*/
|
*/
|
inline void aembSignal(volatile semaphore _sem)
|
static inline void aembSignal(volatile semaphore _sem)
|
{
|
{
|
_aembLockMTX();
|
_aembLockMTX();
|
_sem++;
|
_sem++;
|
_aembFreeMTX();
|
_aembFreeMTX();
|
}
|
}
|
|
|
/**
|
/**
|
Software Semaphore Wait.
|
Software Semaphore Wait.
|
|
|
Decrement the semaphore and block if < 0. This is a software
|
Decrement the semaphore and block if < 0. This is a software
|
mechanism.
|
mechanism.
|
*/
|
*/
|
inline void aembWait(volatile semaphore _sem)
|
static inline void aembWait(volatile semaphore _sem)
|
{
|
{
|
_aembLockMTX();
|
_aembLockMTX();
|
_sem--;
|
_sem--;
|
_aembFreeMTX();
|
_aembFreeMTX();
|
while (_sem < 0);
|
while (_sem < 0);
|
}
|
}
|
|
|
semaphore __mutex_rendezvous0 = 0; ///< internal rendezvous mutex
|
|
semaphore __mutex_rendezvous1 = 1; ///< internal rendezvous mutex
|
|
|
|
/**
|
/**
|
Implements a simple rendezvous mechanism
|
Implements a simple rendezvous mechanism
|
*/
|
*/
|
/*
|
/*
|
inline void aembRendezvous()
|
static inline void aembRendezvous()
|
{
|
{
|
if (isThread1())
|
if (isThread1())
|
{
|
{
|
wait(__mutex_rendezvous0);
|
wait(__mutex_rendezvous0);
|
signal(__mutex_rendezvous1);
|
signal(__mutex_rendezvous1);
|
}
|
}
|
else
|
else
|
{
|
{
|
signal(__mutex_rendezvous0);
|
signal(__mutex_rendezvous0);
|
wait(__mutex_rendezvous1);
|
wait(__mutex_rendezvous1);
|
}
|
}
|
}
|
}
|
*/
|
*/
|
|
|
#ifdef __cplusplus
|
#ifdef __cplusplus
|
}
|
}
|
#endif
|
#endif
|
|
|
#endif
|
#endif
|
|
|
/*
|
/*
|
$log$
|
$log$
|
*/
|
*/
|
|
|