1 // Copyright (c) 2012 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.
5 #include "gpu/command_buffer/service/sync_point_manager.h"
9 #include "base/logging.h"
10 #include "base/rand_util.h"
11 #include "base/sequence_checker.h"
15 static const int kMaxSyncBase
= INT_MAX
;
17 SyncPointManager::SyncPointManager(bool allow_threaded_wait
)
18 : allow_threaded_wait_(allow_threaded_wait
),
19 // To reduce the risk that a sync point created in a previous GPU process
20 // will be in flight in the next GPU process, randomize the starting sync
21 // point number. http://crbug.com/373452
22 next_sync_point_(base::RandInt(1, kMaxSyncBase
)),
23 retire_cond_var_(&lock_
) {}
25 SyncPointManager::~SyncPointManager() {}
27 uint32
SyncPointManager::GenerateSyncPoint() {
28 base::AutoLock
lock(lock_
);
29 uint32 sync_point
= next_sync_point_
++;
30 // When an integer overflow occurs, don't return 0.
32 sync_point
= next_sync_point_
++;
34 // Note: wrapping would take days for a buggy/compromized renderer that would
35 // insert sync points in a loop, but if that were to happen, better explicitly
36 // crash the GPU process than risk worse.
37 // For normal operation (at most a few per frame), it would take ~a year to
39 CHECK(sync_point_map_
.find(sync_point
) == sync_point_map_
.end());
40 sync_point_map_
.insert(std::make_pair(sync_point
, ClosureList()));
44 void SyncPointManager::RetireSyncPoint(uint32 sync_point
) {
47 base::AutoLock
lock(lock_
);
48 SyncPointMap::iterator it
= sync_point_map_
.find(sync_point
);
49 if (it
== sync_point_map_
.end()) {
50 LOG(ERROR
) << "Attempted to retire sync point that"
51 " didn't exist or was already retired.";
54 list
.swap(it
->second
);
55 sync_point_map_
.erase(it
);
56 if (allow_threaded_wait_
)
57 retire_cond_var_
.Broadcast();
59 for (ClosureList::iterator i
= list
.begin(); i
!= list
.end(); ++i
)
63 void SyncPointManager::AddSyncPointCallback(uint32 sync_point
,
64 const base::Closure
& callback
) {
66 base::AutoLock
lock(lock_
);
67 SyncPointMap::iterator it
= sync_point_map_
.find(sync_point
);
68 if (it
!= sync_point_map_
.end()) {
69 it
->second
.push_back(callback
);
76 bool SyncPointManager::IsSyncPointRetired(uint32 sync_point
) {
77 base::AutoLock
lock(lock_
);
78 return IsSyncPointRetiredLocked(sync_point
);
81 void SyncPointManager::WaitSyncPoint(uint32 sync_point
) {
82 if (!allow_threaded_wait_
) {
83 DCHECK(IsSyncPointRetired(sync_point
));
87 base::AutoLock
lock(lock_
);
88 while (!IsSyncPointRetiredLocked(sync_point
)) {
89 retire_cond_var_
.Wait();
93 bool SyncPointManager::IsSyncPointRetiredLocked(uint32 sync_point
) {
94 lock_
.AssertAcquired();
95 return sync_point_map_
.find(sync_point
) == sync_point_map_
.end();