Browse Source

Add option to set default stack size for JUCE threads

tags/2021-05-28
hogliux 10 years ago
parent
commit
10d1228e86
7 changed files with 36 additions and 16 deletions
  1. +4
    -3
      modules/juce_core/native/java/JuceAppActivity.java
  2. +1
    -1
      modules/juce_core/native/juce_android_JNIHelpers.h
  3. +7
    -8
      modules/juce_core/native/juce_android_Threads.cpp
  4. +13
    -1
      modules/juce_core/native/juce_posix_SharedCode.h
  5. +1
    -1
      modules/juce_core/native/juce_win32_Threads.cpp
  6. +2
    -1
      modules/juce_core/threads/juce_Thread.cpp
  7. +8
    -1
      modules/juce_core/threads/juce_Thread.h

+ 4
- 3
modules/juce_core/native/java/JuceAppActivity.java View File

@@ -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);
}
}

+ 1
- 1
modules/juce_core/native/juce_android_JNIHelpers.h View File

@@ -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


+ 7
- 8
modules/juce_core/native/juce_android_Threads.cpp View File

@@ -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<AndroidThreadData> priv (reinterpret_cast<AndroidThreadData*> (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<Thread*> (host))
if (AndroidThreadData* thread = reinterpret_cast<AndroidThreadData*> (host))
threadEntryProc (thread);
}
@@ -114,8 +110,11 @@ 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());
jobject juceNewThread = android.activity.callObjectMethod (JuceAppActivity.createNewThread,
(jlong) threadPrivateData.get(),
jName.get(), (jlong) threadStackSize);
if (jobject juceThread = getEnv()->NewGlobalRef (juceNewThread))
{


+ 13
- 1
modules/juce_core/native/juce_posix_SharedCode.h View File

@@ -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()


+ 1
- 1
modules/juce_core/native/juce_win32_Threads.cpp View File

@@ -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;
}


+ 2
- 1
modules/juce_core/threads/juce_Thread.cpp View File

@@ -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)
{


+ 8
- 1
modules/juce_core/threads/juce_Thread.h View File

@@ -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;


Loading…
Cancel
Save