Program Listing for File semaphore_pos.hpp

Return to documentation for file (/tmp/ws/src/ecl_core/ecl_ipc/include/ecl/ipc/semaphore_pos.hpp)

/*****************************************************************************
** Ifdefs
*****************************************************************************/

#ifndef ECL_IPC_SEMAPHORE_POS_HPP_
#define ECL_IPC_SEMAPHORE_POS_HPP_

/*****************************************************************************
** Platform Check
*****************************************************************************/

#include <ecl/config.hpp>
#if defined(ECL_IS_POSIX)
#include <unistd.h>
#ifdef _POSIX_SEMAPHORES
#if _POSIX_SEMAPHORES > 0

/*****************************************************************************
** Ecl Functionality Defines
*****************************************************************************/

#ifndef ECL_HAS_POSIX_SEMAPHORES
  #define ECL_HAS_POSIX_SEMAPHORES
#endif
#ifndef ECL_HAS_SEMAPHORES
  #define ECL_HAS_SEMAPHORES
#endif

/*****************************************************************************
** Includes
*****************************************************************************/

#include <iostream>
#include <string>
#include <semaphore.h>
#include <fcntl.h>           /* For O_* constants */
#include <ecl/exceptions/standard_exception.hpp>
#include <ecl/time/duration.hpp>

/*****************************************************************************
** Namespaces
*****************************************************************************/

namespace ecl {

/*****************************************************************************
** Forward Declarations
*****************************************************************************/

class Semaphore;

#ifdef ECL_HAS_EXCEPTIONS

namespace ipc {

/*****************************************************************************
** Semaphore Exceptions
*****************************************************************************/
ECL_LOCAL ecl::StandardException openSemaphoreException(const char* loc );
ECL_LOCAL ecl::StandardException tryLockSemaphoreException(const char* loc);

} // namespace ipc

#endif /* ECL_HAS_EXCEPTIONS */

/*****************************************************************************
** Semaphores
*****************************************************************************/
class ECL_PUBLIC Semaphore
{
public:
    /*********************
    ** C&D's
    **********************/
    Semaphore();
    Semaphore(const std::string& string_id);
    virtual ~Semaphore();

    /*********************
    ** Locking
    **********************/
    void lock();
    void unlock();
    bool trylock();
    bool trylock( const Duration &timeout );

private:
    std::string name;
    sem_t* semaphore;

    int count();
};


} // namespace ecl


#endif /* _POSIX_SEMAPHORES > 0 */
#endif /* _POSIX_SEMAPHORES */
#endif /* ECL_IS_POSIX */

#endif /* ECL_IPC_SEMAPHORE_POS_HPP_ */