Bug 159384 - WTF::Lock should be fair eventually
Summary: WTF::Lock should be fair eventually
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Template Framework (show other bugs)
Version: WebKit Nightly Build
Hardware: All All
: P2 Normal
Assignee: Filip Pizlo
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-07-01 21:09 PDT by Filip Pizlo
Modified: 2016-07-18 11:34 PDT (History)
13 users (show)

See Also:


Attachments
maybe (12.55 KB, patch)
2016-07-01 21:09 PDT, Filip Pizlo
no flags Details | Formatted Diff | Diff
OMG it totally works! (26.30 KB, patch)
2016-07-02 13:42 PDT, Filip Pizlo
no flags Details | Formatted Diff | Diff
the patch (33.92 KB, patch)
2016-07-02 14:31 PDT, Filip Pizlo
no flags Details | Formatted Diff | Diff
the patch (35.16 KB, patch)
2016-07-02 14:45 PDT, Filip Pizlo
buildbot: commit-queue-
Details | Formatted Diff | Diff
Archive of layout-test-results from ews113 for mac-yosemite (1.46 MB, application/zip)
2016-07-02 15:54 PDT, Build Bot
no flags Details
the patch (35.47 KB, patch)
2016-07-02 16:49 PDT, Filip Pizlo
no flags Details | Formatted Diff | Diff
the patch (36.12 KB, patch)
2016-07-03 10:50 PDT, Filip Pizlo
ggaren: review+
Details | Formatted Diff | Diff
patch for landing (37.52 KB, patch)
2016-07-05 12:08 PDT, Filip Pizlo
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Filip Pizlo 2016-07-01 21:09:08 PDT
For critical sections that are held for a long time, it makes sense to have a completely fair lock, since the cost of forced context switches is small by comparison to the cost of waiting on a thread that barged into the lock.
Comment 1 Filip Pizlo 2016-07-01 21:09:41 PDT
Created attachment 282637 [details]
maybe

I haven't even compiled this yet.
Comment 2 Filip Pizlo 2016-07-02 13:42:46 PDT
Created attachment 282642 [details]
OMG it totally works!
Comment 3 Filip Pizlo 2016-07-02 14:31:17 PDT
Created attachment 282643 [details]
the patch
Comment 4 WebKit Commit Bot 2016-07-02 14:33:26 PDT
Attachment 282643 [details] did not pass style-queue:


ERROR: Source/WTF/wtf/ParkingLot.cpp:622:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WTF/wtf/ParkingLot.cpp:659:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WTF/wtf/ParkingLot.cpp:698:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WTF/wtf/ParkingLot.cpp:738:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
Total errors found: 4 in 8 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 5 Filip Pizlo 2016-07-02 14:45:36 PDT
Created attachment 282644 [details]
the patch
Comment 6 WebKit Commit Bot 2016-07-02 14:46:10 PDT
Attachment 282644 [details] did not pass style-queue:


ERROR: Source/WTF/wtf/ParkingLot.cpp:622:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WTF/wtf/ParkingLot.cpp:659:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WTF/wtf/ParkingLot.cpp:698:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WTF/wtf/ParkingLot.cpp:738:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
Total errors found: 4 in 10 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 7 Build Bot 2016-07-02 15:54:32 PDT
Comment on attachment 282644 [details]
the patch

Attachment 282644 [details] did not pass mac-debug-ews (mac):
Output: http://webkit-queues.webkit.org/results/1615273

Number of test failures exceeded the failure limit.
Comment 8 Build Bot 2016-07-02 15:54:36 PDT
Created attachment 282645 [details]
Archive of layout-test-results from ews113 for mac-yosemite

The attached test failures were seen while running run-webkit-tests on the mac-debug-ews.
Bot: ews113  Port: mac-yosemite  Platform: Mac OS X 10.10.5
Comment 9 Filip Pizlo 2016-07-02 16:49:54 PDT
Created attachment 282646 [details]
the patch

Turns out I had an assertion exactly backwards.
Comment 10 WebKit Commit Bot 2016-07-02 16:52:22 PDT
Attachment 282646 [details] did not pass style-queue:


ERROR: Source/WTF/wtf/ParkingLot.cpp:622:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WTF/wtf/ParkingLot.cpp:666:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WTF/wtf/ParkingLot.cpp:705:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WTF/wtf/ParkingLot.cpp:745:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
Total errors found: 4 in 10 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 11 Filip Pizlo 2016-07-03 10:50:21 PDT
Created attachment 282661 [details]
the patch
Comment 12 WebKit Commit Bot 2016-07-03 10:51:52 PDT
Attachment 282661 [details] did not pass style-queue:


ERROR: Source/WTF/wtf/ParkingLot.cpp:622:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WTF/wtf/ParkingLot.cpp:666:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WTF/wtf/ParkingLot.cpp:705:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WTF/wtf/ParkingLot.cpp:745:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
Total errors found: 4 in 10 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 13 Geoffrey Garen 2016-07-05 10:36:21 PDT
Comment on attachment 282661 [details]
the patch

View in context: https://bugs.webkit.org/attachment.cgi?id=282661&action=review

This is pretty cool. Randomness wins again!

> Source/WTF/wtf/Lock.cpp:121
> +                    return 1;

It's nice that token establishes a separation of concerns between ParkingLot and Lock, but it's also a bummer for readability that we have to be so abstract (0 vs 1) about a concept that has only one use -- to determine if we need to lock or not.

One way to solve this is to use an old-school enum inside Lock, which can auto-convert to int:

enum LockToken : intptr_t {
	DoesNotHoldLock,
	HoldsLock
};

Or maybe a class inside Lock:

class LockToken {
public:
	LockToken(bool holdsLock) : m_holdsLock(holdsLock) { }
	operator intptr_t() { return m_holdsLock; }
        bool holdsLock() { return m_holdsLock; }

private:
	bool m_holdsLock;
};

> Source/WTF/wtf/Lock.h:134
> +    enum Fairness {

Let's use enum class. The type safety and namespacing are nice.
Comment 14 Filip Pizlo 2016-07-05 11:09:53 PDT
(In reply to comment #13)
> Comment on attachment 282661 [details]
> the patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=282661&action=review
> 
> This is pretty cool. Randomness wins again!
> 
> > Source/WTF/wtf/Lock.cpp:121
> > +                    return 1;
> 
> It's nice that token establishes a separation of concerns between ParkingLot
> and Lock, but it's also a bummer for readability that we have to be so
> abstract (0 vs 1) about a concept that has only one use -- to determine if
> we need to lock or not.

You're right, this should be an enum!

> 
> One way to solve this is to use an old-school enum inside Lock, which can
> auto-convert to int:
> 
> enum LockToken : intptr_t {
> 	DoesNotHoldLock,
> 	HoldsLock
> };

Or we could use the Fairness enum.

> 
> Or maybe a class inside Lock:
> 
> class LockToken {
> public:
> 	LockToken(bool holdsLock) : m_holdsLock(holdsLock) { }
> 	operator intptr_t() { return m_holdsLock; }
>         bool holdsLock() { return m_holdsLock; }
> 
> private:
> 	bool m_holdsLock;
> };
> 
> > Source/WTF/wtf/Lock.h:134
> > +    enum Fairness {
> 
> Let's use enum class. The type safety and namespacing are nice.

OK.
Comment 15 Filip Pizlo 2016-07-05 11:23:07 PDT
I ended up going with normal enums, not enum classes, because this is not valid:

enum class Thing : intptr_t { Foo, Bar }

[&] (things) -> intptr_t {
    return Thing::Foo; // Compiler error, it wants a cast here!!
}

Also, for the Fairness enum, it felt really awkward to say Fairness::Fair and Fairness::Unfair.  I don't know if we have a style rule for this, but I've been using the rule that plain enums are better if prefixing the enum class name would be tautological.  It is in this case, since the enums are already encapsulated inside the protected namespace of WTF::LockBase and the names of the members tell you everything you need to know.
Comment 16 Filip Pizlo 2016-07-05 12:08:05 PDT
Created attachment 282802 [details]
patch for landing
Comment 17 WebKit Commit Bot 2016-07-05 12:10:00 PDT
Attachment 282802 [details] did not pass style-queue:


ERROR: Source/WTF/wtf/ParkingLot.cpp:622:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WTF/wtf/ParkingLot.cpp:666:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WTF/wtf/ParkingLot.cpp:705:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WTF/wtf/ParkingLot.cpp:745:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
Total errors found: 4 in 10 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 18 Filip Pizlo 2016-07-18 11:34:54 PDT
Landed in https://trac.webkit.org/changeset/203350