-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-13244: [C++] Add facility to get current thread id as uint64 #10644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #include "arrow/io/stdio.h" | ||
|
|
||
| #include <iostream> | ||
|
|
||
| #include "arrow/buffer.h" | ||
| #include "arrow/result.h" | ||
|
|
||
| namespace arrow { | ||
| namespace io { | ||
|
|
||
| // | ||
| // StdoutStream implementation | ||
| // | ||
|
|
||
| StdoutStream::StdoutStream() : pos_(0) { set_mode(FileMode::WRITE); } | ||
|
|
||
| Status StdoutStream::Close() { return Status::OK(); } | ||
|
|
||
| bool StdoutStream::closed() const { return false; } | ||
|
|
||
| Result<int64_t> StdoutStream::Tell() const { return pos_; } | ||
|
|
||
| Status StdoutStream::Write(const void* data, int64_t nbytes) { | ||
| pos_ += nbytes; | ||
| std::cout.write(reinterpret_cast<const char*>(data), nbytes); | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| // | ||
| // StderrStream implementation | ||
| // | ||
|
|
||
| StderrStream::StderrStream() : pos_(0) { set_mode(FileMode::WRITE); } | ||
|
|
||
| Status StderrStream::Close() { return Status::OK(); } | ||
|
|
||
| bool StderrStream::closed() const { return false; } | ||
|
|
||
| Result<int64_t> StderrStream::Tell() const { return pos_; } | ||
|
|
||
| Status StderrStream::Write(const void* data, int64_t nbytes) { | ||
| pos_ += nbytes; | ||
| std::cerr.write(reinterpret_cast<const char*>(data), nbytes); | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| // | ||
| // StdinStream implementation | ||
| // | ||
|
|
||
| StdinStream::StdinStream() : pos_(0) { set_mode(FileMode::READ); } | ||
|
|
||
| Status StdinStream::Close() { return Status::OK(); } | ||
|
|
||
| bool StdinStream::closed() const { return false; } | ||
|
|
||
| Result<int64_t> StdinStream::Tell() const { return pos_; } | ||
|
|
||
| Result<int64_t> StdinStream::Read(int64_t nbytes, void* out) { | ||
| std::cin.read(reinterpret_cast<char*>(out), nbytes); | ||
| if (std::cin) { | ||
| pos_ += nbytes; | ||
| return nbytes; | ||
| } else { | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| Result<std::shared_ptr<Buffer>> StdinStream::Read(int64_t nbytes) { | ||
| ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateResizableBuffer(nbytes)); | ||
| ARROW_ASSIGN_OR_RAISE(int64_t bytes_read, Read(nbytes, buffer->mutable_data())); | ||
| ARROW_RETURN_NOT_OK(buffer->Resize(bytes_read, false)); | ||
| buffer->ZeroPadding(); | ||
| return std::move(buffer); | ||
| } | ||
|
|
||
| } // namespace io | ||
| } // namespace arrow |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| #include "arrow/io/interfaces.h" | ||
| #include "arrow/util/visibility.h" | ||
|
|
||
| namespace arrow { | ||
| namespace io { | ||
|
|
||
| // Output stream that just writes to stdout. | ||
| class ARROW_EXPORT StdoutStream : public OutputStream { | ||
| public: | ||
| StdoutStream(); | ||
| ~StdoutStream() override {} | ||
|
|
||
| Status Close() override; | ||
| bool closed() const override; | ||
|
|
||
| Result<int64_t> Tell() const override; | ||
|
|
||
| Status Write(const void* data, int64_t nbytes) override; | ||
|
|
||
| private: | ||
| int64_t pos_; | ||
| }; | ||
|
|
||
| // Output stream that just writes to stderr. | ||
| class ARROW_EXPORT StderrStream : public OutputStream { | ||
| public: | ||
| StderrStream(); | ||
| ~StderrStream() override {} | ||
|
|
||
| Status Close() override; | ||
| bool closed() const override; | ||
|
|
||
| Result<int64_t> Tell() const override; | ||
|
|
||
| Status Write(const void* data, int64_t nbytes) override; | ||
|
|
||
| private: | ||
| int64_t pos_; | ||
| }; | ||
|
|
||
| // Input stream that just reads from stdin. | ||
| class ARROW_EXPORT StdinStream : public InputStream { | ||
| public: | ||
| StdinStream(); | ||
| ~StdinStream() override {} | ||
|
|
||
| Status Close() override; | ||
| bool closed() const override; | ||
|
|
||
| Result<int64_t> Tell() const override; | ||
|
|
||
| Result<int64_t> Read(int64_t nbytes, void* out) override; | ||
|
|
||
| Result<std::shared_ptr<Buffer>> Read(int64_t nbytes) override; | ||
|
|
||
| private: | ||
| int64_t pos_; | ||
| }; | ||
|
|
||
| } // namespace io | ||
| } // namespace arrow |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't see any reason this wouldn't work but why not just use the
uint64_tversion ofstd::thread::id()? Is it to allow this to beconstexpr?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I hadn't thought of that. But it seems simpler to use a hardcoded number like this.