Main MRPT website > C++ reference for MRPT 1.5.5
CSemaphore_APP.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" // Only for precomp. headers, include all libmrpt-core headers.
11 
12 
13 #include <mrpt/config.h>
14 #if defined(MRPT_OS_APPLE)
15 
16 #include <mrpt/synch/CSemaphore.h>
18 #include <mrpt/system/threads.h>
19 
20 #include <mach/mach_init.h>
21 #include <mach/task.h>
22 #include <mach/semaphore.h>
23 
24 #include <iostream>
25 #include <pthread.h>
26 #include <errno.h>
27 #include <fcntl.h> // O_CREAT
28 #include <semaphore.h>
29 #include <sys/timeb.h>
30 
31 using namespace mrpt;
32 using namespace mrpt::utils;
33 using namespace mrpt::synch;
34 
35 typedef struct
36 {
37  sem_t * semid;
38 } sem_private_struct, *sem_private;
39 
40 // ######################################################################
41 CSemaphore::CSemaphore(
42  unsigned int initialCount,
43  unsigned int maxCount)
44 {
46 
47  m_data.resize(sizeof(semaphore_t));
48  semaphore_t * sem = m_data.getAsPtr<semaphore_t>();
49 
50  kern_return_t error = semaphore_create(mach_task_self(), sem, SYNC_POLICY_FIFO, initialCount);
51  if(error != KERN_SUCCESS)
52  THROW_EXCEPTION( format("Creating semaphore raised error: %d", error ) );
53 
54  MRPT_END
55 }
56 
57 // ######################################################################
58 CSemaphore::~CSemaphore()
59 {
61 
62  if (m_data.alias_count()==1)
63  {
64  semaphore_t * sem = m_data.getAsPtr<semaphore_t>();
65  semaphore_destroy(mach_task_self(), *sem);
66  }
67 
68  MRPT_END
69 }
70 
71 // ######################################################################
72 bool CSemaphore::waitForSignal( unsigned int timelimit )
73 {
75 
76  semaphore_t * sem = m_data.getAsPtr<semaphore_t>();
77 
78  if(timelimit == 0)
79  {
80  kern_return_t result = semaphore_wait(*sem);
81 
82  return (result == KERN_SUCCESS);
83  }
84  else
85  {
86  mach_timespec_t ts;
87  ts.tv_sec = timelimit / 1000;
88  ts.tv_nsec = (timelimit % 1000) * 1000000;
89 
90  kern_return_t result = semaphore_timedwait(*sem, ts);
91 
92  return (result == KERN_SUCCESS);
93  }
94 
95  MRPT_END
96 }
97 
98 // ######################################################################
99 void CSemaphore::release(unsigned int increaseCount )
100 {
101  MRPT_START
102 
103  semaphore_t * sem = m_data.getAsPtr<semaphore_t>();
104  for(unsigned int i=0; i<increaseCount; ++i)
105  {
106  kern_return_t result = semaphore_signal(*sem);
107  if(result != KERN_SUCCESS)
108  THROW_EXCEPTION( format("Increasing count of semaphore raised error: %d", result ) );
109  }
110 
111  MRPT_END
112 }
113 
114 #endif // defined(MRPT_OS_APPLE)
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
#define THROW_EXCEPTION(msg)
#define MRPT_END
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:21
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.
GLsizei maxCount
Definition: glext.h:3902



Page generated by Doxygen 1.8.14 for MRPT 1.5.5 Git: e06b63dbf Fri Dec 1 14:41:11 2017 +0100 at lun oct 28 01:31:35 CET 2019