Thrill  0.1
block.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/data/block.cpp
3  *
4  * Part of Project Thrill - http://project-thrill.org
5  *
6  * Copyright (C) 2015 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
9  ******************************************************************************/
10 
11 #include <thrill/data/block.hpp>
13 
14 #include <string>
15 
16 namespace thrill {
17 namespace data {
18 
19 /******************************************************************************/
20 // Block
21 
22 std::ostream& operator << (std::ostream& os, const Block& b) {
23  os << "[Block " << std::hex << &b << std::dec
24  << " byte_block_=" << std::hex << b.byte_block_.get() << std::dec;
25  if (b.IsValid()) {
26  os << " begin_=" << b.begin_
27  << " end_=" << b.end_
28  << " first_item_=" << b.first_item_
29  << " num_items_=" << b.num_items_;
30  // << " data=" << common::Hexdump(b.ToString());
31  }
32  return os << "]";
33 }
34 
35 PinnedBlock Block::PinWait(size_t local_worker_id) const {
36  return Pin(local_worker_id)->Wait();
37 }
38 
39 PinRequestPtr Block::Pin(size_t local_worker_id) const {
40  assert(IsValid());
41  return byte_block()->block_pool_->PinBlock(*this, local_worker_id);
42 }
43 
44 /******************************************************************************/
45 // PinnedBlock
46 
48  if (!IsValid()) return std::string();
49  return std::string(
50  reinterpret_cast<const char*>(data_begin()), size());
51 }
52 
53 std::ostream& operator << (std::ostream& os, const PinnedBlock& b) {
54  os << "[PinnedBlock"
55  << " block_=" << static_cast<const Block&>(b);
56  if (b.byte_block_)
57  os << " pin_count_=" << b.byte_block_->pin_count_str();
58  return os << "]";
59 }
60 
61 /******************************************************************************/
62 // PinRequest
63 
65  if (ready_) return block_;
66 
67  std::unique_lock<std::mutex> lock(block_pool_->mutex_);
68  while (!ready_.load())
69  block_pool_->cv_read_complete_.wait(lock);
70  lock.unlock();
71  return block_;
72 }
73 
74 } // namespace data
75 } // namespace thrill
76 
77 /******************************************************************************/
PinnedBlock PinWait(size_t local_worker_id) const
Convenience function to call Pin() and wait for the future.
Definition: block.cpp:35
Block combines a reference to a read-only ByteBlock and book-keeping information. ...
Definition: block.hpp:52
PinnedBlock Wait()
wait and get the PinnedBlock. this may block until the read is complete.
Definition: block.cpp:64
size_t first_item_
Definition: block.hpp:136
size_t size() const
return length of valid data in bytes.
Definition: block.hpp:100
Type * get() const noexcept
return the enclosed pointer.
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
ByteBlockPtr byte_block_
referenced ByteBlock
Definition: block.hpp:125
PinRequestPtr Pin(size_t local_worker_id) const
Definition: block.cpp:39
High-performance smart pointer used as a wrapping reference counting pointer.
A pinned / pin-counted derivative of a Block.
Definition: block.hpp:157
size_t begin_
beginning offset of valid bytes to read
Definition: block.hpp:128
friend std::ostream & operator<<(std::ostream &os, const Block &b)
Definition: block.cpp:22
bool IsValid() const
Return whether the enclosed ByteBlock is valid.
Definition: block.hpp:74
std::ostream & operator<<(std::ostream &os, const Block &b)
Definition: block.cpp:22
const ByteBlockPtr & byte_block() const
access to byte_block_
Definition: block.hpp:79
std::string ToString() const
Definition: block.cpp:47