RESOLVED INVALID 59984
Need a way to determine if the main thread has been initialized.
https://bugs.webkit.org/show_bug.cgi?id=59984
Summary Need a way to determine if the main thread has been initialized.
David Levin
Reported 2011-05-02 17:08:57 PDT
wtf may be used without initializing the main thread. Due to these cases, it would be useful to know if the main thread has been initialized before calling isMainThread.
Attachments
Patch (7.49 KB, patch)
2011-05-04 15:17 PDT, David Levin
no flags
Patch (6.93 KB, patch)
2011-05-04 18:05 PDT, David Levin
no flags
Patch (6.60 KB, patch)
2011-05-04 18:28 PDT, David Levin
no flags
Patch (7.07 KB, patch)
2011-05-05 11:10 PDT, David Levin
levin: review-
David Levin
Comment 1 2011-05-04 15:17:10 PDT
Darin Adler
Comment 2 2011-05-04 15:22:27 PDT
Comment on attachment 92327 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=92327&action=review > Source/JavaScriptCore/wtf/MainThread.cpp:253 > +bool internalIsMainThreadInitialized() > +{ > +#ifndef NDEBUG > + return mainThreadInitialized; > +#else > + return false; > +#endif > +} It doesn’t make sense to have this return false in NDEBUG builds. Maybe put a CRASH() here instead or in addition?
Alexey Proskuryakov
Comment 3 2011-05-04 16:15:47 PDT
Comment on attachment 92327 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=92327&action=review > Source/JavaScriptCore/wtf/MainThread.cpp:246 > +bool internalIsMainThreadInitialized() It's slightly inelegant to have a function that returns a boolean, but only "true" results can be trusted. Trusting a "false" results would be a race condition. Can this be changed into a check-style function that asserts internally?
Alexey Proskuryakov
Comment 4 2011-05-04 16:18:03 PDT
Comment on attachment 92327 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=92327&action=review > Source/JavaScriptCore/wtf/MainThread.cpp:248 > +#ifndef NDEBUG Another extremely minor nit. ASSERTION_ENABLED is not exactly the same as !NDEBUG. People sometimes want to build release mode with assertions - I know that I did that once, with a significant but practical amount of code changes.
David Levin
Comment 5 2011-05-04 16:36:00 PDT
(In reply to comment #3) > (From update of attachment 92327 [details]) > View in context: https://bugs.webkit.org/attachment.cgi?id=92327&action=review > > > Source/JavaScriptCore/wtf/MainThread.cpp:246 > > +bool internalIsMainThreadInitialized() > > It's slightly inelegant to have a function that returns a boolean, but only "true" results can be trusted. Per discussion in irc, will change the name to mayCallIsMainThread. (In reply to comment #4) > (From update of attachment 92327 [details]) > View in context: https://bugs.webkit.org/attachment.cgi?id=92327&action=review > > > Source/JavaScriptCore/wtf/MainThread.cpp:248 > > +#ifndef NDEBUG > > Another extremely minor nit. ASSERTION_ENABLED is not exactly the same as !NDEBUG. People sometimes want to build release mode with assertions - I know that I did that once, with a significant but practical amount of code changes. Thanks. Will fix.
David Levin
Comment 6 2011-05-04 17:04:51 PDT
Comment on attachment 92327 [details] Patch Changing to "isKnownAsMainThread" which will change the code some.
David Levin
Comment 7 2011-05-04 18:05:36 PDT
David Levin
Comment 8 2011-05-04 18:28:59 PDT
David Levin
Comment 9 2011-05-04 23:42:03 PDT
Comment on attachment 92354 [details] Patch I should add a similar atomicIncrement to initializeMainThreadToProcessMainThreadOnce().
David Levin
Comment 10 2011-05-05 11:10:56 PDT
Alexey Proskuryakov
Comment 11 2011-05-05 12:15:14 PDT
Comment on attachment 92433 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=92433&action=review > Source/JavaScriptCore/ChangeLog:13 > + (WTF::initializeMainThreadOnce): Ditto (for OSX). > + (WTF::initializeMainThreadToProcessMainThreadOnce): Ditto (for OSX). "OS X". > Source/JavaScriptCore/wtf/MainThread.cpp:76 > +static int mainThreadInitialized; It's quite confusing to have both mainThreadInitialized and initializedMainThread in this code. > Source/JavaScriptCore/wtf/MainThread.cpp:113 > + // Flush all writes and set mainThreadInitialized to 1 to indicate that the initialization has been done. > + atomicIncrement(&mainThreadInitialized); Not sure if the fact that setting a variable named mainThreadInitialized to 1 indicates that the initialization has been done needs a comment. That said, I can see why the use of atomicIncrement to insert a memory barrier needs some explanation. But after an IRC discussion, I also don't think that it's doing the job. There is no way to guarantee memory consistency without a memory barrier on a thread that checks mainThreadInitialized - it's not enough to use on the thread that changes it. Also, I'm not sure if we want or do guarantee a full memory barrier in atomicIncrement implementations. Clearly, it's not required to increment one integer. > Source/JavaScriptCore/wtf/MainThread.cpp:257 > + CRASH(); This probably need a comment.
David Levin
Comment 12 2011-07-12 14:52:03 PDT
Instead of doing this, I'm looking into making current thread much faster. currentThread takes about 3x the time of isMainThread in debug and more than 2x in release, which is the reason for preferring to use isMainThread when possible. On the other hand, if currentThread were faster than it is now, then this issue may go away.
Note You need to log in before you can comment on or make changes to this bug.