blob: 3cab899b70b2010d66f2fc5cd0c698fdd95565a5 [file] [log] [blame]
// umlrtmutex.hh
/*******************************************************************************
* Copyright (c) 2014-2015 Zeligsoft (2009) Limited and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
#ifndef UMLRTMUTEX_HH
#define UMLRTMUTEX_HH
#include <pthread.h>
#include <stdint.h>
// UMLRTMutex is platform-independent mutual-exclusion.
// Currently not os-indepedent.
// Bug 32 tracks move of this implementation to +/os/linux.
class UMLRTMutexGuard
{
public:
UMLRTMutexGuard ( pthread_mutex_t & m_ ) : mutex(m_) { pthread_mutex_lock( &mutex ); }
~ UMLRTMutexGuard ( ) { pthread_mutex_unlock( &mutex ); }
private:
pthread_mutex_t mutex;
};
class UMLRTMutex
{
public:
UMLRTMutex();
// Can create a mutex which starts life as taken already.
UMLRTMutex( bool taken );
~UMLRTMutex();
// Wait forever for it to be available.
void take(void);
// Wait only so many milliseconds.
// Timed - returns true for success, false for timeout.
bool take( uint32_t msec );
// Give it back.
void give(void);
private:
pthread_mutex_t guard;
pthread_cond_t condition;
bool locked;
};
#endif // UMLRTMUTEX_HH