| 199 | } |
| 200 | |
| 201 | void TryTakeMany(int num_elements, bool allow_small_batch, int64 timeout, |
| 202 | OpKernelContext* ctx, |
| 203 | const IndicesKeysValuesCallback& callback) { |
| 204 | int num_elements_to_deliver = num_elements; |
| 205 | { |
| 206 | mutex_lock lock(mu_); |
| 207 | if (closed_) { |
| 208 | int available_elements = ready_size(); |
| 209 | if (allow_small_batch) { |
| 210 | // We want to deliver a maximum of num_elements, if there are less |
| 211 | // elements available, we deliver at most the available_elements. If |
| 212 | // there are no |
| 213 | // elements available, a call to TryTakeMany should fail with |
| 214 | // OutOfRange. We trigger this error by setting the request here to 1. |
| 215 | num_elements_to_deliver = std::min(num_elements, available_elements); |
| 216 | } else { |
| 217 | // We're happy to wait for additional elements to be completed. |
| 218 | available_elements += incomplete_.size(); |
| 219 | } |
| 220 | // If there are 0 available elements or less elements than the |
| 221 | // number we can deliver, then we are done. |
| 222 | if (available_elements < std::max(num_elements_to_deliver, 1)) { |
| 223 | ctx->SetStatus(errors::OutOfRange( |
| 224 | "Barrier '", name_, "' is closed and has ", |
| 225 | "insufficient elements (requested ", num_elements_to_deliver, |
| 226 | ", total size ", available_elements, ")")); |
| 227 | callback(Tensor(DT_INT64), Tensor(DT_STRING), Tuple()); |
| 228 | return; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | ready_queue_->TryDequeueMany( |
| 234 | num_elements_to_deliver, ctx, allow_small_batch, |
| 235 | [this, ctx, callback](const Tuple& t) { |
| 236 | Tensor indices(DT_INT64); |
| 237 | Tensor keys(DT_STRING); |
| 238 | Tuple values; |
| 239 | |
| 240 | if (!ctx->status().ok()) { |
| 241 | callback(indices, keys, values); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | CHECK_EQ(t.size(), 2 + num_components()); |
| 246 | indices = t[0]; |
| 247 | keys = t[1]; |
| 248 | values.insert(values.begin(), t.begin() + 2, t.end()); |
| 249 | callback(indices, keys, values); |
| 250 | }); |
| 251 | } |
| 252 | |
| 253 | void Close(OpKernelContext* ctx, bool cancel_pending_enqueues, |
| 254 | const DoneCallback& callback) { |
no test coverage detected