• Simeon Anfinrud's avatar
    [Chromecast] Type-safe static sequences. · fb5abd41
    Simeon Anfinrud authored
    With this, you can make the compiler ensure, statically, that
    the methods of your class always run on the same sequence.
    This is vastly safer than runtime checks like SequenceChecker
    since it will fail to compile if you violate the requirement.
    
    (It wouldn't be C++ if there weren't hacky ways around this, but
    any attempt to trick the compiler into allowing a sequence
    violation should look pretty obvious to reviewers.)
    
    To use this, declare a struct that inherits from StaticSequence.
    This struct will automatically declare a Key class that can only
    be constructed inside the StaticSequence's PostTask function. To
    force users to go through that StaticSequence's PostTask() to
    call your method, simply declare a reference to the Key object
    as the last parameter to the method.
    
    This also includes a wrapper template called Sequenced, similar
    to base::SequenceBound, but with the TaskRunner known at compile
    time rather than runtime. This can be used to add an extra level
    of safety over runtime checks like
    DCHECK_CALLED_ON_VALID_SEQUENCE or
    TaskRunner::RunsTasksInCurrentThread. This wrapper ensures the
    wrapped object is destroyed on the correct sequence, essentially
    turning any sequence-affine object to a thread-safe object.
    
    This will be made even more useful once base::PostTask() returns
    a base::Promise, as it will work with methods that return values
    as well as void methods.
    
    Bug: None
    Test: cast_base_unittests
    Change-Id: Ic408e343bc084c19c4c6f9e983b343b98502daf2
    Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1863549Reviewed-by: default avatarKenneth MacKay <kmackay@chromium.org>
    Reviewed-by: default avatarYuchen Liu <yucliu@chromium.org>
    Commit-Queue: Simeon Anfinrud <sanfin@chromium.org>
    Cr-Commit-Position: refs/heads/master@{#711007}
    fb5abd41
BUILD.gn 882 Bytes