Main MRPT website > C++ reference for MRPT 1.5.6
CSemaphore_WIN.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
12 
13 
14 #include <mrpt/config.h>
15 
16 #ifdef MRPT_OS_WINDOWS
17 
18 #include <windows.h>
19 
20 #include <mrpt/synch/CSemaphore.h>
21 #include <mrpt/utils/CStream.h>
22 
23 using namespace mrpt;
24 using namespace mrpt::synch;
25 
26 /*---------------------------------------------------------------
27  CSemaphore
28 ---------------------------------------------------------------*/
30  unsigned int initialCount,
31  unsigned int maxCount)
32 {
34 
35  HANDLE hSem = CreateSemaphoreA(
36  NULL, // pointer to security attributes
37  initialCount, // initial count
38  maxCount, // maximum count
39  NULL /*name*/ );
40 
41  if (!hSem) THROW_EXCEPTION("Error creating semaphore!");
42 
43  m_data.resize( sizeof(HANDLE) );
44 
45  * m_data.getAsPtr<HANDLE>() = hSem;
46 
47  MRPT_END
48 }
49 
50 /*---------------------------------------------------------------
51  ~CSemaphore
52 ---------------------------------------------------------------*/
54 {
55  if (m_data.alias_count()==1)
56  {
57  CloseHandle( * m_data.getAsPtr<HANDLE>() );
58  }
59 }
60 
61 /*---------------------------------------------------------------
62 Blocks until the count of the semaphore to be non-zero.
63 \param timeout_ms The timeout in milliseconds, or set to zero to wait indefinidely.
64 \return true if the semaphore has been signaled, false on timeout or any other error.
65 ---------------------------------------------------------------*/
66 bool CSemaphore::waitForSignal( unsigned int timeout_ms )
67 {
69 
70  DWORD tim = (timeout_ms==0) ? INFINITE : timeout_ms;
71  DWORD ret = WaitForSingleObject( * m_data.getAsPtr<HANDLE>(), tim );
72 
73  return (ret==WAIT_OBJECT_0);
74 
75  MRPT_END
76 }
77 
78 /*---------------------------------------------------------------
79  Increments the count of the semaphore by a given amount.
80 ---------------------------------------------------------------*/
81 void CSemaphore::release(unsigned int increaseCount )
82 {
84 
85  if (!ReleaseSemaphore(
86  *m_data.getAsPtr<HANDLE>(), // handle of the semaphore object
87  increaseCount, // amount to add to current count
88  NULL )) // address of previous count
89  THROW_EXCEPTION("Error increasing semaphore count!");
90 
91  MRPT_END
92 }
93 
94 
95 #endif // Windows
CSemaphore(unsigned int initialCount, unsigned int maxCount)
Creates a semaphore.
virtual ~CSemaphore()
Dtor.
#define THROW_EXCEPTION(msg)
void resize(size_t mem_block_size)
Resize the shared memory block.
#define MRPT_END
void release(unsigned int increaseCount=1)
Increments the count of the semaphore by a given amount.
This namespace provides multitask, synchronization utilities.
Definition: atomic_incr.h:29
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::utils::CReferencedMemBlock m_data
Definition: CSemaphore.h:31
bool waitForSignal(unsigned int timeout_ms=0)
Blocks until the count of the semaphore to be non-zero.
GLsizei maxCount
Definition: glext.h:3902



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019