12 #include <mrpt/config.h> 13 #if defined(MRPT_OS_LINUX) 26 #include <semaphore.h> 27 #include <sys/timeb.h> 39 } sem_private_struct, *sem_private;
45 CSemaphore::CSemaphore(
46 unsigned int initialCount,
53 m_data.resize(
sizeof(sem_private_struct) );
54 sem_private token = m_data.getAsPtr<sem_private_struct>();
57 token->has_to_free_mem =
true;
58 token->semid =
static_cast<sem_t*
>( malloc(
sizeof(sem_t)) );
60 if (sem_init(token->semid, 0 , initialCount))
61 token->semid=(sem_t*)SEM_FAILED;
64 if (token->semid==SEM_FAILED)
75 CSemaphore::~CSemaphore()
77 if (m_data.alias_count()==1)
79 sem_private token = m_data.getAsPtr<sem_private_struct>();
82 sem_destroy((sem_t *)token->semid);
84 if (token->has_to_free_mem)
94 bool CSemaphore::waitForSignal(
unsigned int timelimit )
98 sem_private token = m_data.getAsPtr<sem_private_struct>();
107 rc = sem_wait( token->semid );
114 const long sec = timelimit / 1000;
115 const long millisec = timelimit % 1000;
118 tp.millitm += millisec;
119 if( tp.millitm > 999 )
127 tm.tv_nsec = tp.millitm * 1000000 ;
130 while ((rc = sem_timedwait( token->semid, &tm )) == -1 && errno == EINTR)
135 if (rc!=0 && errno!=ETIMEDOUT)
136 std::cerr <<
format(
"[CSemaphore::waitForSignal] In semaphore, error: %s\n", strerror(errno) );
146 void CSemaphore::release(
unsigned int increaseCount )
150 sem_private token = m_data.getAsPtr<sem_private_struct>();
152 for (
unsigned int i=0;i<increaseCount;i++)
153 if (sem_post(token->semid))
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
#define THROW_EXCEPTION(msg)
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
This namespace provides multitask, synchronization utilities.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.