| 128 | } |
| 129 | |
| 130 | void PriorityQueue::TryEnqueueMany(const Tuple& tuple, OpKernelContext* ctx, |
| 131 | DoneCallback callback) { |
| 132 | const int64 batch_size = tuple[0].dim_size(0); |
| 133 | if (batch_size == 0) { |
| 134 | callback(); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | CancellationManager* cm = ctx->cancellation_manager(); |
| 139 | CancellationToken token = cm->get_cancellation_token(); |
| 140 | bool already_cancelled; |
| 141 | { |
| 142 | mutex_lock l(mu_); |
| 143 | already_cancelled = !cm->RegisterCallback( |
| 144 | token, [this, cm, token]() { Cancel(kEnqueue, cm, token); }); |
| 145 | if (!already_cancelled) { |
| 146 | enqueue_attempts_.emplace_back( |
| 147 | batch_size, callback, ctx, cm, token, |
| 148 | [tuple, this, ctx](Attempt* attempt) EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
| 149 | if (closed_) { |
| 150 | attempt->context->SetStatus( |
| 151 | errors::Cancelled("PriorityQueue '", name_, "' is closed.")); |
| 152 | return kComplete; |
| 153 | } |
| 154 | RunResult result = kNoProgress; |
| 155 | while (queues_[0].size() < static_cast<size_t>(capacity_)) { |
| 156 | result = kProgress; |
| 157 | const int index = |
| 158 | tuple[0].dim_size(0) - attempt->elements_requested; |
| 159 | |
| 160 | PersistentTensor priority_element; |
| 161 | attempt->context->SetStatus(GetElementComponentFromBatch( |
| 162 | tuple, index, 0, attempt->context, &priority_element)); |
| 163 | if (!attempt->context->status().ok()) return kComplete; |
| 164 | Tensor* priority_tensor = priority_element.AccessTensor(ctx); |
| 165 | if (!TensorShapeUtils::IsScalar(priority_tensor->shape())) { |
| 166 | attempt->context->SetStatus(errors::InvalidArgument( |
| 167 | "Expected the priority element to be a scalar, but " |
| 168 | "received shape: ", |
| 169 | priority_tensor->shape().DebugString())); |
| 170 | return kComplete; |
| 171 | } |
| 172 | const int64 priority = priority_tensor->scalar<int64>()(); |
| 173 | for (int i = 0; i < num_components(); ++i) { |
| 174 | PersistentTensor element; |
| 175 | attempt->context->SetStatus(GetElementComponentFromBatch( |
| 176 | tuple, index, i, attempt->context, &element)); |
| 177 | if (!attempt->context->status().ok()) return kComplete; |
| 178 | queues_[i].emplace(priority, element); |
| 179 | } |
| 180 | --attempt->elements_requested; |
| 181 | if (attempt->elements_requested == 0) { |
| 182 | return kComplete; |
| 183 | } |
| 184 | } |
| 185 | return result; |
| 186 | }); |
| 187 | } |
nothing calls this directly
no test coverage detected