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

fix Issue 8960 - Unable to set thread priority #550

Merged
merged 1 commit into from
Sep 28, 2014

Conversation

MartinNowak
Copy link
Member

  • ignore FreeBSD error when get-/setting priority on terminated thread
  • this also ignores invalid thread handles

@ghost
Copy link

ghost commented Jul 25, 2013

Note, that situation on linux and windows has fundamentally the same problem. Would you place some code between starting some quickly terminated thread and setting/getting its priority, you would see same error on these platforms. Also ESRCH does not distinguish between outlived thread IDs and simply wrong ones, so passing any wrong value in FreeBSD will be silently ignored.
My suggestion is to add new parameter which will indicate whether druntime should ignore these errors and test in priority getter/setter whether a thread had terminated.

@MartinNowak
Copy link
Member Author

My suggestion is to add new parameter which will indicate whether druntime should ignore these errors and test in priority getter/setter whether a thread had terminated.

We can't make setting/getting the priority of a thread an error without providing a way to synchronize that operation with the thread lifetime. The other problem is that access to m_isRunning is not synchronized so we're missing a way to determine whether a thread has terminated.

@complexmath
Copy link
Member

Testing for whether a thread is running does have some potential slippage, but not much. Accesses to m_isRunning should probably be made atomic(msync.raw) at least just to make things more clear.

@braddr
Copy link
Member

braddr commented Jul 29, 2013

Essentially any use of a state variable in a conditional before taking some action is buggy unless both the conditional and the action are both done while a single lock is held throughout both operations. Just making the conditional check atomic is insufficient (unless it's one way, like isShutdown or similar conditions).

I totally agree that the conditional on freebsd part is fundamentally wrong. It's all platforms.

@complexmath
Copy link
Member

I wouldn't make setting priority on a non-running thread an error because I'm inclined to say that you should be able to create a thread object, set its priority, and then start the thread. And now that I look at the code, it doesn't support this behavior. Either way though, I don't see a point in trying to enforce that this call is only valid for running threads.

@MartinNowak
Copy link
Member Author

I totally agree that the conditional on freebsd part is fundamentally wrong. It's all platforms.

Well glibc and OSX's libc will simply set/get the priority on the dead thread.

Either way though, I don't see a point in trying to enforce that this call is only valid for running threads.

We'd need to save the priority in the Thread object.

I wouldn't make setting priority on a non-running thread an error because I'm inclined to say that you should be able to create a thread object, set its priority, and then start the thread.

Setting the priority before creation (in the pthread_attr_t) has no effect on FreeBSD. We tried that with #542.

@alexrp
Copy link
Member

alexrp commented Oct 5, 2013

I'm inclined to agree with Sean. You should be able to create a thread, set its priority, start it, and have that priority take effect when it's started. If that means checking the priority in start and putting it into effect if it's non-default, then I think that's fine.

@braddr
Copy link
Member

braddr commented Dec 1, 2013

So, what are we going to do with this issue? I added a really fast mac to the build cluster some this weekend (can't stay full time). It failed something in the neighborhood of 1/4 of the test runs due to this bug.

@WalterBright
Copy link
Member

I don't know enough about this issue to be able to pull this.

@MartinNowak
Copy link
Member Author

Let's close it for now, people didn't like the idead of ignoring an error.
It seems that we can't reliably implement the current API, i.e. changing the priority of a running thread, because there is no simple way to know whether the thread represented by a Thread object has already terminated.

@MartinNowak MartinNowak reopened this May 30, 2014
@MartinNowak
Copy link
Member Author

Reopened.
I think we can be pragmatic about resolving this issue by ignoring this specific error, as invalid handles would only occur with memory corruption. I changed the documentation to reflect that behavior. The problem applies to all OSes btw.

@MartinNowak
Copy link
Member Author

Implementation is now based on m_isRunning.

@quickfur
Copy link
Member

quickfur commented Aug 7, 2014

ping
Looks like we have a merge conflict. Please rebase. I'd like to see this get merged to eliminate annoying random autotester failures.

@quickfur
Copy link
Member

ping

@MartinNowak
Copy link
Member Author

Updated

@quickfur
Copy link
Member

quickfur commented Sep 5, 2014

Failing on Darwin/32 with "Unable to set priority". :-(

- Ignore error when get-/setting the priority of a terminated thread.
@MartinNowak
Copy link
Member Author

OK updated again.

@quickfur
Copy link
Member

Looks clean now. Time to merge?

@DmitryOlshansky
Copy link
Member

LGTM

@DmitryOlshansky
Copy link
Member

Auto-merge toggled on

DmitryOlshansky added a commit that referenced this pull request Sep 28, 2014
fix Issue 8960 - Unable to set thread priority
@DmitryOlshansky DmitryOlshansky merged commit b188057 into dlang:master Sep 28, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
7 participants