Home | History | Annotate | Download | only in service
      1 /* Copyright 2017 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_COMPILER_XLA_POOL_H_
     17 #define TENSORFLOW_COMPILER_XLA_POOL_H_
     18 
     19 #include <functional>
     20 #include <vector>
     21 
     22 #include "tensorflow/compiler/xla/ptr_util.h"
     23 #include "tensorflow/core/platform/mutex.h"
     24 
     25 namespace xla {
     26 
     27 // Pool of values, which are created as needed and destroyed when the `Pool` is
     28 // destroyed
     29 template <typename T>
     30 class Pool {
     31  public:
     32   struct Deleter {
     33     void operator()(T* ptr) { pool->Deallocate(ptr); }
     34 
     35     Pool<T>* pool;
     36   };
     37 
     38   // A pointer to a taken element of a `Pool` which returns it to the pool on
     39   // destruction
     40   using SmartPtr = std::unique_ptr<T, Deleter>;
     41 
     42   // Constructs a `Pool` with given factory function, which need not be
     43   // thread-safe.
     44   explicit Pool(std::function<std::unique_ptr<T>()> factory)
     45       : factory_(factory) {}
     46 
     47   explicit Pool() : Pool([]() { return MakeUnique<T>(); }) {}
     48 
     49   // Returns a pointer to a value in the pool, creating a new value if none is
     50   // free. The returned smart pointer returns the element to the pool on
     51   // destruction.
     52   //
     53   // This method is thread-safe.
     54   SmartPtr Allocate() {
     55     tensorflow::mutex_lock lock(mu_);
     56     T* ptr;
     57     if (!xs_.empty()) {
     58       ptr = std::move(xs_.back()).release();
     59       xs_.pop_back();
     60     } else {
     61       ptr = factory_().release();
     62     }
     63     Deleter del = {this};
     64     return std::unique_ptr<T, Deleter>(ptr, del);
     65   }
     66 
     67  private:
     68   // Puts a pointer to a value back into the pool, leaving it free for future
     69   // use.
     70   //
     71   // This method is thread-safe.
     72   void Deallocate(T* ptr) {
     73     tensorflow::mutex_lock lock(mu_);
     74     xs_.push_back(std::unique_ptr<T>(ptr));
     75   }
     76 
     77   const std::function<std::unique_ptr<T>()> factory_ GUARDED_BY(mu_);
     78   std::vector<std::unique_ptr<T>> xs_ GUARDED_BY(mu_);
     79   tensorflow::mutex mu_;
     80 };
     81 
     82 }  // namespace xla
     83 
     84 #endif  // TENSORFLOW_COMPILER_XLA_POOL_H_
     85