helpers.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2011 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #ifdef _WIN32
  21. #ifdef __MINGW32__
  22. #define _WIN32_IE 0x501
  23. #else
  24. #define _WIN32_IE 0x400
  25. #endif
  26. #endif
  27. #include "config.h"
  28. #include <algorithm>
  29. #include <cerrno>
  30. #include <cstdarg>
  31. #include <cstdlib>
  32. #include <cstdio>
  33. #include <cstring>
  34. #include <mutex>
  35. #include <string>
  36. #ifdef HAVE_DIRENT_H
  37. #include <dirent.h>
  38. #endif
  39. #ifdef HAVE_INTRIN_H
  40. #include <intrin.h>
  41. #endif
  42. #ifdef HAVE_CPUID_H
  43. #include <cpuid.h>
  44. #endif
  45. #ifdef HAVE_SSE_INTRINSICS
  46. #include <xmmintrin.h>
  47. #endif
  48. #ifdef HAVE_SYS_SYSCONF_H
  49. #include <sys/sysconf.h>
  50. #endif
  51. #ifdef HAVE_PROC_PIDPATH
  52. #include <libproc.h>
  53. #endif
  54. #ifdef __FreeBSD__
  55. #include <sys/types.h>
  56. #include <sys/sysctl.h>
  57. #endif
  58. #ifndef _WIN32
  59. #include <unistd.h>
  60. #elif defined(_WIN32_IE)
  61. #include <shlobj.h>
  62. #endif
  63. #include "alcmain.h"
  64. #include "almalloc.h"
  65. #include "alfstream.h"
  66. #include "alspan.h"
  67. #include "alstring.h"
  68. #include "compat.h"
  69. #include "cpu_caps.h"
  70. #include "fpu_modes.h"
  71. #include "logging.h"
  72. #include "strutils.h"
  73. #include "vector.h"
  74. #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
  75. defined(_M_IX86) || defined(_M_X64))
  76. using reg_type = unsigned int;
  77. static inline void get_cpuid(unsigned int f, reg_type *regs)
  78. { __get_cpuid(f, &regs[0], &regs[1], &regs[2], &regs[3]); }
  79. #define CAN_GET_CPUID
  80. #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
  81. defined(_M_IX86) || defined(_M_X64))
  82. using reg_type = int;
  83. static inline void get_cpuid(unsigned int f, reg_type *regs)
  84. { (__cpuid)(regs, f); }
  85. #define CAN_GET_CPUID
  86. #endif
  87. int CPUCapFlags = 0;
  88. void FillCPUCaps(int capfilter)
  89. {
  90. int caps = 0;
  91. /* FIXME: We really should get this for all available CPUs in case different
  92. * CPUs have different caps (is that possible on one machine?). */
  93. #ifdef CAN_GET_CPUID
  94. union {
  95. reg_type regs[4];
  96. char str[sizeof(reg_type[4])];
  97. } cpuinf[3]{};
  98. get_cpuid(0, cpuinf[0].regs);
  99. if(cpuinf[0].regs[0] == 0)
  100. ERR("Failed to get CPUID\n");
  101. else
  102. {
  103. unsigned int maxfunc = cpuinf[0].regs[0];
  104. unsigned int maxextfunc;
  105. get_cpuid(0x80000000, cpuinf[0].regs);
  106. maxextfunc = cpuinf[0].regs[0];
  107. TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
  108. TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
  109. if(maxextfunc >= 0x80000004)
  110. {
  111. get_cpuid(0x80000002, cpuinf[0].regs);
  112. get_cpuid(0x80000003, cpuinf[1].regs);
  113. get_cpuid(0x80000004, cpuinf[2].regs);
  114. TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
  115. }
  116. if(maxfunc >= 1)
  117. {
  118. get_cpuid(1, cpuinf[0].regs);
  119. if((cpuinf[0].regs[3]&(1<<25)))
  120. caps |= CPU_CAP_SSE;
  121. if((caps&CPU_CAP_SSE) && (cpuinf[0].regs[3]&(1<<26)))
  122. caps |= CPU_CAP_SSE2;
  123. if((caps&CPU_CAP_SSE2) && (cpuinf[0].regs[2]&(1<<0)))
  124. caps |= CPU_CAP_SSE3;
  125. if((caps&CPU_CAP_SSE3) && (cpuinf[0].regs[2]&(1<<19)))
  126. caps |= CPU_CAP_SSE4_1;
  127. }
  128. }
  129. #else
  130. /* Assume support for whatever's supported if we can't check for it */
  131. #if defined(HAVE_SSE4_1)
  132. #warning "Assuming SSE 4.1 run-time support!"
  133. caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3 | CPU_CAP_SSE4_1;
  134. #elif defined(HAVE_SSE3)
  135. #warning "Assuming SSE 3 run-time support!"
  136. caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3;
  137. #elif defined(HAVE_SSE2)
  138. #warning "Assuming SSE 2 run-time support!"
  139. caps |= CPU_CAP_SSE | CPU_CAP_SSE2;
  140. #elif defined(HAVE_SSE)
  141. #warning "Assuming SSE run-time support!"
  142. caps |= CPU_CAP_SSE;
  143. #endif
  144. #endif
  145. #ifdef HAVE_NEON
  146. al::ifstream file{"/proc/cpuinfo"};
  147. if(!file.is_open())
  148. ERR("Failed to open /proc/cpuinfo, cannot check for NEON support\n");
  149. else
  150. {
  151. std::string features;
  152. auto getline = [](std::istream &f, std::string &output) -> bool
  153. {
  154. while(f.good() && f.peek() == '\n')
  155. f.ignore();
  156. return std::getline(f, output) && !output.empty();
  157. };
  158. while(getline(file, features))
  159. {
  160. if(features.compare(0, 10, "Features\t:", 10) == 0)
  161. break;
  162. }
  163. file.close();
  164. size_t extpos{9};
  165. while((extpos=features.find("neon", extpos+1)) != std::string::npos)
  166. {
  167. if((extpos == 0 || std::isspace(features[extpos-1])) &&
  168. (extpos+4 == features.length() || std::isspace(features[extpos+4])))
  169. {
  170. caps |= CPU_CAP_NEON;
  171. break;
  172. }
  173. }
  174. }
  175. #endif
  176. TRACE("Extensions:%s%s%s%s%s%s\n",
  177. ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
  178. ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
  179. ((capfilter&CPU_CAP_SSE3) ? ((caps&CPU_CAP_SSE3) ? " +SSE3" : " -SSE3") : ""),
  180. ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
  181. ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +NEON" : " -NEON") : ""),
  182. ((!capfilter) ? " -none-" : "")
  183. );
  184. CPUCapFlags = caps & capfilter;
  185. }
  186. FPUCtl::FPUCtl()
  187. {
  188. #if defined(HAVE_SSE_INTRINSICS)
  189. this->sse_state = _mm_getcsr();
  190. unsigned int sseState = this->sse_state;
  191. sseState |= 0x8000; /* set flush-to-zero */
  192. sseState |= 0x0040; /* set denormals-are-zero */
  193. _mm_setcsr(sseState);
  194. #elif defined(__GNUC__) && defined(HAVE_SSE)
  195. if((CPUCapFlags&CPU_CAP_SSE))
  196. {
  197. __asm__ __volatile__("stmxcsr %0" : "=m" (*&this->sse_state));
  198. unsigned int sseState = this->sse_state;
  199. sseState |= 0x8000; /* set flush-to-zero */
  200. if((CPUCapFlags&CPU_CAP_SSE2))
  201. sseState |= 0x0040; /* set denormals-are-zero */
  202. __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
  203. }
  204. #endif
  205. this->in_mode = true;
  206. }
  207. void FPUCtl::leave()
  208. {
  209. if(!this->in_mode) return;
  210. #if defined(HAVE_SSE_INTRINSICS)
  211. _mm_setcsr(this->sse_state);
  212. #elif defined(__GNUC__) && defined(HAVE_SSE)
  213. if((CPUCapFlags&CPU_CAP_SSE))
  214. __asm__ __volatile__("ldmxcsr %0" : : "m" (*&this->sse_state));
  215. #endif
  216. this->in_mode = false;
  217. }
  218. #ifdef _WIN32
  219. const PathNamePair &GetProcBinary()
  220. {
  221. static PathNamePair ret;
  222. if(!ret.fname.empty() || !ret.path.empty())
  223. return ret;
  224. al::vector<WCHAR> fullpath(256);
  225. DWORD len;
  226. while((len=GetModuleFileNameW(nullptr, fullpath.data(), static_cast<DWORD>(fullpath.size()))) == fullpath.size())
  227. fullpath.resize(fullpath.size() << 1);
  228. if(len == 0)
  229. {
  230. ERR("Failed to get process name: error %lu\n", GetLastError());
  231. return ret;
  232. }
  233. fullpath.resize(len);
  234. if(fullpath.back() != 0)
  235. fullpath.push_back(0);
  236. auto sep = std::find(fullpath.rbegin()+1, fullpath.rend(), '\\');
  237. sep = std::find(fullpath.rbegin()+1, sep, '/');
  238. if(sep != fullpath.rend())
  239. {
  240. *sep = 0;
  241. ret.fname = wstr_to_utf8(&*sep + 1);
  242. ret.path = wstr_to_utf8(fullpath.data());
  243. }
  244. else
  245. ret.fname = wstr_to_utf8(fullpath.data());
  246. TRACE("Got binary: %s, %s\n", ret.path.c_str(), ret.fname.c_str());
  247. return ret;
  248. }
  249. void al_print(FILE *logfile, const char *fmt, ...)
  250. {
  251. al::vector<char> dynmsg;
  252. char stcmsg[256];
  253. char *str{stcmsg};
  254. va_list args, args2;
  255. va_start(args, fmt);
  256. va_copy(args2, args);
  257. int msglen{std::vsnprintf(str, sizeof(stcmsg), fmt, args)};
  258. if UNLIKELY(msglen >= 0 && static_cast<size_t>(msglen) >= sizeof(stcmsg))
  259. {
  260. dynmsg.resize(static_cast<size_t>(msglen) + 1u);
  261. str = dynmsg.data();
  262. msglen = std::vsnprintf(str, dynmsg.size(), fmt, args2);
  263. }
  264. va_end(args2);
  265. va_end(args);
  266. std::wstring wstr{utf8_to_wstr(str)};
  267. fputws(wstr.c_str(), logfile);
  268. fflush(logfile);
  269. }
  270. static inline int is_slash(int c)
  271. { return (c == '\\' || c == '/'); }
  272. static void DirectorySearch(const char *path, const char *ext, al::vector<std::string> *const results)
  273. {
  274. std::string pathstr{path};
  275. pathstr += "\\*";
  276. pathstr += ext;
  277. TRACE("Searching %s\n", pathstr.c_str());
  278. std::wstring wpath{utf8_to_wstr(pathstr.c_str())};
  279. WIN32_FIND_DATAW fdata;
  280. HANDLE hdl{FindFirstFileW(wpath.c_str(), &fdata)};
  281. if(hdl == INVALID_HANDLE_VALUE) return;
  282. const auto base = results->size();
  283. do {
  284. results->emplace_back();
  285. std::string &str = results->back();
  286. str = path;
  287. str += '\\';
  288. str += wstr_to_utf8(fdata.cFileName);
  289. } while(FindNextFileW(hdl, &fdata));
  290. FindClose(hdl);
  291. const al::span<std::string> newlist{results->data()+base, results->size()-base};
  292. std::sort(newlist.begin(), newlist.end());
  293. for(const auto &name : newlist)
  294. TRACE(" got %s\n", name.c_str());
  295. }
  296. al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
  297. {
  298. static std::mutex search_lock;
  299. std::lock_guard<std::mutex> _{search_lock};
  300. /* If the path is absolute, use it directly. */
  301. al::vector<std::string> results;
  302. if(isalpha(subdir[0]) && subdir[1] == ':' && is_slash(subdir[2]))
  303. {
  304. std::string path{subdir};
  305. std::replace(path.begin(), path.end(), '/', '\\');
  306. DirectorySearch(path.c_str(), ext, &results);
  307. return results;
  308. }
  309. if(subdir[0] == '\\' && subdir[1] == '\\' && subdir[2] == '?' && subdir[3] == '\\')
  310. {
  311. DirectorySearch(subdir, ext, &results);
  312. return results;
  313. }
  314. std::string path;
  315. /* Search the app-local directory. */
  316. if(auto localpath = al::getenv(L"ALSOFT_LOCAL_PATH"))
  317. {
  318. path = wstr_to_utf8(localpath->c_str());
  319. if(is_slash(path.back()))
  320. path.pop_back();
  321. }
  322. else if(WCHAR *cwdbuf{_wgetcwd(nullptr, 0)})
  323. {
  324. path = wstr_to_utf8(cwdbuf);
  325. if(is_slash(path.back()))
  326. path.pop_back();
  327. free(cwdbuf);
  328. }
  329. else
  330. path = ".";
  331. std::replace(path.begin(), path.end(), '/', '\\');
  332. DirectorySearch(path.c_str(), ext, &results);
  333. /* Search the local and global data dirs. */
  334. static const int ids[2]{ CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
  335. for(int id : ids)
  336. {
  337. WCHAR buffer[MAX_PATH];
  338. if(SHGetSpecialFolderPathW(nullptr, buffer, id, FALSE) == FALSE)
  339. continue;
  340. path = wstr_to_utf8(buffer);
  341. if(!is_slash(path.back()))
  342. path += '\\';
  343. path += subdir;
  344. std::replace(path.begin(), path.end(), '/', '\\');
  345. DirectorySearch(path.c_str(), ext, &results);
  346. }
  347. return results;
  348. }
  349. void SetRTPriority(void)
  350. {
  351. bool failed = false;
  352. if(RTPrioLevel > 0)
  353. failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  354. if(failed) ERR("Failed to set priority level for thread\n");
  355. }
  356. #else
  357. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
  358. #include <pthread.h>
  359. #include <sched.h>
  360. #endif
  361. const PathNamePair &GetProcBinary()
  362. {
  363. static PathNamePair ret;
  364. if(!ret.fname.empty() || !ret.path.empty())
  365. return ret;
  366. al::vector<char> pathname;
  367. #ifdef __FreeBSD__
  368. size_t pathlen;
  369. int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
  370. if(sysctl(mib, 4, nullptr, &pathlen, nullptr, 0) == -1)
  371. WARN("Failed to sysctl kern.proc.pathname: %s\n", strerror(errno));
  372. else
  373. {
  374. pathname.resize(pathlen + 1);
  375. sysctl(mib, 4, pathname.data(), &pathlen, nullptr, 0);
  376. pathname.resize(pathlen);
  377. }
  378. #endif
  379. #ifdef HAVE_PROC_PIDPATH
  380. if(pathname.empty())
  381. {
  382. char procpath[PROC_PIDPATHINFO_MAXSIZE]{};
  383. const pid_t pid{getpid()};
  384. if(proc_pidpath(pid, procpath, sizeof(procpath)) < 1)
  385. ERR("proc_pidpath(%d, ...) failed: %s\n", pid, strerror(errno));
  386. else
  387. pathname.insert(pathname.end(), procpath, procpath+strlen(procpath));
  388. }
  389. #endif
  390. if(pathname.empty())
  391. {
  392. pathname.resize(256);
  393. const char *selfname{"/proc/self/exe"};
  394. ssize_t len{readlink(selfname, pathname.data(), pathname.size())};
  395. if(len == -1 && errno == ENOENT)
  396. {
  397. selfname = "/proc/self/file";
  398. len = readlink(selfname, pathname.data(), pathname.size());
  399. }
  400. if(len == -1 && errno == ENOENT)
  401. {
  402. selfname = "/proc/curproc/exe";
  403. len = readlink(selfname, pathname.data(), pathname.size());
  404. }
  405. if(len == -1 && errno == ENOENT)
  406. {
  407. selfname = "/proc/curproc/file";
  408. len = readlink(selfname, pathname.data(), pathname.size());
  409. }
  410. while(len > 0 && static_cast<size_t>(len) == pathname.size())
  411. {
  412. pathname.resize(pathname.size() << 1);
  413. len = readlink(selfname, pathname.data(), pathname.size());
  414. }
  415. if(len <= 0)
  416. {
  417. WARN("Failed to readlink %s: %s\n", selfname, strerror(errno));
  418. return ret;
  419. }
  420. pathname.resize(static_cast<size_t>(len));
  421. }
  422. while(!pathname.empty() && pathname.back() == 0)
  423. pathname.pop_back();
  424. auto sep = std::find(pathname.crbegin(), pathname.crend(), '/');
  425. if(sep != pathname.crend())
  426. {
  427. ret.path = std::string(pathname.cbegin(), sep.base()-1);
  428. ret.fname = std::string(sep.base(), pathname.cend());
  429. }
  430. else
  431. ret.fname = std::string(pathname.cbegin(), pathname.cend());
  432. TRACE("Got binary: %s, %s\n", ret.path.c_str(), ret.fname.c_str());
  433. return ret;
  434. }
  435. void al_print(FILE *logfile, const char *fmt, ...)
  436. {
  437. va_list ap;
  438. va_start(ap, fmt);
  439. vfprintf(logfile, fmt, ap);
  440. va_end(ap);
  441. fflush(logfile);
  442. }
  443. static void DirectorySearch(const char *path, const char *ext, al::vector<std::string> *const results)
  444. {
  445. TRACE("Searching %s for *%s\n", path, ext);
  446. DIR *dir{opendir(path)};
  447. if(!dir) return;
  448. const auto base = results->size();
  449. const size_t extlen{strlen(ext)};
  450. struct dirent *dirent;
  451. while((dirent=readdir(dir)) != nullptr)
  452. {
  453. if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0)
  454. continue;
  455. const size_t len{strlen(dirent->d_name)};
  456. if(len <= extlen) continue;
  457. if(al::strcasecmp(dirent->d_name+len-extlen, ext) != 0)
  458. continue;
  459. results->emplace_back();
  460. std::string &str = results->back();
  461. str = path;
  462. if(str.back() != '/')
  463. str.push_back('/');
  464. str += dirent->d_name;
  465. }
  466. closedir(dir);
  467. const al::span<std::string> newlist{results->data()+base, results->size()-base};
  468. std::sort(newlist.begin(), newlist.end());
  469. for(const auto &name : newlist)
  470. TRACE(" got %s\n", name.c_str());
  471. }
  472. al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
  473. {
  474. static std::mutex search_lock;
  475. std::lock_guard<std::mutex> _{search_lock};
  476. al::vector<std::string> results;
  477. if(subdir[0] == '/')
  478. {
  479. DirectorySearch(subdir, ext, &results);
  480. return results;
  481. }
  482. /* Search the app-local directory. */
  483. if(auto localpath = al::getenv("ALSOFT_LOCAL_PATH"))
  484. DirectorySearch(localpath->c_str(), ext, &results);
  485. else
  486. {
  487. al::vector<char> cwdbuf(256);
  488. while(!getcwd(cwdbuf.data(), cwdbuf.size()))
  489. {
  490. if(errno != ERANGE)
  491. {
  492. cwdbuf.clear();
  493. break;
  494. }
  495. cwdbuf.resize(cwdbuf.size() << 1);
  496. }
  497. if(cwdbuf.empty())
  498. DirectorySearch(".", ext, &results);
  499. else
  500. {
  501. DirectorySearch(cwdbuf.data(), ext, &results);
  502. cwdbuf.clear();
  503. }
  504. }
  505. // Search local data dir
  506. if(auto datapath = al::getenv("XDG_DATA_HOME"))
  507. {
  508. std::string &path = *datapath;
  509. if(path.back() != '/')
  510. path += '/';
  511. path += subdir;
  512. DirectorySearch(path.c_str(), ext, &results);
  513. }
  514. else if(auto homepath = al::getenv("HOME"))
  515. {
  516. std::string &path = *homepath;
  517. if(path.back() == '/')
  518. path.pop_back();
  519. path += "/.local/share/";
  520. path += subdir;
  521. DirectorySearch(path.c_str(), ext, &results);
  522. }
  523. // Search global data dirs
  524. std::string datadirs{al::getenv("XDG_DATA_DIRS").value_or("/usr/local/share/:/usr/share/")};
  525. size_t curpos{0u};
  526. while(curpos < datadirs.size())
  527. {
  528. size_t nextpos{datadirs.find(':', curpos)};
  529. std::string path{(nextpos != std::string::npos) ?
  530. datadirs.substr(curpos, nextpos++ - curpos) : datadirs.substr(curpos)};
  531. curpos = nextpos;
  532. if(path.empty()) continue;
  533. if(path.back() != '/')
  534. path += '/';
  535. path += subdir;
  536. DirectorySearch(path.c_str(), ext, &results);
  537. }
  538. return results;
  539. }
  540. void SetRTPriority()
  541. {
  542. bool failed = false;
  543. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
  544. if(RTPrioLevel > 0)
  545. {
  546. struct sched_param param;
  547. /* Use the minimum real-time priority possible for now (on Linux this
  548. * should be 1 for SCHED_RR) */
  549. param.sched_priority = sched_get_priority_min(SCHED_RR);
  550. failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
  551. }
  552. #else
  553. /* Real-time priority not available */
  554. failed = (RTPrioLevel>0);
  555. #endif
  556. if(failed)
  557. ERR("Failed to set priority level for thread\n");
  558. }
  559. #endif