| 486 | typename T |
| 487 | > |
| 488 | bool pipe<T>:: |
| 489 | enqueue_or_timeout ( |
| 490 | T& item, |
| 491 | unsigned long timeout |
| 492 | ) |
| 493 | { |
| 494 | auto_mutex M(m); |
| 495 | ++enqueue_waiters; |
| 496 | |
| 497 | // wait until there is room or we are disabled or |
| 498 | // we run out of time. |
| 499 | bool timed_out = false; |
| 500 | while (pipe_size == pipe_max_size && enabled && enqueue_enabled && |
| 501 | !(pipe_max_size == 0 && dequeue_waiters > 0 && first == 1) ) |
| 502 | { |
| 503 | if (timeout == 0 || enqueue_sig.wait_or_timeout(timeout) == false) |
| 504 | { |
| 505 | timed_out = true; |
| 506 | break; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | if (enabled == false || timed_out || enqueue_enabled == false) |
| 511 | { |
| 512 | --enqueue_waiters; |
| 513 | // let the destructor know we are unblocking |
| 514 | unblock_sig.broadcast(); |
| 515 | return false; |
| 516 | } |
| 517 | |
| 518 | // set the appropriate values for first and last |
| 519 | if (pipe_size == 0) |
| 520 | { |
| 521 | first = 0; |
| 522 | last = 0; |
| 523 | } |
| 524 | else |
| 525 | { |
| 526 | last = (last+1)%pipe_max_size; |
| 527 | } |
| 528 | |
| 529 | |
| 530 | exchange(item,data[last]); |
| 531 | |
| 532 | // wake up a call to dequeue() if there are any currently blocked |
| 533 | if (dequeue_waiters > 0) |
| 534 | dequeue_sig.signal(); |
| 535 | |
| 536 | if (pipe_max_size > 0) |
| 537 | { |
| 538 | ++pipe_size; |
| 539 | } |
| 540 | else |
| 541 | { |
| 542 | // wait for a dequeue to take the item out |
| 543 | while (last == 0 && enabled && enqueue_enabled) |
| 544 | enqueue_sig.wait(); |
| 545 |
nothing calls this directly
no test coverage detected