-
Notifications
You must be signed in to change notification settings - Fork 103
/
reference_counted_future_impl.cc
736 lines (622 loc) · 25.4 KB
/
reference_counted_future_impl.cc
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*
* Copyright 2016 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "app/src/reference_counted_future_impl.h"
#include <algorithm>
#include <cstdint>
#include <string>
#include "app/src/assert.h"
#include "app/src/include/firebase/future.h"
#include "app/src/log.h"
#include "app/src/mutex.h"
#include "app/src/intrusive_list.h"
// Set this to 1 to enable verbose logging in this module.
#if !defined(FIREBASE_FUTURE_TRACE_ENABLE)
#define FIREBASE_FUTURE_TRACE_ENABLE 0
#endif // !defined(FIREBASE_FUTURE_TRACE_ENABLE)
#if FIREBASE_FUTURE_TRACE_ENABLE
#define FIREBASE_FUTURE_TRACE(...) LogDebug(__VA_ARGS__)
#else
#define FIREBASE_FUTURE_TRACE(...)
#endif // FIREBASE_FUTURE_TRACE_ENABLE
#if !defined(FIREBASE_NAMESPACE)
#define FIREBASE_NAMESPACE firebase
#endif
namespace FIREBASE_NAMESPACE {
// See warning at the top of FutureBase's declaration for details.
static_assert(sizeof(FutureBase) == sizeof(Future<int>),
"Future should not introduce virtual functions or data members.");
typedef void DataDeleteFn(void* data_to_delete);
typedef std::pair<FutureHandle, FutureBackingData*> BackingPair;
namespace {
// This class manages proxies to a Future.
// The goal is to allow LastResult to return a proxy to a Future, so that we
// don't have to duplicate the asynchronous call, but still have the Futures
// be independent from a user's perspective.
// - The subject Future is the Future that existed first, owns the data and
// listens to the result of the asynchronous system call.
// It must stay alive as long as there are clients. (For the data.)
// - There can be multiple client Futures, which complete when the subject
// completes. They refer to the same data as the subject and they each have
// their own completion callback.
// This class manages the link between the two.
class FutureProxyManager {
public:
FutureProxyManager(ReferenceCountedFutureImpl* api, FutureHandle subject)
: api_(api), subject_(subject) {}
void RegisterClient(FutureHandle handle) {
// We create one reference per client to the Future.
// This way the ReferenceCountedFutureImpl will do the right thing if one
// thread tries to unregister the last client while adding a new one.
api_->ReferenceFuture(subject_);
clients_.push_back(handle);
}
struct UnregisterData {
UnregisterData(FutureProxyManager* proxy, FutureHandle handle)
: proxy(proxy), handle(handle) {}
FutureProxyManager* proxy;
FutureHandle handle;
};
static void UnregisterCallback(void* data) {
UnregisterData* udata = static_cast<UnregisterData*>(data);
udata->proxy->UnregisterClient(udata->handle);
delete udata;
}
void UnregisterClient(FutureHandle handle) {
for (FutureHandle& h : clients_) {
if (h == handle) {
h = kInvalidFutureHandle;
// Release one reference. This can delete subject_, which in turn will
// delete `this`, as the subject owns the proxy. This is expected and
// fine; as long as we don't do anything after the ReleaseFuture call.
api_->ReleaseFuture(subject_);
break;
}
}
}
void CompleteClients(int error, const char* error_msg) {
for (const FutureHandle& h : clients_) {
if (h != kInvalidFutureHandle) {
api_->Complete(h, error, error_msg);
}
}
}
private:
std::vector<FutureHandle> clients_;
ReferenceCountedFutureImpl* api_;
// We need to keep the subject alive, as it owns us and the data.
FutureHandle subject_;
};
struct CompletionCallbackData {
// Pointers to the next and previous nodes in the list.
intrusive_list_node node;
// The function to call once the future is marked completed.
FutureBase::CompletionCallback completion_callback;
// The data to pass into `completion_callback`.
void* callback_user_data;
// If set, this function will be called to delete callback_user_data after the
// callback runs or the Future is destroyed.
void (*callback_user_data_delete_fn)(void*);
CompletionCallbackData(FutureBase::CompletionCallback callback,
void *user_data,
void (* user_data_delete_fn)(void *))
: completion_callback(callback), callback_user_data(user_data),
callback_user_data_delete_fn(user_data_delete_fn) {}
};
using intrusive_list_iterator =
intrusive_list<CompletionCallbackData>::iterator;
} // anonymous namespace
struct FutureBackingData {
// Create with type-specific data.
explicit FutureBackingData(void* data, DataDeleteFn* delete_data_fn)
: status(kFutureStatusPending),
error(0),
reference_count(0),
data(data),
data_delete_fn(delete_data_fn),
context_data(nullptr),
context_data_delete_fn(nullptr),
completion_single_callback(nullptr),
completion_multiple_callbacks(&CompletionCallbackData::node),
proxy(nullptr) {}
// Call the type-specific destructor on data.
// Also call the type-specific context data destructor on context_data.
// Also deallocate the completion_callbacks and proxy.
~FutureBackingData();
// Clear out any existing callback functions,
// and deallocate the memory associated with them.
void ClearExistingCallbacks();
// Remove the specified callback from the list of callbacks,
// and deallocate the memory associated with it.
intrusive_list_iterator ClearCallbackData(intrusive_list_iterator it);
// Deallocate the memory associated with a single callback.
static void ClearSingleCallbackData(CompletionCallbackData *data);
// Status of the asynchronous call.
FutureStatus status;
// Error reported upon call completion.
int error;
// Error string reported upon call completion.
std::string error_msg;
// Number of outstanding futures referencing this asynchronous call.
// When this count reaches zero, this class is removed from the `backings_`
// map and deleted.
uint32_t reference_count;
// The call-specific result that is returned in Future<T>,
// or nullptr if return value is Future<void>.
void* data;
// A function that can deletes data by calling its destructor.
DataDeleteFn* data_delete_fn;
// Temporary context data used to produce the result returned in Future<T>.
// E.g., if the result of Future<T> depends on the results of multiple async
// operations, context_data may be used to store objects that must exist for
// the lifetime of the Future.
void* context_data;
// A function that deletes the context_data.
DataDeleteFn* context_data_delete_fn;
// A single function to call when the future completes.
// Dynamically allocated with 'new'.
CompletionCallbackData *completion_single_callback;
// A list of functions to call when the future completes.
// Note that the elements of this list are themselves dynamically allocated
// using 'new', and must be deleted when removing them from the list.
// (We can't use a list of pointers here, because intrusive_list requires
// that the list element type must contain an instrusive_list_node.)
intrusive_list<CompletionCallbackData> completion_multiple_callbacks;
FutureProxyManager* proxy;
};
FutureBackingData::~FutureBackingData() {
ClearExistingCallbacks();
if (data != nullptr) {
FIREBASE_ASSERT(data_delete_fn != nullptr);
data_delete_fn(data);
data = nullptr;
}
if (context_data != nullptr) {
FIREBASE_ASSERT(context_data_delete_fn != nullptr);
context_data_delete_fn(context_data);
context_data = nullptr;
}
delete proxy;
}
void FutureBackingData::ClearExistingCallbacks() {
// Clear out any existing callbacks.
ClearSingleCallbackData(completion_single_callback);
completion_single_callback = nullptr;
auto it = completion_multiple_callbacks.begin();
while (it != completion_multiple_callbacks.end()) {
it = ClearCallbackData(it);
}
}
intrusive_list_iterator FutureBackingData::ClearCallbackData(
intrusive_list_iterator it) {
CompletionCallbackData* data = &*it;
it = completion_multiple_callbacks.erase(it);
ClearSingleCallbackData(data);
return it;
}
void FutureBackingData::ClearSingleCallbackData(
CompletionCallbackData* data) {
if (data == nullptr) {
return;
}
if (data->callback_user_data_delete_fn != nullptr) {
data->callback_user_data_delete_fn(data->callback_user_data);
}
delete data;
}
namespace detail {
// Non-inline implementation of FutureApiInterface's virtual destructor
// to prevent its vtable being emitted in each translation unit.
FutureApiInterface::~FutureApiInterface() {}
} // namespace detail
const char ReferenceCountedFutureImpl::kErrorMessageFutureIsNoLongerValid[] =
"Invalid Future";
ReferenceCountedFutureImpl::~ReferenceCountedFutureImpl() {
// All futures should be released before we destroy ourselves.
for (size_t i = 0; i < last_results_.size(); ++i) {
last_results_[i].Release();
}
// Invalidate any externally-held futures.
cleanup_.CleanupAll();
// TODO(jsanmiya): Change this to use unique_ptr so deletion is automatic.
while (!backings_.empty()) {
auto it = backings_.begin();
LogWarning(
"Future with handle %d still exists though its backing API"
" 0x%X is being deleted. Please call Future::Release() before"
" deleting the backing API.",
it->first, static_cast<int>(reinterpret_cast<uintptr_t>(this)));
FutureBackingData* backing = it->second;
backings_.erase(it);
delete backing;
}
}
FutureHandle ReferenceCountedFutureImpl::AllocInternal(
int fn_idx, void* data, void (*delete_data_fn)(void* data_to_delete)) {
// Backings get deleted in ReleaseFuture() and ~ReferenceCountedFutureImpl().
FutureBackingData* backing = new FutureBackingData(data, delete_data_fn);
// Allocate a unique handle and insert the new backing into the map.
// Note that it's theoretically possible to have a handle collision if we
// allocate four billion more handles before releasing one. We ignore this
// possibility.
MutexLock lock(mutex_);
const FutureHandle handle = AllocHandle();
FIREBASE_FUTURE_TRACE("API: Allocated handle %d", handle);
backings_.insert(BackingPair(handle, backing));
// Update the most recent Future for this function.
if (0 <= fn_idx && fn_idx < static_cast<int>(last_results_.size())) {
FIREBASE_FUTURE_TRACE("API: Future handle %d (fn %d) --> %08x", handle,
fn_idx, &last_results_[fn_idx]);
last_results_[fn_idx] = FutureBase(this, handle);
}
FIREBASE_FUTURE_TRACE("API: Alloc complete.");
return handle;
}
void* ReferenceCountedFutureImpl::BackingData(FutureBackingData* backing) {
return backing->data;
}
void ReferenceCountedFutureImpl::SetBackingError(FutureBackingData* backing,
int error,
const char* error_msg) {
// This function is in the cpp instead of the header because
// FutureBackingData is only declared in the cpp.
backing->error = error;
backing->error_msg = error_msg == nullptr ? "" : error_msg;
}
void ReferenceCountedFutureImpl::CompleteProxy(FutureBackingData* backing) {
// This function is in the cpp instead of the header because
// FutureBackingData is only declared in the cpp.
if (backing->proxy) {
backing->proxy->CompleteClients(backing->error, backing->error_msg.c_str());
}
}
void ReferenceCountedFutureImpl::CompleteHandle(FutureHandle handle) {
FutureBackingData* backing = BackingFromHandle(handle);
// Ensure this Future is valid.
FIREBASE_ASSERT(backing != nullptr);
// Ensure we are only setting the status to complete once.
FIREBASE_ASSERT(backing->status != kFutureStatusComplete);
// Mark backing as complete.
backing->status = kFutureStatusComplete;
}
void ReferenceCountedFutureImpl::ReleaseMutexAndRunCallbacks(
FutureHandle handle) {
FutureBackingData* backing = BackingFromHandle(handle);
FIREBASE_ASSERT(backing != nullptr);
// Call the completion callbacks, if any have been registered,
// removing them from the list as we go.
if (backing->completion_single_callback != nullptr ||
!backing->completion_multiple_callbacks.empty()) {
FutureBase future_base(this, handle);
if (backing->completion_single_callback != nullptr) {
CompletionCallbackData* data = backing->completion_single_callback;
auto callback = data->completion_callback;
auto user_data = data->callback_user_data;
auto delete_fn = data->callback_user_data_delete_fn;
delete data;
backing->completion_single_callback = nullptr;
RunCallback(&future_base, callback, user_data, delete_fn);
}
while (!backing->completion_multiple_callbacks.empty()) {
CompletionCallbackData* data =
&backing->completion_multiple_callbacks.front();
auto callback = data->completion_callback;
auto user_data = data->callback_user_data;
auto delete_fn = data->callback_user_data_delete_fn;
backing->completion_multiple_callbacks.pop_front();
RunCallback(&future_base, callback, user_data, delete_fn);
delete data;
}
}
mutex_.Release();
}
void ReferenceCountedFutureImpl::RunCallback(
FutureBase *future_base, FutureBase::CompletionCallback callback,
void *user_data, void (*delete_fn)(void *)) {
// Make sure we're not deallocated while running the callback, because it
// would make `future_base` invalid.
is_running_callback_ = true;
// Release the lock, which is assumed to be obtained by the caller, before
// calling the callback.
mutex_.Release();
callback(*future_base, user_data);
mutex_.Acquire();
is_running_callback_ = false;
// Call this while holding lock, as we can't assume that the callback is
// thread-safe from the user's perspective.
if (delete_fn) {
delete_fn(user_data);
}
}
static void CleanupFuture(FutureBase* future) { future->Release(); }
void ReferenceCountedFutureImpl::RegisterFutureForCleanup(FutureBase* future) {
cleanup_.RegisterObject(future, CleanupFuture);
}
void ReferenceCountedFutureImpl::UnregisterFutureForCleanup(
FutureBase* future) {
cleanup_.UnregisterObject(future);
}
void ReferenceCountedFutureImpl::ReferenceFuture(FutureHandle handle) {
MutexLock lock(mutex_);
BackingFromHandle(handle)->reference_count++;
FIREBASE_FUTURE_TRACE("API: Reference handle %d, ref count %d", handle,
BackingFromHandle(handle)->reference_count);
}
void ReferenceCountedFutureImpl::ReleaseFuture(FutureHandle handle) {
MutexLock lock(mutex_);
FIREBASE_FUTURE_TRACE("API: Release future %d", (int)handle);
// Assert if the handle isn't registered.
// If a Future exists with a handle, then the backing should still exist for
// it, too.
auto it = backings_.find(handle);
FIREBASE_ASSERT(it != backings_.end());
// Decrement the reference count.
FutureBackingData* backing = it->second;
FIREBASE_ASSERT(backing->reference_count > 0);
backing->reference_count--;
FIREBASE_FUTURE_TRACE("API: Release handle %d, ref count %d", handle,
BackingFromHandle(handle)->reference_count);
// If asynchronous call is no longer referenced, delete the backing struct.
if (backing->reference_count == 0) {
backings_.erase(it);
delete backing;
backing = nullptr;
}
}
FutureStatus ReferenceCountedFutureImpl::GetFutureStatus(
FutureHandle handle) const {
MutexLock lock(mutex_);
const FutureBackingData* backing = BackingFromHandle(handle);
return backing == nullptr ? kFutureStatusInvalid : backing->status;
}
int ReferenceCountedFutureImpl::GetFutureError(FutureHandle handle) const {
MutexLock lock(mutex_);
const FutureBackingData* backing = BackingFromHandle(handle);
return backing == nullptr ? kErrorFutureIsNoLongerValid : backing->error;
}
const char* ReferenceCountedFutureImpl::GetFutureErrorMessage(
FutureHandle handle) const {
MutexLock lock(mutex_);
const FutureBackingData* backing = BackingFromHandle(handle);
return backing == nullptr ? kErrorMessageFutureIsNoLongerValid
: backing->error_msg.c_str();
}
const void* ReferenceCountedFutureImpl::GetFutureResult(
FutureHandle handle) const {
MutexLock lock(mutex_);
const FutureBackingData* backing = BackingFromHandle(handle);
return backing == nullptr || backing->status != kFutureStatusComplete
? nullptr
: backing->data;
}
FutureBackingData* ReferenceCountedFutureImpl::BackingFromHandle(
FutureHandle handle) {
MutexLock lock(mutex_);
auto it = backings_.find(handle);
return it == backings_.end() ? nullptr : it->second;
}
detail::CompletionCallbackHandle
ReferenceCountedFutureImpl::AddCompletionCallback(
FutureHandle handle, FutureBase::CompletionCallback callback,
void* user_data,
void (*user_data_delete_fn_ptr)(void *),
bool single_completion) {
// Record the callback parameters.
CompletionCallbackData *callback_data = new CompletionCallbackData(
callback, user_data, user_data_delete_fn_ptr);
// To handle the case where the future is already complete and we want to
// call the callback immediately, we acquire the mutex directly, so that
// it can be freed in ReleaseMutexAndRunCallbacks, prior to calling the
// callback.
mutex_.Acquire();
// If the handle is no longer valid, don't do anything.
FutureBackingData* backing = BackingFromHandle(handle);
if (backing == nullptr) {
mutex_.Release();
delete callback_data;
return detail::CompletionCallbackHandle();
}
if (single_completion) {
FutureBackingData::ClearSingleCallbackData(
backing->completion_single_callback);
backing->completion_single_callback = callback_data;
} else {
backing->completion_multiple_callbacks.push_back(*callback_data);
}
// If the future was already completed, call the callback now.
if (backing->status == kFutureStatusComplete) {
// ReleaseMutexAndRunCallbacks is in charge of releasing the mutex.
ReleaseMutexAndRunCallbacks(handle);
return detail::CompletionCallbackHandle();
} else {
mutex_.Release();
return detail::CompletionCallbackHandle(
callback, user_data, user_data_delete_fn_ptr);
}
}
class CompletionMatcher {
private:
CompletionCallbackData match_;
public:
CompletionMatcher(FutureBase::CompletionCallback callback,
void *user_data,
void (* user_data_delete_fn)(void *))
: match_(callback, user_data, user_data_delete_fn) {}
bool operator() (const CompletionCallbackData &data) const {
return data.completion_callback == match_.completion_callback &&
data.callback_user_data == match_.callback_user_data &&
data.callback_user_data_delete_fn ==
match_.callback_user_data_delete_fn;
}
};
void ReferenceCountedFutureImpl::RemoveCompletionCallback(
FutureHandle handle,
detail::CompletionCallbackHandle callback_handle) {
MutexLock lock(mutex_);
FutureBackingData* backing = BackingFromHandle(handle);
if (backing != nullptr) {
CompletionMatcher matches_callback_handle(
callback_handle.callback_,
callback_handle.user_data_,
callback_handle.user_data_delete_fn_);
if (backing->completion_single_callback != nullptr &&
matches_callback_handle(*backing->completion_single_callback)) {
FutureBackingData::ClearSingleCallbackData(
backing->completion_single_callback);
backing->completion_single_callback = nullptr;
}
auto it = backing->completion_multiple_callbacks.begin();
while (it != backing->completion_multiple_callbacks.end() &&
!matches_callback_handle(*it)) {
++it;
}
if (it != backing->completion_multiple_callbacks.end()) {
backing->ClearCallbackData(it);
}
}
}
#ifdef FIREBASE_USE_STD_FUNCTION
static void CallStdFunction(const FutureBase& future, void* function_void) {
if (function_void) {
std::function<void(const FutureBase&)>* function =
reinterpret_cast<std::function<void(const FutureBase&)>*>(
function_void);
(*function)(future);
}
}
static void DeleteStdFunction(void* function_void) {
if (function_void) {
std::function<void(const FutureBase&)>* function =
reinterpret_cast<std::function<void(const FutureBase&)>*>(
function_void);
delete function;
}
}
detail::CompletionCallbackHandle
ReferenceCountedFutureImpl::AddCompletionCallbackLambda(
FutureHandle handle, std::function<void(const FutureBase&)> callback,
bool single_completion) {
// Record the callback parameters.
CompletionCallbackData *completion_callback_data = new CompletionCallbackData(
/*callback=*/ CallStdFunction,
/*user_data=*/ new std::function<void(const FutureBase&)>(callback),
/*user_data_delete_fn=*/ DeleteStdFunction);
// To handle the case where the future is already complete and we want to
// call the callback immediately, we acquire the mutex directly, so that
// it can be freed in ReleaseMutexAndRunCallbacks, prior to calling the
// callback.
mutex_.Acquire();
// If the handle is no longer valid, don't do anything.
FutureBackingData* backing = BackingFromHandle(handle);
if (backing == nullptr) {
mutex_.Release();
delete completion_callback_data;
return detail::CompletionCallbackHandle();
}
if (single_completion) {
FutureBackingData::ClearSingleCallbackData(
backing->completion_single_callback);
backing->completion_single_callback = completion_callback_data;
} else {
backing->completion_multiple_callbacks.push_back(*completion_callback_data);
}
// If the future was already completed, call the callback(s) now.
if (backing->status == kFutureStatusComplete) {
// ReleaseMutexAndRunCallbacks is in charge of releasing the mutex.
ReleaseMutexAndRunCallbacks(handle);
return detail::CompletionCallbackHandle();
} else {
mutex_.Release();
return detail::CompletionCallbackHandle(
completion_callback_data->completion_callback,
completion_callback_data->callback_user_data,
completion_callback_data->callback_user_data_delete_fn);
}
}
#endif // FIREBASE_USE_STD_FUNCTION
bool ReferenceCountedFutureImpl::IsSafeToDelete() const {
MutexLock lock(mutex_);
// Check if any Futures we have are still pending.
for (auto i = backings_.begin(); i != backings_.end(); ++i) {
// If any Future is still pending, not safe to delete.
if (i->second->status == kFutureStatusPending) return false;
}
if (is_running_callback_) {
return false;
}
return true;
}
bool ReferenceCountedFutureImpl::IsReferencedExternally() const {
MutexLock lock(mutex_);
int total_references = 0;
int internal_references = 0;
for (auto i = backings_.begin(); i != backings_.end(); ++i) {
// Count the total number of references to all valid Futures.
total_references += i->second->reference_count;
}
for (int i = 0; i < last_results_.size(); i++) {
if (last_results_[i].status() != kFutureStatusInvalid) {
// If the status is not invalid, this entry is using up a reference.
// Count up the internal references.
internal_references++;
}
}
// If there are more references than the internal ones, someone is holding
// onto a Future.
return total_references > internal_references;
}
void ReferenceCountedFutureImpl::SetContextData(
FutureHandle handle, void* context_data,
void (*delete_context_data_fn)(void* data_to_delete)) {
MutexLock lock(mutex_);
// If the handle is no longer valid, don't do anything.
FutureBackingData* backing = BackingFromHandle(handle);
if (backing == nullptr) return;
FIREBASE_ASSERT((delete_context_data_fn != nullptr) ||
(context_data == nullptr));
backing->context_data = context_data;
backing->context_data_delete_fn = delete_context_data_fn;
}
// We need to have this define because FutureBase::GetHandle() is only
// available when build INTERNAL_EXPERIMENTAL.
#if defined(INTERNAL_EXPERIMENTAL)
FutureBase ReferenceCountedFutureImpl::LastResultProxy(int fn_idx) {
MutexLock lock(mutex_);
const FutureBase& future = last_results_[fn_idx];
// We only do this complicated dance if the Future is pending.
if (future.status() != kFutureStatusPending) {
return future;
}
// Get the subject backing and (if needed) allocate the ProxyManager.
FutureHandle handle = future.GetHandle();
FutureBackingData* backing = BackingFromHandle(handle);
if (!backing->proxy) {
backing->proxy = new FutureProxyManager(this, handle);
}
// Allocate the client backing. We reuse the subject data, with a noop
// delete function, because the subject owns the data.
FutureHandle client_handle =
AllocInternal(kNoFunctionIndex, backing->data, [](void*) {});
// Use the context data to inform the proxy manager when the client dies.
SetContextData(
client_handle,
new FutureProxyManager::UnregisterData(backing->proxy, client_handle),
FutureProxyManager::UnregisterCallback);
backing->proxy->RegisterClient(client_handle);
return FutureBase(this, client_handle);
}
#endif // defined(INTERNAL_EXPERIMENTAL)
// NOLINTNEXTLINE - allow namespace overridden
} // namespace FIREBASE_NAMESPACE