Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 /******************************************************************************/
Block combines a reference to a read-only ByteBlock and book-keeping information. ...
Definition: block.hpp:52
const ByteBlockPtr & byte_block() const
access to byte_block_
Definition: block.hpp:79
PinnedBlock Wait()
wait and get the PinnedBlock. this may block until the read is complete.
Definition: block.cpp:64
PinRequestPtr Pin(size_t local_worker_id) const
Definition: block.cpp:39
size_t first_item_
Definition: block.hpp:136
size_t size() const
return length of valid data in bytes.
Definition: block.hpp:245
std::string ToString() const
Definition: block.cpp:47
bool IsValid() const
Return whether the enclosed ByteBlock is valid.
Definition: block.hpp:74
Type * get() const noexcept
return the enclosed pointer.
std::condition_variable cv_read_complete_
Definition: block_pool.hpp:185
BlockPool * block_pool_
reference back to BlockPool
Definition: block.hpp:350
PinnedBlock PinWait(size_t local_worker_id) const
Convenience function to call Pin() and wait for the future.
Definition: block.cpp:35
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
const Byte * data_begin() const
return pointer to beginning of valid data
Definition: block.hpp:259
High-performance smart pointer used as a wrapping reference counting pointer.
A pinned / pin-counted derivative of a Block.
Definition: block.hpp:157
std::atomic< bool > ready_
indication that the PinnedBlocks ready
Definition: block.hpp:358
PinnedBlock block_
Definition: block.hpp:353
bool IsValid() const
Return whether the enclosed ByteBlock is valid.
Definition: block.hpp:223
size_t begin_
beginning offset of valid bytes to read
Definition: block.hpp:128
std::mutex mutex_
locked before internal state is changed
Definition: block_pool.hpp:181
std::ostream & operator<<(std::ostream &os, const Block &b)
Definition: block.cpp:22