Ardour  8.12
semutils.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010-2013 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2013 John Emmas <john@creativepost.co.uk>
4  * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #ifndef __pbd_semutils_h__
22 #define __pbd_semutils_h__
23 
24 #if (defined PLATFORM_WINDOWS && !defined USE_PTW32_SEMAPHORE)
25 #define WINDOWS_SEMAPHORE 1
26 #endif
27 
28 #ifdef WINDOWS_SEMAPHORE
29 # include <windows.h>
30 #elif defined USE_FUTEX_SEMAPHORE
31 # include <atomic>
32 #else
33 # include <pthread.h>
34 # include <semaphore.h>
35 #endif
36 
37 #include "pbd/libpbd_visibility.h"
38 
39 namespace PBD {
40 
42  private:
43 #ifdef WINDOWS_SEMAPHORE
44  HANDLE _sem;
45 
46 #elif __APPLE__
47  sem_t* _sem;
48  sem_t* ptr_to_sem() const { return _sem; }
49 #elif defined USE_FUTEX_SEMAPHORE
50  int _futex;
51  std::atomic<int> _value;
52 #else
53  mutable sem_t _sem;
54  sem_t* ptr_to_sem() const { return &_sem; }
55 #endif
56 
57  public:
58  Semaphore (const char* name, int val);
60 
61 #if defined WINDOWS_SEMAPHORE || defined USE_FUTEX_SEMAPHORE
62 
63  int signal ();
64  int wait ();
65  int reset ();
66 
67 #else
68  int signal () { return sem_post (ptr_to_sem()); }
69  int wait () { return sem_wait (ptr_to_sem()); }
70  int reset () { int rv = 0 ; while (sem_trywait (ptr_to_sem()) == 0) ++rv; return rv; }
71 #endif
72 };
73 
74 }
75 
76 #endif /* __pbd_semutils_h__ */
sem_t * ptr_to_sem() const
Definition: semutils.h:54
sem_t _sem
Definition: semutils.h:53
Semaphore(const char *name, int val)
int reset()
Definition: semutils.h:70
int signal()
Definition: semutils.h:68
int wait()
Definition: semutils.h:69
GtkImageIconNameData name
Definition: gtkimage.h:6
#define LIBPBD_API
Definition: axis_view.h:42