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.

278 lines
8.0KB

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