-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,9 @@ class TestThread : XCTestCase { | |
("test_currentThread", test_currentThread ), | ||
("test_threadStart", test_threadStart), | ||
("test_threadName", test_threadName), | ||
("test_mainThread", test_mainThread), | ||
("test_callStackSymbols", test_callStackSymbols), | ||
("test_callStackReurnAddresses", test_callStackReturnAddresses), | ||
] | ||
} | ||
|
||
|
@@ -33,6 +36,7 @@ class TestThread : XCTestCase { | |
XCTAssertNotNil(thread1) | ||
XCTAssertNotNil(thread2) | ||
XCTAssertEqual(thread1, thread2) | ||
XCTAssertEqual(thread1, Thread.mainThread) | ||
} | ||
|
||
func test_threadStart() { | ||
|
@@ -84,4 +88,44 @@ class TestThread : XCTestCase { | |
XCTAssertEqual(thread3.name, "Thread3") | ||
XCTAssertNotEqual(thread3.name, getPThreadName()) | ||
} | ||
|
||
func test_mainThread() { | ||
XCTAssertTrue(Thread.isMainThread) | ||
let t = Thread.mainThread | ||
XCTAssertTrue(t.isMainThread) | ||
let c = Thread.current | ||
XCTAssertTrue(c.isMainThread) | ||
XCTAssertTrue(c.isExecuting) | ||
XCTAssertTrue(c.isEqual(t)) | ||
|
||
var started = false | ||
let condition = NSCondition() | ||
let thread = Thread() { | ||
condition.lock() | ||
started = true | ||
XCTAssertFalse(Thread.isMainThread) | ||
XCTAssertFalse(Thread.mainThread == Thread.current) | ||
condition.broadcast() | ||
condition.unlock() | ||
} | ||
thread.start() | ||
|
||
condition.lock() | ||
if !started { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually just copied the There is a I notice that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the 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 |
||
condition.wait() | ||
} | ||
condition.unlock() | ||
} | ||
|
||
func test_callStackSymbols() { | ||
let symbols = Thread.callStackSymbols | ||
XCTAssertTrue(symbols.count > 0) | ||
XCTAssertTrue(symbols.count <= 128) | ||
} | ||
|
||
func test_callStackReturnAddresses() { | ||
let addresses = Thread.callStackReturnAddresses | ||
XCTAssertTrue(addresses.count > 0) | ||
XCTAssertTrue(addresses.count <= 128) | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.