Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(201)

Side by Side Diff: base/sequence_token.cc

Issue 2165663003: TaskScheduler: Add SequenceToken and ScopedSetSequenceTokenForCurrentThread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: improve comment Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/sequence_token.h"
6
7 #include "base/atomic_sequence_num.h"
8 #include "base/lazy_instance.h"
9 #include "base/logging.h"
10 #include "base/threading/thread_local.h"
11
12 namespace base {
13 namespace internal {
14
15 namespace {
16
17 base::StaticAtomicSequenceNumber g_sequence_token_generator;
18
19 LazyInstance<ThreadLocalPointer<ScopedSetCurrentSequenceToken>>::Leaky
20 tls_current_sequence_token = LAZY_INSTANCE_INITIALIZER;
21
22 } // namespace
23
24 bool SequenceToken::operator==(const SequenceToken& other) const {
25 return token_ == other.token_;
26 }
27
28 bool SequenceToken::operator!=(const SequenceToken& other) const {
29 return !(*this == other);
30 }
31
32 bool SequenceToken::IsValid() const {
33 return token_ != kInvalidSequenceToken;
34 }
35
36 SequenceToken SequenceToken::Create() {
37 return SequenceToken(g_sequence_token_generator.GetNext());
38 }
39
40 SequenceToken SequenceToken::GetCurrent() {
41 const ScopedSetCurrentSequenceToken* current_sequence_token =
42 tls_current_sequence_token.Get().Get();
43 return current_sequence_token ? current_sequence_token->token_
44 : SequenceToken();
45 }
46
47 ScopedSetCurrentSequenceToken::ScopedSetCurrentSequenceToken(
48 const SequenceToken& token)
49 : token_(token) {
50 DCHECK(!tls_current_sequence_token.Get().Get());
51 tls_current_sequence_token.Get().Set(this);
52 }
53
54 ScopedSetCurrentSequenceToken::~ScopedSetCurrentSequenceToken() {
55 DCHECK_EQ(tls_current_sequence_token.Get().Get(), this);
56 tls_current_sequence_token.Get().Set(nullptr);
57 }
58
59 } // namespace internal
60 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698