• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CORE_COMMON_RUNTIME_ALLOCATOR_RETRY_H_
17 #define TENSORFLOW_CORE_COMMON_RUNTIME_ALLOCATOR_RETRY_H_
18 
19 #include "tensorflow/core/platform/env.h"
20 #include "tensorflow/core/platform/mutex.h"
21 #include "tensorflow/core/platform/types.h"
22 
23 namespace tensorflow {
24 
25 // A retrying wrapper for a memory allocator.
26 class AllocatorRetry {
27  public:
28   AllocatorRetry();
29 
30   // Call 'alloc_func' to obtain memory.  On first call,
31   // 'verbose_failure' will be false.  If return value is nullptr,
32   // then wait up to 'max_millis_to_wait' milliseconds, retrying each
33   // time a call to DeallocateRaw() is detected, until either a good
34   // pointer is returned or the deadline is exhausted.  If the
35   // deadline is exhausted, try one more time with 'verbose_failure'
36   // set to true.  The value returned is either the first good pointer
37   // obtained from 'alloc_func' or nullptr.
38   void* AllocateRaw(std::function<void*(size_t alignment, size_t num_bytes,
39                                         bool verbose_failure)>
40                         alloc_func,
41                     int max_millis_to_wait, size_t alignment, size_t bytes);
42 
43   // Called to notify clients that some memory was returned.
44   void NotifyDealloc();
45 
46  private:
47   Env* env_;
48   mutex mu_;
49   condition_variable memory_returned_;
50 };
51 
52 // Implementation details below
NotifyDealloc()53 inline void AllocatorRetry::NotifyDealloc() {
54   mutex_lock l(mu_);
55   memory_returned_.notify_all();
56 }
57 
58 }  // namespace tensorflow
59 
60 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_ALLOCATOR_RETRY_H_
61