The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
7.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. // (This file gets included by juce_linux_NativeCode.cpp, rather than being
  19. // compiled on its own).
  20. #ifdef JUCE_INCLUDED_FILE
  21. /*
  22. Note that a lot of methods that you'd expect to find in this file actually
  23. live in juce_posix_SharedCode.h!
  24. */
  25. //==============================================================================
  26. void JUCE_API juce_threadEntryPoint (void*);
  27. void* threadEntryProc (void* value) throw()
  28. {
  29. // New threads start off as root when running suid
  30. Process::lowerPrivilege();
  31. juce_threadEntryPoint (value);
  32. return 0;
  33. }
  34. void* juce_createThread (void* userData) throw()
  35. {
  36. pthread_t handle = 0;
  37. if (pthread_create (&handle, 0, threadEntryProc, userData) == 0)
  38. {
  39. pthread_detach (handle);
  40. return (void*)handle;
  41. }
  42. return 0;
  43. }
  44. void juce_killThread (void* handle) throw()
  45. {
  46. if (handle != 0)
  47. pthread_cancel ((pthread_t)handle);
  48. }
  49. void juce_setCurrentThreadName (const String& /*name*/) throw()
  50. {
  51. }
  52. Thread::ThreadID Thread::getCurrentThreadId() throw()
  53. {
  54. return (ThreadID) pthread_self();
  55. }
  56. /*
  57. * This is all a bit non-ideal... the trouble is that on Linux you
  58. * need to call setpriority to affect the dynamic priority for
  59. * non-realtime processes, but this requires the pid, which is not
  60. * accessible from the pthread_t. We could get it by calling getpid
  61. * once each thread has started, but then we would need a list of
  62. * running threads etc etc.
  63. * Also there is no such thing as IDLE priority on Linux.
  64. * For the moment, map idle, low and normal process priorities to
  65. * SCHED_OTHER, with the thread priority ignored for these classes.
  66. * Map high priority processes to the lower half of the SCHED_RR
  67. * range, and realtime to the upper half
  68. */
  69. // priority 1 to 10 where 5=normal, 1=low. If the handle is 0, sets the
  70. // priority of the current thread
  71. bool juce_setThreadPriority (void* handle, int priority) throw()
  72. {
  73. struct sched_param param;
  74. int policy, maxp, minp, pri;
  75. if (handle == 0)
  76. handle = (void*) pthread_self();
  77. if (pthread_getschedparam ((pthread_t) handle, &policy, &param) == 0
  78. && policy != SCHED_OTHER)
  79. {
  80. minp = sched_get_priority_min(policy);
  81. maxp = sched_get_priority_max(policy);
  82. pri = ((maxp - minp) / 2) * (priority - 1) / 9;
  83. if (param.__sched_priority >= (minp + (maxp - minp) / 2))
  84. // Realtime process priority
  85. param.__sched_priority = minp + ((maxp - minp) / 2) + pri;
  86. else
  87. // High process priority
  88. param.__sched_priority = minp + pri;
  89. param.sched_priority = jlimit (1, 127, 1 + (priority * 126) / 11);
  90. return pthread_setschedparam ((pthread_t) handle, policy, &param) == 0;
  91. }
  92. return false;
  93. }
  94. void Thread::setCurrentThreadAffinityMask (const uint32 affinityMask) throw()
  95. {
  96. #if SUPPORT_AFFINITIES
  97. cpu_set_t affinity;
  98. CPU_ZERO (&affinity);
  99. for (int i = 0; i < 32; ++i)
  100. if ((affinityMask & (1 << i)) != 0)
  101. CPU_SET (i, &affinity);
  102. /*
  103. N.B. If this line causes a compile error, then you've probably not got the latest
  104. version of glibc installed.
  105. If you don't want to update your copy of glibc and don't care about cpu affinities,
  106. then you can just disable all this stuff by removing the SUPPORT_AFFINITIES macro
  107. from the linuxincludes.h file.
  108. */
  109. sched_setaffinity (getpid(), sizeof (cpu_set_t), &affinity);
  110. sched_yield();
  111. #else
  112. /* affinities aren't supported because either the appropriate header files weren't found,
  113. or the SUPPORT_AFFINITIES macro was turned off in linuxheaders.h
  114. */
  115. jassertfalse
  116. #endif
  117. }
  118. void Thread::yield() throw()
  119. {
  120. sched_yield();
  121. }
  122. //==============================================================================
  123. // sets the process to 0=low priority, 1=normal, 2=high, 3=realtime
  124. void Process::setPriority (ProcessPriority prior)
  125. {
  126. struct sched_param param;
  127. int policy, maxp, minp;
  128. const int p = (int) prior;
  129. if (p <= 1)
  130. policy = SCHED_OTHER;
  131. else
  132. policy = SCHED_RR;
  133. minp = sched_get_priority_min (policy);
  134. maxp = sched_get_priority_max (policy);
  135. if (p < 2)
  136. param.__sched_priority = 0;
  137. else if (p == 2 )
  138. // Set to middle of lower realtime priority range
  139. param.__sched_priority = minp + (maxp - minp) / 4;
  140. else
  141. // Set to middle of higher realtime priority range
  142. param.__sched_priority = minp + (3 * (maxp - minp) / 4);
  143. pthread_setschedparam (pthread_self(), policy, &param);
  144. }
  145. void Process::terminate()
  146. {
  147. exit (0);
  148. }
  149. bool JUCE_CALLTYPE juce_isRunningUnderDebugger() throw()
  150. {
  151. static char testResult = 0;
  152. if (testResult == 0)
  153. {
  154. testResult = (char) ptrace (PT_TRACE_ME, 0, 0, 0);
  155. if (testResult >= 0)
  156. {
  157. ptrace (PT_DETACH, 0, (caddr_t) 1, 0);
  158. testResult = 1;
  159. }
  160. }
  161. return testResult < 0;
  162. }
  163. bool JUCE_CALLTYPE Process::isRunningUnderDebugger() throw()
  164. {
  165. return juce_isRunningUnderDebugger();
  166. }
  167. void Process::raisePrivilege()
  168. {
  169. // If running suid root, change effective user
  170. // to root
  171. if (geteuid() != 0 && getuid() == 0)
  172. {
  173. setreuid (geteuid(), getuid());
  174. setregid (getegid(), getgid());
  175. }
  176. }
  177. void Process::lowerPrivilege()
  178. {
  179. // If runing suid root, change effective user
  180. // back to real user
  181. if (geteuid() == 0 && getuid() != 0)
  182. {
  183. setreuid (geteuid(), getuid());
  184. setregid (getegid(), getgid());
  185. }
  186. }
  187. #if ! JUCE_ONLY_BUILD_CORE_LIBRARY
  188. void* PlatformUtilities::loadDynamicLibrary (const String& name)
  189. {
  190. return dlopen ((const char*) name.toUTF8(), RTLD_LOCAL | RTLD_NOW);
  191. }
  192. void PlatformUtilities::freeDynamicLibrary (void* handle)
  193. {
  194. dlclose(handle);
  195. }
  196. void* PlatformUtilities::getProcedureEntryPoint (void* libraryHandle, const String& procedureName)
  197. {
  198. return dlsym (libraryHandle, (const char*) procedureName);
  199. }
  200. #endif
  201. #endif