From 10d1228e86a5f1e48f0cf65bd6b696e4287e9ffa Mon Sep 17 00:00:00 2001 From: hogliux Date: Mon, 15 Feb 2016 11:06:28 +0000 Subject: [PATCH] Add option to set default stack size for JUCE threads --- .../juce_core/native/java/JuceAppActivity.java | 7 ++++--- .../juce_core/native/juce_android_JNIHelpers.h | 2 +- modules/juce_core/native/juce_android_Threads.cpp | 15 +++++++-------- modules/juce_core/native/juce_posix_SharedCode.h | 14 +++++++++++++- modules/juce_core/native/juce_win32_Threads.cpp | 2 +- modules/juce_core/threads/juce_Thread.cpp | 3 ++- modules/juce_core/threads/juce_Thread.h | 9 ++++++++- 7 files changed, 36 insertions(+), 16 deletions(-) diff --git a/modules/juce_core/native/java/JuceAppActivity.java b/modules/juce_core/native/java/JuceAppActivity.java index 5675699ba1..d69ccdd0e1 100644 --- a/modules/juce_core/native/java/JuceAppActivity.java +++ b/modules/juce_core/native/java/JuceAppActivity.java @@ -1132,8 +1132,9 @@ public class JuceAppActivity extends Activity private static class JuceThread extends Thread { - public JuceThread (long host) + public JuceThread (long host, String threadName, long threadStackSize) { + super (null, null, threadName, threadStackSize); _this = host; } @@ -1146,8 +1147,8 @@ public class JuceAppActivity extends Activity private long _this; } - public final Thread createNewThread(long host) + public final Thread createNewThread(long host, String threadName, long threadStackSize) { - return new JuceThread(host); + return new JuceThread(host, threadName, threadStackSize); } } diff --git a/modules/juce_core/native/juce_android_JNIHelpers.h b/modules/juce_core/native/juce_android_JNIHelpers.h index 83ad5b74f6..98e3389410 100644 --- a/modules/juce_core/native/juce_android_JNIHelpers.h +++ b/modules/juce_core/native/juce_android_JNIHelpers.h @@ -293,7 +293,7 @@ extern AndroidSystem android; METHOD (audioManagerGetProperty, "audioManagerGetProperty", "(Ljava/lang/String;)Ljava/lang/String;") \ METHOD (setCurrentThreadPriority, "setCurrentThreadPriority", "(I)I") \ METHOD (hasSystemFeature, "hasSystemFeature", "(Ljava/lang/String;)Z" ) \ - METHOD (createNewThread, "createNewThread", "(J)Ljava/lang/Thread;") \ + METHOD (createNewThread, "createNewThread", "(JLjava/lang/String;J)Ljava/lang/Thread;") \ DECLARE_JNI_CLASS (JuceAppActivity, JUCE_ANDROID_ACTIVITY_CLASSPATH); #undef JNI_CLASS_MEMBERS diff --git a/modules/juce_core/native/juce_android_Threads.cpp b/modules/juce_core/native/juce_android_Threads.cpp index 585b97a7e6..0636fe5c48 100644 --- a/modules/juce_core/native/juce_android_Threads.cpp +++ b/modules/juce_core/native/juce_android_Threads.cpp @@ -84,10 +84,8 @@ struct AndroidThreadData void JUCE_API juce_threadEntryPoint (void*); -extern "C" void* threadEntryProc (void*); -extern "C" void* threadEntryProc (void* userData) +void* threadEntryProc (AndroidThreadData* priv) { - ScopedPointer priv (reinterpret_cast (userData)); priv->tId = (Thread::ThreadID) pthread_self(); priv->eventSet.signal(); priv->eventGet.wait (-1); @@ -100,12 +98,10 @@ extern "C" void* threadEntryProc (void* userData) JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024JuceThread), runThread, void, (JNIEnv* env, jobject device, jlong host)) { - // Java may create a Midi thread which JUCE doesn't know about and this callback may be - // received on this thread. Java will have already created a JNI Env for this new thread, - // which we need to tell Juce about + // This thread does not have a JNIEnv assigned to it yet. So assign it now. setEnv (env); - if (Thread* thread = reinterpret_cast (host)) + if (AndroidThreadData* thread = reinterpret_cast (host)) threadEntryProc (thread); } @@ -114,8 +110,11 @@ void Thread::launchThread() threadHandle = 0; ScopedPointer threadPrivateData = new AndroidThreadData (this); + const LocalRef jName (javaString (threadName)); - jobject juceNewThread = android.activity.callObjectMethod (JuceAppActivity.createNewThread, (jlong) threadPrivateData.get()); + jobject juceNewThread = android.activity.callObjectMethod (JuceAppActivity.createNewThread, + (jlong) threadPrivateData.get(), + jName.get(), (jlong) threadStackSize); if (jobject juceThread = getEnv()->NewGlobalRef (juceNewThread)) { diff --git a/modules/juce_core/native/juce_posix_SharedCode.h b/modules/juce_core/native/juce_posix_SharedCode.h index ddba9d527d..e2d3658f4f 100644 --- a/modules/juce_core/native/juce_posix_SharedCode.h +++ b/modules/juce_core/native/juce_posix_SharedCode.h @@ -898,13 +898,25 @@ void Thread::launchThread() { threadHandle = 0; pthread_t handle = 0; + pthread_attr_t attr; + pthread_attr_t* attrPtr = nullptr; - if (pthread_create (&handle, 0, threadEntryProc, this) == 0) + if (pthread_attr_init (&attr) == 0) + { + attrPtr = &attr; + + pthread_attr_setstacksize (attrPtr, threadStackSize); + } + + if (pthread_create (&handle, attrPtr, threadEntryProc, this) == 0) { pthread_detach (handle); threadHandle = (void*) handle; threadId = (ThreadID) threadHandle; } + + if (attrPtr != nullptr) + pthread_attr_destroy (attrPtr); } void Thread::closeThreadHandle() diff --git a/modules/juce_core/native/juce_win32_Threads.cpp b/modules/juce_core/native/juce_win32_Threads.cpp index fec0635c93..91f61682c6 100644 --- a/modules/juce_core/native/juce_win32_Threads.cpp +++ b/modules/juce_core/native/juce_win32_Threads.cpp @@ -109,7 +109,7 @@ static unsigned int __stdcall threadEntryProc (void* userData) void Thread::launchThread() { unsigned int newThreadId; - threadHandle = (void*) _beginthreadex (0, 0, &threadEntryProc, this, 0, &newThreadId); + threadHandle = (void*) _beginthreadex (0, threadStackSize, &threadEntryProc, this, 0, &newThreadId); threadId = (ThreadID) (pointer_sized_int) newThreadId; } diff --git a/modules/juce_core/threads/juce_Thread.cpp b/modules/juce_core/threads/juce_Thread.cpp index 721f7410b6..27ab0d5c5b 100644 --- a/modules/juce_core/threads/juce_Thread.cpp +++ b/modules/juce_core/threads/juce_Thread.cpp @@ -26,11 +26,12 @@ ============================================================================== */ -Thread::Thread (const String& threadName_) +Thread::Thread (const String& threadName_, const size_t stackSize) : threadName (threadName_), threadHandle (nullptr), threadId (0), threadPriority (5), + threadStackSize (stackSize), affinityMask (0), shouldExit (false) { diff --git a/modules/juce_core/threads/juce_Thread.h b/modules/juce_core/threads/juce_Thread.h index 89a0c507a1..88bcfe9a29 100644 --- a/modules/juce_core/threads/juce_Thread.h +++ b/modules/juce_core/threads/juce_Thread.h @@ -53,8 +53,14 @@ public: When first created, the thread is not running. Use the startThread() method to start it. + + @param threadName The name of the thread which typically appears in + debug logs and profiles. + @param threadStackSize The size of the stack of the thread. If this value + is zero then the default stack size of the OS will + be used. */ - explicit Thread (const String& threadName); + explicit Thread (const String& threadName, size_t threadStackSize = 0); /** Destructor. @@ -270,6 +276,7 @@ private: CriticalSection startStopLock; WaitableEvent startSuspensionEvent, defaultEvent; int threadPriority; + size_t threadStackSize; uint32 affinityMask; bool volatile shouldExit;