1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/*
==============================================================================
This file is part of the juce_core module of the JUCE library.
Copyright (c) 2015 - ROLI Ltd.
Permission to use, copy, modify, and/or distribute this software for any purpose with
or without fee is hereby granted, provided that the above copyright notice and this
permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
------------------------------------------------------------------------------
NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
All other JUCE modules are covered by a dual GPL/commercial license, so if you are
using any other modules, be sure to check that you also comply with their license.
For more details, visit www.juce.com
==============================================================================
*/
/*
Note that a lot of methods that you'd expect to find in this file actually
live in juce_posix_SharedCode.h!
*/
//==============================================================================
// sets the process to 0=low priority, 1=normal, 2=high, 3=realtime
JUCE_API void JUCE_CALLTYPE Process::setPriority (ProcessPriority prior)
{
// TODO
struct sched_param param;
int policy, maxp, minp;
const int p = (int) prior;
if (p <= 1)
policy = SCHED_OTHER;
else
policy = SCHED_RR;
minp = sched_get_priority_min (policy);
maxp = sched_get_priority_max (policy);
if (p < 2)
param.sched_priority = 0;
else if (p == 2 )
// Set to middle of lower realtime priority range
param.sched_priority = minp + (maxp - minp) / 4;
else
// Set to middle of higher realtime priority range
param.sched_priority = minp + (3 * (maxp - minp) / 4);
pthread_setschedparam (pthread_self(), policy, ¶m);
}
JUCE_API bool JUCE_CALLTYPE juce_isRunningUnderDebugger() noexcept
{
return false;
}
JUCE_API void JUCE_CALLTYPE Process::raisePrivilege() {}
JUCE_API void JUCE_CALLTYPE Process::lowerPrivilege() {}
struct AndroidThreadData
{
AndroidThreadData (Thread* thread) noexcept
: owner (thread), tId (0)
{
}
Thread* owner;
Thread::ThreadID tId;
WaitableEvent eventSet, eventGet;
};
void JUCE_API juce_threadEntryPoint (void*);
void* threadEntryProc (AndroidThreadData* priv)
{
priv->tId = (Thread::ThreadID) pthread_self();
priv->eventSet.signal();
priv->eventGet.wait (-1);
juce_threadEntryPoint (priv->owner);
return nullptr;
}
JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024JuceThread), runThread,
void, (JNIEnv* env, jobject device, jlong host))
{
// This thread does not have a JNIEnv assigned to it yet. So assign it now.
setEnv (env);
if (AndroidThreadData* thread = reinterpret_cast<AndroidThreadData*> (host))
threadEntryProc (thread);
}
void Thread::launchThread()
{
threadHandle = 0;
ScopedPointer<AndroidThreadData> threadPrivateData = new AndroidThreadData (this);
const LocalRef<jstring> jName (javaString (threadName));
jobject juceNewThread = android.activity.callObjectMethod (JuceAppActivity.createNewThread,
(jlong) threadPrivateData.get(),
jName.get(), (jlong) threadStackSize);
if (jobject juceThread = getEnv()->NewGlobalRef (juceNewThread))
{
AndroidThreadData* priv = threadPrivateData.release();
threadHandle = (void*) juceThread;
getEnv()->CallVoidMethod (juceThread, JuceThread.start);
priv->eventSet.wait (-1);
threadId = priv->tId;
priv->eventGet.signal();
}
}
void Thread::closeThreadHandle()
{
if (threadHandle != 0)
{
jobject juceThread = reinterpret_cast<jobject> (threadHandle);
getEnv()->DeleteGlobalRef (juceThread);
threadHandle = 0;
}
threadId = 0;
}
void Thread::killThread()
{
if (threadHandle != 0)
{
jobject juceThread = reinterpret_cast<jobject> (threadHandle);
getEnv()->CallVoidMethod (juceThread, JuceThread.stop);
}
}
void JUCE_CALLTYPE Thread::setCurrentThreadName (const String& name)
{
LocalRef<jobject> juceThread (getEnv()->CallStaticObjectMethod (JuceThread, JuceThread.currentThread));
if (jobject t = juceThread.get())
getEnv()->CallVoidMethod (t, JuceThread.setName, javaString (name).get());
}
bool Thread::setThreadPriority (void* handle, int priority)
{
if (handle == nullptr)
{
LocalRef<jobject> juceThread (getEnv()->CallStaticObjectMethod (JuceThread, JuceThread.currentThread));
if (jobject t = juceThread.get())
return setThreadPriority (t, priority);
return false;
}
jobject juceThread = reinterpret_cast<jobject> (handle);
const int minPriority = 1;
const int maxPriority = 10;
jint javaPriority = ((maxPriority - minPriority) * priority) / 10 + minPriority;
getEnv()->CallVoidMethod (juceThread, JuceThread.setPriority, javaPriority);
return true;
}
//==============================================================================
struct HighResolutionTimer::Pimpl
{
struct HighResolutionThread : public Thread
{
HighResolutionThread (HighResolutionTimer::Pimpl& parent)
: Thread ("High Resolution Timer"), pimpl (parent)
{
startThread();
}
void run() override
{
pimpl.timerThread();
}
private:
HighResolutionTimer::Pimpl& pimpl;
};
//==============================================================================
Pimpl (HighResolutionTimer& t) : owner (t) {}
~Pimpl()
{
stop();
}
void start (int newPeriod)
{
if (periodMs != newPeriod)
{
if (thread.get() == nullptr
|| thread->getThreadId() != Thread::getCurrentThreadId()
|| thread->threadShouldExit())
{
stop();
periodMs = newPeriod;
thread = new HighResolutionThread (*this);
}
else
{
periodMs = newPeriod;
}
}
}
void stop()
{
if (thread.get() != nullptr)
{
thread->signalThreadShouldExit();
if (thread->getThreadId() != Thread::getCurrentThreadId())
{
thread->waitForThreadToExit (-1);
thread = nullptr;
}
}
}
HighResolutionTimer& owner;
int volatile periodMs;
private:
ScopedPointer<Thread> thread;
void timerThread()
{
jassert (thread.get() != nullptr);
int lastPeriod = periodMs;
Clock clock (lastPeriod);
while (! thread->threadShouldExit())
{
clock.wait();
owner.hiResTimerCallback();
if (lastPeriod != periodMs)
{
lastPeriod = periodMs;
clock = Clock (lastPeriod);
}
}
periodMs = 0;
}
struct Clock
{
Clock (double millis) noexcept : delta ((uint64) (millis * 1000000))
{
}
void wait() noexcept
{
struct timespec t;
t.tv_sec = (time_t) (delta / 1000000000);
t.tv_nsec = (long) (delta % 1000000000);
nanosleep (&t, nullptr);
}
uint64 delta;
};
static bool setThreadToRealtime (pthread_t thread, uint64 periodMs)
{
ignoreUnused (periodMs);
struct sched_param param;
param.sched_priority = sched_get_priority_max (SCHED_RR);
return pthread_setschedparam (thread, SCHED_RR, ¶m) == 0;
}
JUCE_DECLARE_NON_COPYABLE (Pimpl)
};
|