Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented thread handling for WaitForMultipleObjects. #1964

Merged
merged 18 commits into from
Nov 11, 2014

Conversation

akallabeth
Copy link
Member

Implemented thread specific functions:

  • Thread debugging (open handles, ...) now available via CMake flag
    ** Add DumpThreadHandles wherever you want to know the state of thread handles
  • CloseHandle now properly supports threads via registered close function
  • WaitForSingleObject and WaitForMultipleObjects now support threads in INFINITE and timed wait.
  • WaitForMultipleObjects bWaitAll now supported
  • _GetCurrentThread now actually returns a valid handle.
  • Thread pool cleanup fixed, now properly cleaning up resources in ArrayList free function and exiting thread.
  • pthread_mutex_timedlock now using actual timeout instead of 10ms polling.

@freerdp-bot
Copy link

Test FAILed.
Refer to this link for build results: https://ci.freerdp.com/job/PullRequestBuilder/474/

@freerdp-bot
Copy link

Test FAILed.
Refer to this link for build results: https://ci.freerdp.com/job/PullRequestBuilder/473/

@freerdp-bot
Copy link

Test FAILed.
Refer to this link for build results: https://ci.freerdp.com/job/PullRequestBuilder/475/

@freerdp-bot
Copy link

Test FAILed.
Refer to this link for build results: https://ci.freerdp.com/job/PullRequestBuilder/476/

@akallabeth
Copy link
Member Author

@freerdp-bot test

@freerdp-bot
Copy link

Test PASSed.
Refer to this link for build results: https://ci.freerdp.com/job/PullRequestBuilder/477/

@freerdp-bot
Copy link

Test FAILed.
Refer to this link for build results: https://ci.freerdp.com/job/PullRequestBuilder/478/

@MartinHaimberger
Copy link
Member

+1, code looks good besides the failing test

@freerdp-bot
Copy link

Test PASSed.
Refer to this link for build results: https://ci.freerdp.com/job/PullRequestBuilder/479/

@akallabeth
Copy link
Member Author

@MartinHaimberger Last commit fixes failing tests.

@freerdp-bot
Copy link

Test PASSed.
Refer to this link for build results: https://ci.freerdp.com/job/PullRequestBuilder/480/


fprintf(stderr, "---------------- End Dumping thread handles -------------\n");
}
#endif
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really do like this idea!

@akallabeth
Copy link
Member Author

@bmiklautz @MartinHaimberger Added support for bWaitAll and fixed bugs found during testing, looks more solid now. Could you please confirm?

@freerdp-bot
Copy link

Test FAILed.
Refer to this link for build results: https://ci.freerdp.com/job/PullRequestBuilder/482/

@freerdp-bot
Copy link

Test FAILed.
Refer to this link for build results: https://ci.freerdp.com/job/PullRequestBuilder/483/

@akallabeth
Copy link
Member Author

@freerdp-bot test

@freerdp-bot
Copy link

Test PASSed.
Refer to this link for build results: https://ci.freerdp.com/job/PullRequestBuilder/484/

@akallabeth
Copy link
Member Author

@bmiklautz @MartinHaimberger @awakecoding @mfleisz @nfedera Did some more testing, working quite well now. Could you check if it works for you as well?

@freerdp-bot
Copy link

Test PASSed.
Refer to this link for build results: https://ci.freerdp.com/job/PullRequestBuilder/488/


#endif
}

/**
* TODO: implement thread suspend/resume using pthreads
* http://stackoverflow.com/questions/3140867/suspend-pthreads-without-using-condition
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've read the article and I really don't know who would want a pthread_suspend() function. When a thread gets suspended it should always be a volontary action (from the thread). Otherwise you can get suspended with some mutex hold and lead to a deadlock.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hardening if I'm not mistaken windows can create a suspended thread so I guess that why one want that in winpr.

@hardening
Copy link
Contributor

With the recent changes around monolithic, can't a Thread have an Event instead of dupllicating the Event code in Thread ?

@hardening
Copy link
Contributor

except my remark LGTM, +1

@akallabeth
Copy link
Member Author

@hardening could probably be done. Just didn't want the dependency on event in thread to avoid the effort (recursive calls, ...) for testing.

@freerdp-bot
Copy link

Test PASSed.
Refer to this link for build results: https://ci.freerdp.com/job/PullRequestBuilder/513/

@freerdp-bot
Copy link

Test FAILed.
Refer to this link for build results: https://ci.freerdp.com/job/PullRequestBuilder/566/

@freerdp-bot
Copy link

Test FAILed.
Refer to this link for build results: https://ci.freerdp.com/job/PullRequestBuilder/565/

@akallabeth
Copy link
Member Author

@freerdp-bot test

@freerdp-bot
Copy link

Test FAILed.
Refer to this link for build results (access rights to CI server needed):
https://ci.freerdp.com//job/PullRequestBuilder/651/Test FAILed.

@bmiklautz
Copy link
Member

@freerdp-bot test

@freerdp-bot
Copy link

Test FAILed.
Refer to this link for build results (access rights to CI server needed):
https://ci.freerdp.com//job/PullRequestBuilder/652/Test FAILed.

@bmiklautz
Copy link
Member

@freerdp-bot test

@freerdp-bot
Copy link

Test PASSed.
Refer to this link for build results (access rights to CI server needed):
https://ci.freerdp.com//job/PullRequestBuilder/653/Test PASSed.


thread->started = FALSE;
if (!thread->joined)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a race condition on this variable if 2 threads call WaitForSingleObject() concurrently.
pthread_join() is atomically correct with this, so we should call it inconditionaly, and we can remove this struct member too (joined).

@hardening
Copy link
Contributor

@akallabeth sorry to comment so late.

@mfleisz
Copy link
Member

mfleisz commented Oct 31, 2014

@akallabeth: Looks good despite the two comments from @hardening. If this is fixed I'll merge since this has been open for ages and nobody seems to have any objections.

@akallabeth
Copy link
Member Author

So, fixed the races. The flag is still there (preventing multiple waits to call pthread_join, which is undefined) but it is locked by a mutex now.
@hardening better late than never, no problem ;)
@mfleisz Ok, looking forward to that.

@freerdp-bot
Copy link

Test PASSed.
Refer to this link for build results (access rights to CI server needed):
https://ci.freerdp.com//job/PullRequestBuilder/662/Test PASSed.

@hardening
Copy link
Contributor

pthread_join() is a real pain, it's functional only for trivial, limited, cases, which is of course the one you never hit in real programs. I don't know why they gave such a stupid definition for pthread_join()...

Anyway I guess that in
pthread_lock()
.... pthread_join()
pthread_unlock()

We could use a pthread_condition stored in the thread to accomplish the same operation and allowing multiple thread to wait for completion. The pthread_condition would also allow to do a wait with timeout. It's not a brilliant idea from me it's just that many toolkit are doing it this way (at least Qt, and others)

{
WLog_ERR(TAG, "pthread_join failure: [%d] %s",
status, strerror(status));
return WAIT_FAILED;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this exit path we're still holding the thread->mutex lock

@freerdp-bot
Copy link

Test PASSed.
Refer to this link for build results (access rights to CI server needed):
https://ci.freerdp.com//job/PullRequestBuilder/671/Test PASSed.

mfleisz added a commit that referenced this pull request Nov 11, 2014
Implemented thread handling for WaitForMultipleObjects.
@mfleisz mfleisz merged commit 6cd55a5 into FreeRDP:master Nov 11, 2014
@akallabeth akallabeth deleted the thread-wait-support branch November 11, 2014 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants