forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
shared_thread_merger.h
78 lines (57 loc) · 2.6 KB
/
shared_thread_merger.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_FML_SHARED_THREAD_MERGER_H_
#define FLUTTER_FML_SHARED_THREAD_MERGER_H_
#include <condition_variable>
#include <mutex>
#include "flutter/fml/macros.h"
#include "flutter/fml/memory/ref_counted.h"
#include "flutter/fml/message_loop_task_queues.h"
namespace fml {
class RasterThreadMerger;
typedef void* RasterThreadMergerId;
/// Instance of this class is shared between multiple |RasterThreadMerger|
/// instances, Most of the callings from |RasterThreadMerger| will be redirected
/// to this class with one more caller parameter.
class SharedThreadMerger
: public fml::RefCountedThreadSafe<SharedThreadMerger> {
public:
SharedThreadMerger(TaskQueueId owner, TaskQueueId subsumed);
// It's called by |RasterThreadMerger::MergeWithLease|.
// See the doc of |RasterThreadMerger::MergeWithLease|.
bool MergeWithLease(RasterThreadMergerId caller, size_t lease_term);
// It's called by |RasterThreadMerger::UnMergeNowIfLastOne|.
// See the doc of |RasterThreadMerger::UnMergeNowIfLastOne|.
bool UnMergeNowIfLastOne(RasterThreadMergerId caller);
// It's called by |RasterThreadMerger::ExtendLeaseTo|.
// See the doc of |RasterThreadMerger::ExtendLeaseTo|.
void ExtendLeaseTo(RasterThreadMergerId caller, size_t lease_term);
// It's called by |RasterThreadMerger::IsMergedUnSafe|.
// See the doc of |RasterThreadMerger::IsMergedUnSafe|.
bool IsMergedUnSafe() const;
// It's called by |RasterThreadMerger::IsEnabledUnSafe|.
// See the doc of |RasterThreadMerger::IsEnabledUnSafe|.
bool IsEnabledUnSafe() const;
// It's called by |RasterThreadMerger::Enable| or
// |RasterThreadMerger::Disable|.
void SetEnabledUnSafe(bool enabled);
// It's called by |RasterThreadMerger::DecrementLease|.
// See the doc of |RasterThreadMerger::DecrementLease|.
bool DecrementLease(RasterThreadMergerId caller);
private:
fml::TaskQueueId owner_;
fml::TaskQueueId subsumed_;
fml::MessageLoopTaskQueues* task_queues_;
std::mutex mutex_;
bool enabled_;
/// The |MergeWithLease| or |ExtendLeaseTo| method will record the caller
/// into this lease_term_by_caller_ map, |UnMergeNowIfLastOne|
/// method will remove the caller from this lease_term_by_caller_.
std::map<RasterThreadMergerId, std::atomic_size_t> lease_term_by_caller_;
bool IsAllLeaseTermsZeroUnSafe() const;
bool UnMergeNowUnSafe();
FML_DISALLOW_COPY_AND_ASSIGN(SharedThreadMerger);
};
} // namespace fml
#endif // FLUTTER_FML_SHARED_THREAD_MERGER_H_