RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sem.cpp
Go to the documentation of this file.
1 /*
2  The Real-Time eXperiment Interface (RTXI)
3  Copyright (C) 2011 Georgia Institute of Technology, University of Utah, Weill Cornell Medical College
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 
20 #include <rt.h>
21 #include <sem.h>
22 #include <unistd.h>
23 
24 Semaphore::Semaphore(size_t n):count(n) {}
25 
27 
28 void Semaphore::down(void)
29 {
30 #ifdef DEBUG
31  if (RT::OS::isRealtime())
32  {
33  ERROR_MSG("Detected unsafe down attempt in RT thread\n");
34  PRINT_BACKTRACE();
35  return;
36  }
37 #endif // DEBUG
38 
39  --count;
40  while (count < 0) usleep(1000);
41 }
42 
43 void Semaphore::up(void)
44 {
45  ++count;
46 }
47 
49 {
50  return count;
51 }
void down(void)
Definition: sem.cpp:28
void up(void)
Definition: sem.cpp:43
bool isRealtime(void)
int value(void)
Definition: sem.cpp:48
#define ERROR_MSG(fmt, args...)
Definition: debug.h:41
Semaphore(size_t n=0)
Definition: sem.cpp:24
~Semaphore(void)
Definition: sem.cpp:26
int count
Definition: sem.h:39