-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemaphore.h
69 lines (56 loc) · 2.14 KB
/
Semaphore.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// *****************************************************************************
//
// Semaphores implementation using C++11 concurrency features.
// (public interface declarations)
// Copyright (C) 2017 Carlos Ureña Almagro
//
// April 2017: created
// 15-Sept-2017: removed reference count, now 'std::shared_ptr' is used instead
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// *****************************************************************************
#ifndef SEMAPHORES_HPP
#define SEMAPHORES_HPP
#include <memory> // (for std::shared_ptr)
#include <mutex>
#include <condition_variable>
#include <cassert>
namespace SEM
{
// incomplete declaration of the implementation class
class SemaphoreRepr ;
// *****************************************************************************
// Classic semaphore objects
// it is a reference to a Semaphore implementation object
class Semaphore
{
public:
// initialization
Semaphore( unsigned init_value );
// dissallow asignements
void operator = (Semaphore & sem ) = delete ;
// allows to initialize a semaphore by using another (a new alias is created)
Semaphore( const Semaphore & sem ) ;
// operations (member methods)
void sem_wait();
void sem_signal() ;
// operations (non member functions)
friend inline void sem_wait ( Semaphore & s ) { s.sem_wait() ; }
friend inline void sem_signal( Semaphore & s ) { s.sem_signal(); }
private:
std::shared_ptr<SemaphoreRepr> repr; // pointer to semaphore implementation
} ;
} // end namespace SCD
#endif // ifndef SEMAPHORES_HPP