Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #517 from mxfm/unabletoset
Browse files Browse the repository at this point in the history
Attempt to fix "Unable to set thread priority" bug
  • Loading branch information
MartinNowak committed Jun 20, 2013
2 parents 73d3205 + 22c1bf5 commit 7df7e30
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
37 changes: 36 additions & 1 deletion src/core/thread.d
Expand Up @@ -567,13 +567,18 @@ class Thread
* In:
* This routine may only be called once per thread instance.
*
* Params:
* priority = Priority of this thread. The value must be in range
* PRIORITY_MIN - PRIORITY_MAX. Default value is PRIORITY_DEFAULT.
*
* Throws:
* ThreadException if the thread fails to start.
*/
final void start()
final void start( int priority = PRIORITY_DEFAULT )
in
{
assert( !next && !prev );
assert( priority >= PRIORITY_MIN && priority <= PRIORITY_MAX );
}
body
{
Expand All @@ -596,6 +601,13 @@ class Thread
throw new ThreadException( "Error initializing thread stack size" );
if( pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE ) )
throw new ThreadException( "Error setting thread joinable" );
if( priority != PRIORITY_DEFAULT )
{
sched_param param;
param.sched_priority = priority;
if( pthread_attr_setschedparam(&attr, &param) )
throw new ThreadException( "Error setting thread priority" );
}
}

version( Windows )
Expand All @@ -613,6 +625,11 @@ class Thread
m_hndl = cast(HANDLE) _beginthreadex( null, cast(uint) m_sz, &thread_entryPoint, cast(void*) this, CREATE_SUSPENDED, &m_addr );
if( cast(size_t) m_hndl == 0 )
throw new ThreadException( "Error creating thread" );
if( priority != PRIORITY_DEFAULT )
{
if( !SetThreadPriority( m_hndl, priority ) )
throw new ThreadException( "Error setting thread priority" );
}
}

// NOTE: The starting thread must be added to the global thread list
Expand Down Expand Up @@ -661,6 +678,24 @@ class Thread
}


unittest
{
int prio = Thread.PRIORITY_DEFAULT;

void test()
{
auto thr = new Thread({assert(Thread.getThis().priority == prio);});
thr.start(prio);
assert(thr.priority == prio);
thr.join();
}
test();
prio = Thread.PRIORITY_MIN;
test();
prio = Thread.PRIORITY_MAX;
test();
}

/**
* Waits for this thread to complete. If the thread terminated as the
* result of an unhandled exception, this exception will be rethrown.
Expand Down
6 changes: 5 additions & 1 deletion src/core/thread.di
Expand Up @@ -163,10 +163,14 @@ class Thread
* In:
* This routine may only be called once per thread instance.
*
* Params:
* priority = Priority of this thread. The value must be in range
* PRIORITY_MIN - PRIORITY_MAX. Default value is PRIORITY_DEFAULT.
*
* Throws:
* ThreadException if the thread fails to start.
*/
final void start();
final void start( int priority = PRIORITY_DEFAULT );


/**
Expand Down

0 comments on commit 7df7e30

Please sign in to comment.