Skip to content

Thread: Implement more functionality #1162

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

Merged
merged 3 commits into from
Aug 23, 2017

Conversation

spevans
Copy link
Contributor

@spevans spevans commented Aug 7, 2017

  • Eliminate _compiler_crash_fix() as no crashes are observed anymore.

  • Re-enable TestThread.swift test cases.

I exposed _CFMainPThread since pthread_main_thread_np() is a #define and not a real function on linux and so doesn't export to swift correctly.

I used _CFIsMainThread instead of pthread_main_np for consistency and to avoid the local definition of pthread_main_np() in Operation.swift not having the same semantics as pthread_main_np() on macOS where it can return -1.

- Eliminate _compiler_crash_fix() as no crashes are observed anymore.

- Re-enable TestThread.swift test cases.
@ianpartridge
Copy link
Contributor

@swift-ci please test

@spevans
Copy link
Contributor Author

spevans commented Aug 8, 2017

Looks like ssh to github.com failed

@ianpartridge
Copy link
Contributor

Yeah, timeout. I'll give it another try.

@ianpartridge
Copy link
Contributor

@swift-ci please test

@ianpartridge ianpartridge requested a review from parkera August 9, 2017 09:11

Boolean _CFIsMainThread(void) {
return pthread_main_np() == 1;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is meant to be a Darwin implementation then?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if we should make that #else more platform specific. Other than that, this looks great to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasnt sure what other platforms support it, I thought FreeBSD might have it as well. Thought it would be easier to leave it open and then a version of _CFIsMainThread can be added to platforms as they need it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It also works for Windows since there is a #define pthread_main_np _NS_pthread_main_np wrapped inside a #if DEPLOYMENT_TARGET_WINDOWS

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I guess we'll hear back from people if this breaks their compile.

@ianpartridge
Copy link
Contributor

Can you update the Thread status in https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/Status.md too?

@spevans
Copy link
Contributor Author

spevans commented Aug 17, 2017

Done, although I think some more test coverage will be needed at some point.

thread.start()

condition.lock()
if !started {
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't this a race on started? what if the thread takes a while to start up and the condition lock here is locked, then started will be false and the condition wont wait.

Copy link
Contributor

Choose a reason for hiding this comment

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

also this could potentially be waiting forever (if there was a problem with Thread), perhaps a semaphore with a timeout might be a good idea?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually just copied the test_threadStart() function. You are correct that with a delay starting up the thread, !started might be false and the .wait() will never happen.

There is a pthread_cond_timedwait() function to timeout a .wait() so I will see if I can find a way to incorporate that to handle the thread never calling .broadcast().

I notice that NSLock also has 2 time bounded lock functions that need implementing so I might see if I can implement and incorporate them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like NSCondition wait(until limit: Date) (using pthread_cond_timedwait()) has already been implemented so I will update to use that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the started flag and used wait(until:) (including fixing a bug with the implementation) and then tried some tests including not starting the thread or making the thread take too long, and the timeout worked fine under those conditions.

I gave it 10seconds timeout which should cover all cases except incredibly high server load and if thats the case -CI will have other problems anyway

- Fix NSCondition.wait(until:) to correctly calculate the timespec
  to use for the timeout.

- Fix test_threadStart() and test_mainThread() to eliminate the
  `started' flag which could have a race condition and instead
  use NSCondition.wait(until:) to timeout the test thread, in case
  it does not start up or call the broadcast() function.
@spevans
Copy link
Contributor Author

spevans commented Aug 21, 2017

@phausler Does this look ok now?

@phausler
Copy link
Contributor

@swift-ci please test

@spevans
Copy link
Contributor Author

spevans commented Aug 23, 2017

@swift-ci please test

1 similar comment
@ianpartridge
Copy link
Contributor

@swift-ci please test

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.

4 participants