| 39 | } |
| 40 | |
| 41 | Status InitializableLookupTable::Initialize(InitTableIterator& iter) { |
| 42 | if (!iter.Valid()) { |
| 43 | return iter.status(); |
| 44 | } |
| 45 | TF_RETURN_IF_ERROR( |
| 46 | CheckKeyAndValueTensorsForInsert(iter.keys(), iter.values())); |
| 47 | |
| 48 | mutex_lock l(mu_); |
| 49 | if (is_initialized()) { |
| 50 | bool result; |
| 51 | TF_RETURN_IF_ERROR(AreEntriesSame(iter, &result)); |
| 52 | // If the table is already initialized, we make sure that the entries in the |
| 53 | // table are the same that we want to initialize the table with. |
| 54 | if (!result) { |
| 55 | return errors::FailedPrecondition( |
| 56 | "Table was already initialized with " |
| 57 | "different data."); |
| 58 | } else { |
| 59 | return Status::OK(); |
| 60 | } |
| 61 | } |
| 62 | TF_RETURN_IF_ERROR(DoLazyPrepare([&iter]() { return iter.total_size(); })); |
| 63 | while (iter.Valid()) { |
| 64 | TF_RETURN_IF_ERROR(DoInsert(iter.keys(), iter.values())); |
| 65 | iter.Next(); |
| 66 | } |
| 67 | if (!errors::IsOutOfRange(iter.status())) { |
| 68 | return iter.status(); |
| 69 | } |
| 70 | |
| 71 | // Prevent compiler/memory reordering of is_initialized and |
| 72 | // the initialization itself. |
| 73 | std::atomic_thread_fence(std::memory_order_release); |
| 74 | is_initialized_ = true; |
| 75 | return Status::OK(); |
| 76 | } |
| 77 | |
| 78 | Status InitializableLookupTable::AreEntriesSame(const InitTableIterator& iter, |
| 79 | bool* result) { |
nothing calls this directly
no test coverage detected