Ardour  9.0-pre0-582-g084a23a80d
pthread_utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2000-2015 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2013-2014 John Emmas <john@creativepost.co.uk>
4  * Copyright (C) 2017-2018 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_pthread_utils__
22 #define __pbd_pthread_utils__
23 
24 /* Accommodate thread setting (and testing) for both
25  * 'libpthread' and 'libpthread_win32' (whose implementations
26  * of 'pthread_t' are subtlely different)
27  */
28 #ifndef PTHREAD_MACROS_DEFINED
29 #define PTHREAD_MACROS_DEFINED
30 #ifdef PTW32_VERSION /* pthread_win32 */
31 #define mark_pthread_inactive(threadID) threadID.p=0
32 #define is_pthread_active(threadID) threadID.p!=0
33 #else /* normal pthread */
34 #define mark_pthread_inactive(threadID) threadID=0
35 #define is_pthread_active(threadID) threadID!=0
36 #endif /* PTW32_VERSION */
37 #endif /* PTHREAD_MACROS_DEFINED */
38 
39 #ifdef COMPILER_MSVC
40 #include <ardourext/pthread.h>
41 #else
42 #include <pthread.h>
43 #endif
44 #include <signal.h>
45 #include <string>
46 #include <stdint.h>
47 
48 #include "pbd/libpbd_visibility.h"
49 #include "pbd/signals.h"
50 
51 #define PBD_RT_STACKSIZE_PROC 0x80000 // 512kB
52 #define PBD_RT_STACKSIZE_HELP 0x08000 // 32kB
53 
54 /* these are relative to sched_get_priority_max()
55  * see pbd_absolute_rt_priority()
56  */
57 # define PBD_RT_PRI_MAIN pbd_pthread_priority (THREAD_MAIN)
58 # define PBD_RT_PRI_MIDI pbd_pthread_priority (THREAD_MIDI)
59 # define PBD_RT_PRI_PROC pbd_pthread_priority (THREAD_PROC)
60 # define PBD_RT_PRI_CTRL pbd_pthread_priority (THREAD_CTRL)
61 # define PBD_RT_PRI_IOFX pbd_pthread_priority (THREAD_IOFX)
62 
63 LIBPBD_API int pthread_create_and_store (std::string name, pthread_t *thread, void * (*start_routine)(void *), void * arg, uint32_t stacklimit = 0x80000 /*512kB*/);
64 LIBPBD_API void pthread_cancel_one (pthread_t thread);
66 LIBPBD_API void pthread_kill_all (int signum);
67 LIBPBD_API const char* pthread_name ();
68 LIBPBD_API void pthread_set_name (const char* name);
69 
71 
73  THREAD_MAIN, // main audio I/O thread
74  THREAD_MIDI, // MIDI I/O threads
75  THREAD_PROC, // realtime worker
76  THREAD_CTRL, // Automation watch, BaseUI
77  THREAD_IOFX // non-realtime I/O and regionFX
78 };
79 
81 
83  const size_t stacksize,
84  pthread_t *thread,
85  void *(*start_routine) (void *),
86  void *arg);
87 
88 
90  std::string const& debug_name,
91  const int policy, int priority, const size_t stacksize,
92  pthread_t *thread,
93  void *(*start_routine) (void *),
94  void *arg);
95 
96 LIBPBD_API int pbd_absolute_rt_priority (int policy, int priority);
97 LIBPBD_API int pbd_set_thread_priority (pthread_t, int policy, int priority);
98 LIBPBD_API bool pbd_mach_set_realtime_policy (pthread_t thread_id, double period_ns, bool main);
99 
100 namespace PBD {
101  LIBPBD_API extern void notify_event_loops_about_thread_creation (pthread_t, const std::string&, int requests = 256);
102  LIBPBD_API extern PBD::Signal<void(pthread_t,std::string,uint32_t)> ThreadCreatedWithRequestSize;
103 
105  public:
106  static Thread* create (std::function<void ()> const&, std::string const& name);
107  static Thread* self ();
108  void join ();
109  bool caller_is_self () const;
110 
111  private:
112  Thread ();
113  Thread (std::function<void ()> const&, std::string const& name = "");
114  Thread (Thread const&); /* precent copy-construction */
115 
116  static void* _run (void*);
117 
118  pthread_t _t;
119  std::string _name;
120  std::function<void ()> _slot;
121  bool _joinable;
122  };
123 }
124 
125 /* pthread-w32 does not support realtime scheduling
126  * (well, windows, doesn't..) and only supports SetThreadPriority()
127  *
128  * pthread_setschedparam() returns ENOTSUP if the policy is not SCHED_OTHER.
129  *
130  * however, pthread_create() with attributes, ignores the policy and
131  * only sets the priority (when PTHREAD_EXPLICIT_SCHED is used).
132  */
133 #ifdef PLATFORM_WINDOWS
134 #define PBD_SCHED_FIFO SCHED_OTHER
135 #else
136 #define PBD_SCHED_FIFO SCHED_FIFO
137 #endif
138 #endif /* __pbd_pthread_utils__ */
static Thread * create(std::function< void()> const &, std::string const &name)
bool caller_is_self() const
static void * _run(void *)
std::string _name
Thread(Thread const &)
Thread(std::function< void()> const &, std::string const &name="")
pthread_t _t
std::function< void()> _slot
GtkImageIconNameData name
Definition: gtkimage.h:6
#define LIBPBD_API
Definition: axis_view.h:42
void notify_event_loops_about_thread_creation(pthread_t, const std::string &, int requests=256)
PBD::Signal< void(pthread_t, std::string, uint32_t)> ThreadCreatedWithRequestSize
int pbd_realtime_pthread_create(std::string const &debug_name, const int policy, int priority, const size_t stacksize, pthread_t *thread, void *(*start_routine)(void *), void *arg)
void pthread_kill_all(int signum)
void pbd_set_engine_rt_priority(int)
PBDThreadClass
Definition: pthread_utils.h:72
@ THREAD_MIDI
Definition: pthread_utils.h:74
@ THREAD_MAIN
Definition: pthread_utils.h:73
@ THREAD_CTRL
Definition: pthread_utils.h:76
@ THREAD_PROC
Definition: pthread_utils.h:75
@ THREAD_IOFX
Definition: pthread_utils.h:77
bool pbd_mach_set_realtime_policy(pthread_t thread_id, double period_ns, bool main)
void pthread_cancel_one(pthread_t thread)
int pbd_pthread_create(const size_t stacksize, pthread_t *thread, void *(*start_routine)(void *), void *arg)
int pbd_pthread_priority(PBDThreadClass)
const char * pthread_name()
int pthread_create_and_store(std::string name, pthread_t *thread, void *(*start_routine)(void *), void *arg, uint32_t stacklimit=0x80000)
int pbd_set_thread_priority(pthread_t, int policy, int priority)
int pbd_absolute_rt_priority(int policy, int priority)
void pthread_cancel_all()
void pthread_set_name(const char *name)