Anklang 0.3.0-460-gc4ef46ba
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;
19extern const char *const ase_sharedir;
20
21// == Translate i18n strings ==
22const char* (_) (const char *string) __attribute__ ((__format_arg__ (1)));
23std::string (_) (const std::string &string);
24const char* (_) (const char *string, const char *plural, int64_t n) __attribute__ ((__format_arg__ (1), __format_arg__ (2)));
25std::string (_) (const std::string &string, const std::string &plural, int64_t n);
26
27// == INSTALLPATH ==
28enum class RPath {
29 PREFIXDIR = 1,
30 INSTALLDIR,
31 LOCALEDIR,
32 LIBDIR,
33 ELECTRONDIR,
34 DEMODIR,
35 LADSPADIRS,
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 /*dtor*/ ~ScopedSemaphore () noexcept;
113};
114
115// == AsyncBlockingQueue ==
117template<class Value>
119 std::mutex mutex_;
121 std::list<Value> list_;
122public:
123 void push (const Value &v);
124 Value pop ();
125 bool pending ();
126 void swap (std::list<Value> &list);
127};
128
129// == Scheduling ==
130int sched_get_priority (int tid);
131bool sched_set_priority (int tid, int nicelevel);
132bool sched_fast_priority (int tid);
133
134// == Thread Status ==
137 enum State { UNKNOWN = '?', RUNNING = 'R', SLEEPING = 'S', DISKWAIT = 'D', STOPPED = 'T', PAGING = 'W', ZOMBIE = 'Z', DEBUG = 'X', };
141 State state;
149 uint64 ac_utime, ac_stime, ac_cutime, ac_cstime;
150 explicit TaskStatus (int pid, int tid = -1);
151 bool update ();
152 String string ();
153};
154
157 static std::thread::id ase_thread_id;
158public:
160 static void add (const std::string &name, int pid,
161 int tid = -1);
162 static bool remove (int tid);
163 static void update ();
164 static List list ();
165 static void setup_ase (const String &name16chars);
166 static bool is_ase () { return std::this_thread::get_id() == ase_thread_id; }
167};
168
169// == Thread Info ==
171ThreadId this_thread_self ();
172void this_thread_set_name (const String &name16chars);
173String this_thread_get_name ();
174int this_thread_getpid ();
175int this_thread_gettid ();
176int this_thread_online_cpus ();
177inline bool this_thread_is_ase () { return TaskRegistry::is_ase(); }
178
179// == Debugging Aids ==
180extern inline void breakpoint () ASE_ALWAYS_INLINE;
181
182// == Memory Barriers ==
183#if defined __x86_64__ || defined __amd64__
184#define ASE_MFENCE __sync_synchronize()
185#define ASE_SFENCE __asm__ __volatile__ ("sfence" ::: "memory")
186#define ASE_LFENCE __asm__ __volatile__ ("lfence" ::: "memory")
187#else // !x86/64
188#define ASE_SFENCE __sync_synchronize()
189#define ASE_LFENCE __sync_synchronize()
191#define ASE_MFENCE __sync_synchronize()
192#endif
194#define ASE_CFENCE __asm__ __volatile__ ("" ::: "memory")
195
196// == Implementation Details ==
197#if (defined __i386__ || defined __x86_64__)
198inline void breakpoint() { __asm__ __volatile__ ("int $03"); }
199#elif defined __alpha__ && !defined __osf__
200inline void breakpoint() { __asm__ __volatile__ ("bpt"); }
201#else // !__i386__ && !__alpha__
202inline void breakpoint() { __builtin_trap(); }
203#endif
204
205template<class Value> void
206AsyncBlockingQueue<Value>::push (const Value &v)
207{
208 std::lock_guard<std::mutex> locker (mutex_);
209 const bool notify = list_.empty();
210 list_.push_back (v);
211 if (ASE_UNLIKELY (notify))
212 cond_.notify_all();
213}
214
215template<class Value> Value
216AsyncBlockingQueue<Value>::pop ()
217{
218 std::unique_lock<std::mutex> locker (mutex_);
219 while (list_.empty())
220 cond_.wait (locker);
221 Value v = list_.front();
222 list_.pop_front();
223 return v;
224}
225
226template<class Value> bool
227AsyncBlockingQueue<Value>::pending()
228{
229 std::lock_guard<std::mutex> locker (mutex_);
230 return !list_.empty();
231}
232
233template<class Value> void
234AsyncBlockingQueue<Value>::swap (std::list<Value> &list)
235{
236 std::lock_guard<std::mutex> locker (mutex_);
237 const bool notify = list_.empty();
238 list_.swap (list);
239 if (notify && !list_.empty())
240 cond_.notify_all();
241}
242
243} // Ase
244
245#endif // __ASE_PLATFORM_HH__
This is a thread-safe asyncronous queue which blocks in pop() until data is provided through push().
Definition platform.hh:118
~Stopwatch()
Stop and print a previous msg if still running.
Definition platform.cc:682
void stop(const String &msg="")
Stop stop watch, print msg.
Definition platform.cc:659
double seconds() const
Provide seconds elapsed between start() and stop().
Definition platform.cc:669
void start(const String &msg="")
Start or restart stop watch, printing msg later on.
Definition platform.cc:650
double milliseconds() const
Provide milliseconds elapsed between start() and stop().
Definition platform.cc:676
The task registry keeps track of runtime threads for profiling and statistical purposes.
Definition platform.hh:156
static void add(const std::string &name, int pid, int tid=-1)
Add process/thread to registry for runtime profiling.
Definition platform.cc:1064
static void update()
Issue TaskStatus.update on all tasks in registry.
Definition platform.cc:1087
static bool remove(int tid)
Remove process/thread based on thread_id.
Definition platform.cc:1074
static List list()
Retrieve a copy to the list of all tasks in registry.
Definition platform.cc:1095
#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:190
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:154
bool colorize_tty(int fd)
Check whether the tty fd should use colorization, checks ASE_COLOR if fd == -1.
Definition platform.cc:161
const char * color_code(Colors acolor)
Return ANSI code for the specified color.
Definition platform.cc:205
The Anklang C++ API namespace.
Definition api.hh:9
uint64 timestamp_resolution()
Provide resolution of timestamp_benchmark() in nano-seconds.
Definition platform.cc:593
uint64_t uint64
A 64-bit unsigned integer, use PRI*64 in format strings.
Definition cxxaux.hh:25
String cpu_info()
Retrieve string identifying the runtime CPU type.
Definition platform.cc:480
uint64 monotonic_counter()
A monotonically increasing counter, increments are atomic and visible in all threads.
Definition platform.cc:635
String application_name()
Retrieve the localized program name intended for user display.
Definition platform.cc:864
void breakpoint()
Cause a debugging breakpoint, for development only.
Definition platform.hh:202
uint64 timestamp_benchmark()
Returns benchmark timestamp in nano-seconds, clock starts around program startup.
Definition platform.cc:601
std::string executable_path()
Retrieve the path to the currently running executable.
Definition platform.cc:734
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:855
void application_name_init(String desktopname)
Set the application_name to a name other than program_alias if desired.
Definition platform.cc:870
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:931
String program_cwd()
The current working directory during startup.
Definition platform.cc:877
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:467
bool sched_fast_priority(int tid)
Try to acquire low latency scheduling priority, returns true if nice level is < 0.
Definition platform.cc:939
String program_alias()
Retrieve the program name as used for logging or debug messages.
Definition platform.cc:849
const char * ase_version()
Provide a string containing the package version.
Definition platform.cc:835
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:920
uint64 timestamp_startup()
Provides the timestamp_realtime() value from program startup.
Definition platform.cc:570
uint64 timestamp_realtime()
Return the current time as uint64 in µseconds.
Definition platform.cc:578
String timestamp_format(uint64 stamp, uint maxlength)
Convert stamp into a string, adding µsecond fractions if space permits.
Definition platform.cc:620
const char * ase_build_id()
Provide a string containing the ASE library build id.
Definition platform.cc:841
typedef int64_t
Acquire information about a task (process or thread) at runtime.
Definition platform.hh:136
State state
Thread state.
Definition platform.hh:141
int priority
Priority or nice value.
Definition platform.hh:143
String string()
Retrieve string representation of the status information.
Definition platform.cc:1051
uint64 utime
Userspace time.
Definition platform.hh:144
uint64 ac_stamp
Accounting stamp.
Definition platform.hh:148
uint64 cutime
Userspace time of dead children.
Definition platform.hh:146
int processor
Rrunning processor number.
Definition platform.hh:142
uint64 cstime
System time of dead children.
Definition platform.hh:147
bool update()
Update status information, might return false if called too frequently.
Definition platform.cc:1033
int process_id
Process ID.
Definition platform.hh:138
int task_id
Process ID or thread ID.
Definition platform.hh:139
uint64 stime
System time.
Definition platform.hh:145
String name
Thread name (set by user).
Definition platform.hh:140
Value type used to interface with various property types.
Definition value.hh:54
wait