Anklang-0.3.0.dev712+gdc4e642f anklang-0.3.0.dev712+gdc4e642f
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
platform.hh
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#ifndef __ASE_PLATFORM_HH__
3#define __ASE_PLATFORM_HH__
4
5#include <ase/defs.hh>
6#include <condition_variable>
7#include <thread>
8#include <list>
9
10namespace Ase {
11
12// == Build Constants ==
13extern const int ase_major_version;
14extern const int ase_minor_version;
15extern const int ase_micro_version;
16extern const char *const ase_version_long;
17extern const char *const ase_version_short;
18extern const char *const ase_gettext_domain;
19
20// == Translate i18n strings ==
21const char* (_) (const char *string) __attribute__ ((__format_arg__ (1)));
22std::string (_) (const std::string &string);
23const char* (_) (const char *string, const char *plural, int64_t n) __attribute__ ((__format_arg__ (1), __format_arg__ (2)));
24std::string (_) (const std::string &string, const std::string &plural, int64_t n);
25
26// == INSTALLPATH ==
27enum class RPath {
28 PREFIXDIR = 1,
29 INSTALLDIR,
30 LOCALEDIR,
31 LIBDIR,
32 ELECTRONDIR,
33 LADSPADIRS,
34 SAMPLEDIR,
35 DEMODIR,
36};
37std::string anklang_runpath (RPath rpath, const String &segment = "");
38String anklang_home_dir (const String &subdir = "");
39
40// == AnsiColors ==
42namespace AnsiColors {
44enum Colors {
45 NONE,
47 BOLD, BOLD_OFF,
48 ITALICS, ITALICS_OFF,
49 UNDERLINE, UNDERLINE_OFF,
50 INVERSE, INVERSE_OFF,
51 STRIKETHROUGH, STRIKETHROUGH_OFF,
52 FG_BLACK, FG_RED, FG_GREEN, FG_YELLOW, FG_BLUE, FG_MAGENTA, FG_CYAN, FG_WHITE,
53 FG_DEFAULT,
54 BG_BLACK, BG_RED, BG_GREEN, BG_YELLOW, BG_BLUE, BG_MAGENTA, BG_CYAN, BG_WHITE,
55 BG_DEFAULT,
56};
57enum class Colorize : int8 { NEVER = 0, ALWAYS = 1, AUTO = 2 };
58const char* color_code (Colors acolor);
59std::string color (Colors acolor, Colors c1 = Colors::NONE, Colors c2 = Colors::NONE, Colors c3 = Colors::NONE,
60 Colors c4 = Colors::NONE, Colors c5 = Colors::NONE, Colors c6 = Colors::NONE);
61void configure (Colorize colorize);
62bool colorize_tty (int fd = -1);
63} // AnsiColors
64
65// == Timestamp Handling ==
66uint64 timestamp_startup (); // µseconds
67uint64 timestamp_realtime (); // µseconds
68uint64 timestamp_benchmark (); // nseconds
69uint64 timestamp_resolution (); // nseconds
70String timestamp_format (uint64 stamp, uint maxlength = 8);
72
73// == Stopwatch ==
74class Stopwatch {
75 uint64 start_ = 0, end_ = 0; String msg_;
76public:
77 explicit Stopwatch (const String &msg = "");
78 void start (const String &msg = "");
79 void stop (const String &msg = "");
80 double seconds () const;
81 double milliseconds () const;
82 /*dtor*/ ~Stopwatch ();
83};
84
85// == process names ==
87void program_alias_init (String customname);
89void application_name_init (String desktopname);
91std::string executable_name () ASE_PURE;
92std::string executable_path () ASE_PURE;
93std::string cpu_info ();
94std::string cpu_arch ();
95const char* ase_version ();
96const char* ase_build_id ();
97
98// == user ==
99int user_id ();
100String user_name ();
101String user_real_name ();
102
103// == ScopedSemaphore ==
105 unsigned long mem_[4];
106 /*copy*/ ScopedSemaphore (const ScopedSemaphore&) = delete;
107 ScopedSemaphore& operator= (const ScopedSemaphore&) = delete;
108public:
109 explicit ScopedSemaphore () noexcept;
110 int post () noexcept;
111 int wait () noexcept;
112 int wait_for (uint64_t useconds) noexcept;
113 /*dtor*/ ~ScopedSemaphore () noexcept;
114};
115
116// == AsyncBlockingQueue ==
118template<class Value>
120 std::mutex mutex_;
122 std::list<Value> list_;
123public:
124 void push (const Value &v);
125 Value pop ();
126 bool pending ();
127 void swap (std::list<Value> &list);
128};
129
130// == Scheduling ==
131int sched_get_priority (int tid);
132bool sched_set_priority (int tid, int nicelevel);
133bool sched_fast_priority (int tid);
134
135// == Thread Status ==
138 enum State { UNKNOWN = '?', RUNNING = 'R', SLEEPING = 'S', DISKWAIT = 'D', STOPPED = 'T', PAGING = 'W', ZOMBIE = 'Z', DEBUG = 'X', };
142 State state;
150 uint64 ac_utime, ac_stime, ac_cutime, ac_cstime;
151 explicit TaskStatus (int pid, int tid = -1);
152 bool update ();
153 String string ();
154};
155
158 static std::thread::id ase_thread_id, main_thread_id;
159public:
161 static void add (const std::string &name, int pid,
162 int tid = -1);
163 static bool remove (int tid);
164 static void update ();
165 static List list ();
166 static void setup_ase (const String &name16chars);
167 static bool is_ase () { return std::this_thread::get_id() == ase_thread_id; }
168 static bool is_main () { return std::this_thread::get_id() == main_thread_id; }
169};
170
171// == Thread Info ==
173ThreadId this_thread_self ();
174void this_thread_set_name (const String &name16chars);
175String this_thread_get_name ();
176int this_thread_getpid ();
177int this_thread_gettid ();
178int this_thread_online_cpus ();
179inline bool this_thread_is_ase () { return TaskRegistry::is_ase(); }
180inline bool this_thread_is_main () { return TaskRegistry::is_ase(); }
181
182// == Debugging Aids ==
183extern inline void breakpoint () ASE_ALWAYS_INLINE;
184
185// == Memory Barriers ==
186#if defined __x86_64__ || defined __amd64__
187#define ASE_MFENCE __sync_synchronize()
188#define ASE_SFENCE __asm__ __volatile__ ("sfence" ::: "memory")
189#define ASE_LFENCE __asm__ __volatile__ ("lfence" ::: "memory")
190#else // !x86/64
191#define ASE_SFENCE __sync_synchronize()
192#define ASE_LFENCE __sync_synchronize()
194#define ASE_MFENCE __sync_synchronize()
195#endif
197#define ASE_CFENCE __asm__ __volatile__ ("" ::: "memory")
198
199// == Implementation Details ==
200#if (defined __i386__ || defined __x86_64__)
201inline void breakpoint() { __asm__ __volatile__ ("int $03"); }
202#elif defined __alpha__ && !defined __osf__
203inline void breakpoint() { __asm__ __volatile__ ("bpt"); }
204#else // !__i386__ && !__alpha__
205inline void breakpoint() { __builtin_trap(); }
206#endif
207
208template<class Value> void
209AsyncBlockingQueue<Value>::push (const Value &v)
210{
211 std::lock_guard<std::mutex> locker (mutex_);
212 const bool notify = list_.empty();
213 list_.push_back (v);
214 if (ASE_UNLIKELY (notify))
215 cond_.notify_all();
216}
217
218template<class Value> Value
219AsyncBlockingQueue<Value>::pop ()
220{
221 std::unique_lock<std::mutex> locker (mutex_);
222 while (list_.empty())
223 cond_.wait (locker);
224 Value v = list_.front();
225 list_.pop_front();
226 return v;
227}
228
229template<class Value> bool
230AsyncBlockingQueue<Value>::pending()
231{
232 std::lock_guard<std::mutex> locker (mutex_);
233 return !list_.empty();
234}
235
236template<class Value> void
237AsyncBlockingQueue<Value>::swap (std::list<Value> &list)
238{
239 std::lock_guard<std::mutex> locker (mutex_);
240 const bool notify = list_.empty();
241 list_.swap (list);
242 if (notify && !list_.empty())
243 cond_.notify_all();
244}
245
246} // Ase
247
248#endif // __ASE_PLATFORM_HH__
This is a thread-safe asyncronous queue which blocks in pop() until data is provided through push().
Definition platform.hh:119
~Stopwatch()
Stop and print a previous msg if still running.
Definition platform.cc:683
void stop(const String &msg="")
Stop stop watch, print msg.
Definition platform.cc:660
double seconds() const
Provide seconds elapsed between start() and stop().
Definition platform.cc:670
void start(const String &msg="")
Start or restart stop watch, printing msg later on.
Definition platform.cc:651
double milliseconds() const
Provide milliseconds elapsed between start() and stop().
Definition platform.cc:677
The task registry keeps track of runtime threads for profiling and statistical purposes.
Definition platform.hh:157
static void add(const std::string &name, int pid, int tid=-1)
Add process/thread to registry for runtime profiling.
Definition platform.cc:1054
static void update()
Issue TaskStatus.update on all tasks in registry.
Definition platform.cc:1077
static bool remove(int tid)
Remove process/thread based on thread_id.
Definition platform.cc:1064
static List list()
Retrieve a copy to the list of all tasks in registry.
Definition platform.cc:1085
#define ASE_UNLIKELY(expr)
Compiler hint to optimize for expr evaluating to false.
Definition cxxaux.hh:46
T get_id(T... args)
#define _(...)
Retrieve the translation of a C or C++ string.
Definition internal.hh:18
std::string color(Colors acolor, Colors c1, Colors c2, Colors c3, Colors c4, Colors c5, Colors c6)
Return ANSI code for the specified color if stdout & stderr should be colorized, see colorize_tty().
Definition platform.cc:191
Colors
ANSI color symbols.
Definition platform.hh:44
@ RESET
Reset combines BOLD_OFF, ITALICS_OFF, UNDERLINE_OFF, INVERSE_OFF, STRIKETHROUGH_OFF.
Definition platform.hh:46
void configure(Colorize colorize)
Override the environment variable $ASE_COLOR (which may contain "always", "never" or "auto").
Definition platform.cc:155
bool colorize_tty(int fd)
Check whether the tty fd should use colorization, checks ASE_COLOR if fd == -1.
Definition platform.cc:162
const char * color_code(Colors acolor)
Return ANSI code for the specified color.
Definition platform.cc:206
The Anklang C++ API namespace.
Definition api.hh:9
uint64 timestamp_resolution()
Provide resolution of timestamp_benchmark() in nano-seconds.
Definition platform.cc:594
uint64_t uint64
A 64-bit unsigned integer, use PRI*64 in format strings.
Definition cxxaux.hh:25
String cpu_info()
The returned string contains: number of online CPUs, a string describing the CPU architecture,...
Definition platform.cc:481
uint64 monotonic_counter()
A monotonically increasing counter, increments are atomic and visible in all threads.
Definition platform.cc:636
String application_name()
Retrieve the localized program name intended for user display.
Definition platform.cc:832
void breakpoint()
Cause a debugging breakpoint, for development only.
Definition platform.hh:205
uint64 timestamp_benchmark()
Returns benchmark timestamp in nano-seconds, clock starts around program startup.
Definition platform.cc:602
std::string executable_path()
Retrieve the path to the currently running executable.
Definition platform.cc:735
int8_t int8
An 8-bit signed integer.
Definition cxxaux.hh:26
void program_alias_init(String customname)
Set program_alias to a non-localized alias other than program_argv0 if desired.
Definition platform.cc:823
void application_name_init(String desktopname)
Set the application_name to a name other than program_alias if desired.
Definition platform.cc:838
std::string anklang_runpath(RPath rpath, const String &segment)
Retrieve various resource paths at runtime.
Definition platform.cc:58
bool sched_set_priority(int tid, int nicelevel)
Try to set the nice level of process or thread tid to nicelevel.
Definition platform.cc:921
String program_cwd()
The current working directory during startup.
Definition platform.cc:845
String anklang_home_dir(const String &subdir)
Get Anklang home dir, possibly adding subdir.
Definition platform.cc:46
std::string cpu_arch()
Retrieve string identifying the CPU architecture.
Definition platform.cc:468
bool sched_fast_priority(int tid)
Try to acquire low latency scheduling priority, returns true if nice level is < 0.
Definition platform.cc:929
String program_alias()
Retrieve the program name as used for logging or debug messages.
Definition platform.cc:817
const char * ase_version()
Provide a string containing the package version.
Definition platform.cc:803
std::string String
Convenience alias for std::string.
Definition cxxaux.hh:35
std::string executable_name()
Retrieve the name part of executable_path().
Definition platform.cc:742
uint32_t uint
Provide 'uint' as convenience type.
Definition cxxaux.hh:18
int sched_get_priority(int tid)
Retrieve the nice level of process or thread tid.
Definition platform.cc:910
uint64 timestamp_startup()
Provides the timestamp_realtime() value from program startup.
Definition platform.cc:571
uint64 timestamp_realtime()
Return the current time as uint64 in µseconds.
Definition platform.cc:579
String timestamp_format(uint64 stamp, uint maxlength)
Convert stamp into a string, adding µsecond fractions if space permits.
Definition platform.cc:621
const char * ase_build_id()
Provide a string containing the ASE library build id.
Definition platform.cc:809
typedef int64_t
Acquire information about a task (process or thread) at runtime.
Definition platform.hh:137
State state
Thread state.
Definition platform.hh:142
int priority
Priority or nice value.
Definition platform.hh:144
String string()
Retrieve string representation of the status information.
Definition platform.cc:1041
uint64 utime
Userspace time.
Definition platform.hh:145
uint64 ac_stamp
Accounting stamp.
Definition platform.hh:149
uint64 cutime
Userspace time of dead children.
Definition platform.hh:147
int processor
Rrunning processor number.
Definition platform.hh:143
uint64 cstime
System time of dead children.
Definition platform.hh:148
bool update()
Update status information, might return false if called too frequently.
Definition platform.cc:1023
int process_id
Process ID.
Definition platform.hh:139
int task_id
Process ID or thread ID.
Definition platform.hh:140
uint64 stime
System time.
Definition platform.hh:146
String name
Thread name (set by user).
Definition platform.hh:141
Value type used to interface with various property types.
Definition value.hh:54
wait