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.

262 lines
7.4KB

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