Ardour  9.0-pre0-350-gf17a656217
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 #pragma once
22 
23 #if (defined PLATFORM_WINDOWS && !defined USE_PTW32_SEMAPHORE)
24 #define WINDOWS_SEMAPHORE 1
25 #endif
26 
27 #ifdef WINDOWS_SEMAPHORE
28 # include <windows.h>
29 #elif defined USE_FUTEX_SEMAPHORE
30 # include <atomic>
31 #else
32 # include <pthread.h>
33 # include <semaphore.h>
34 #endif
35 
36 #include "pbd/libpbd_visibility.h"
37 
38 namespace PBD {
39 
41  private:
42 #ifdef WINDOWS_SEMAPHORE
43  HANDLE _sem;
44 
45 #elif __APPLE__
46  sem_t* _sem;
47  sem_t* ptr_to_sem() const { return _sem; }
48 #elif defined USE_FUTEX_SEMAPHORE
49  int _futex;
50  std::atomic<int> _value;
51 #else
52  mutable sem_t _sem;
53  sem_t* ptr_to_sem() const { return &_sem; }
54 #endif
55 
56  public:
57  Semaphore (const char* name, int val);
59 
60 #if defined WINDOWS_SEMAPHORE || defined USE_FUTEX_SEMAPHORE
61 
62  int signal ();
63  int wait ();
64  int reset ();
65 
66 #else
67  int signal () { return sem_post (ptr_to_sem()); }
68  int wait () { return sem_wait (ptr_to_sem()); }
69  int reset () { int rv = 0 ; while (sem_trywait (ptr_to_sem()) == 0) ++rv; return rv; }
70 #endif
71 };
72 
73 }
74 
sem_t * ptr_to_sem() const
Definition: semutils.h:53
sem_t _sem
Definition: semutils.h:52
Semaphore(const char *name, int val)
int reset()
Definition: semutils.h:69
int signal()
Definition: semutils.h:67
int wait()
Definition: semutils.h:68
GtkImageIconNameData name
Definition: gtkimage.h:6
#define LIBPBD_API
Definition: axis_view.h:42