File size: 7,960 Bytes
0b5e147 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
// 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 <memory>
#include "arrow/io/interfaces.h"
#include "arrow/result.h"
#include "arrow/status.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/macros.h"
#include "arrow/util/visibility.h"
namespace arrow {
namespace io {
namespace internal {
template <class LockType>
class SharedLockGuard {
public:
explicit SharedLockGuard(LockType* lock) : lock_(lock) { lock_->LockShared(); }
~SharedLockGuard() { lock_->UnlockShared(); }
protected:
LockType* lock_;
};
template <class LockType>
class ExclusiveLockGuard {
public:
explicit ExclusiveLockGuard(LockType* lock) : lock_(lock) { lock_->LockExclusive(); }
~ExclusiveLockGuard() { lock_->UnlockExclusive(); }
protected:
LockType* lock_;
};
// Debug concurrency checker that marks "shared" and "exclusive" code sections,
// aborting if the concurrency rules get violated. Does nothing in release mode.
// Note that we intentionally use the same class declaration in debug and
// release builds in order to avoid runtime failures when e.g. loading a
// release-built DLL with a debug-built application, or the reverse.
class ARROW_EXPORT SharedExclusiveChecker {
public:
SharedExclusiveChecker();
void LockShared();
void UnlockShared();
void LockExclusive();
void UnlockExclusive();
SharedLockGuard<SharedExclusiveChecker> shared_guard() {
return SharedLockGuard<SharedExclusiveChecker>(this);
}
ExclusiveLockGuard<SharedExclusiveChecker> exclusive_guard() {
return ExclusiveLockGuard<SharedExclusiveChecker>(this);
}
protected:
struct Impl;
std::shared_ptr<Impl> impl_;
};
// Concurrency wrappers for IO classes that check the correctness of
// concurrent calls to various methods. It is not necessary to wrap all
// IO classes with these, only a few core classes that get used in tests.
//
// We're not using virtual inheritance here as virtual bases have poorly
// understood semantic overhead which we'd be passing on to implementers
// and users of these interfaces. Instead, we just duplicate the method
// wrappers between those two classes.
template <class Derived>
class ARROW_EXPORT InputStreamConcurrencyWrapper : public InputStream {
public:
Status Close() final {
auto guard = lock_.exclusive_guard();
return derived()->DoClose();
}
Status Abort() final {
auto guard = lock_.exclusive_guard();
return derived()->DoAbort();
}
Result<int64_t> Tell() const final {
auto guard = lock_.exclusive_guard();
return derived()->DoTell();
}
Result<int64_t> Read(int64_t nbytes, void* out) final {
auto guard = lock_.exclusive_guard();
return derived()->DoRead(nbytes, out);
}
Result<std::shared_ptr<Buffer>> Read(int64_t nbytes) final {
auto guard = lock_.exclusive_guard();
return derived()->DoRead(nbytes);
}
Result<std::string_view> Peek(int64_t nbytes) final {
auto guard = lock_.exclusive_guard();
return derived()->DoPeek(nbytes);
}
/*
Methods to implement in derived class:
Status DoClose();
Result<int64_t> DoTell() const;
Result<int64_t> DoRead(int64_t nbytes, void* out);
Result<std::shared_ptr<Buffer>> DoRead(int64_t nbytes);
And optionally:
Status DoAbort() override;
Result<std::string_view> DoPeek(int64_t nbytes) override;
These methods should be protected in the derived class and
InputStreamConcurrencyWrapper declared as a friend with
friend InputStreamConcurrencyWrapper<derived>;
*/
protected:
// Default implementations. They are virtual because the derived class may
// have derived classes itself.
virtual Status DoAbort() { return derived()->DoClose(); }
virtual Result<std::string_view> DoPeek(int64_t ARROW_ARG_UNUSED(nbytes)) {
return Status::NotImplemented("Peek not implemented");
}
Derived* derived() { return ::arrow::internal::checked_cast<Derived*>(this); }
const Derived* derived() const {
return ::arrow::internal::checked_cast<const Derived*>(this);
}
mutable SharedExclusiveChecker lock_;
};
template <class Derived>
class ARROW_EXPORT RandomAccessFileConcurrencyWrapper : public RandomAccessFile {
public:
Status Close() final {
auto guard = lock_.exclusive_guard();
return derived()->DoClose();
}
Status Abort() final {
auto guard = lock_.exclusive_guard();
return derived()->DoAbort();
}
Result<int64_t> Tell() const final {
auto guard = lock_.exclusive_guard();
return derived()->DoTell();
}
Result<int64_t> Read(int64_t nbytes, void* out) final {
auto guard = lock_.exclusive_guard();
return derived()->DoRead(nbytes, out);
}
Result<std::shared_ptr<Buffer>> Read(int64_t nbytes) final {
auto guard = lock_.exclusive_guard();
return derived()->DoRead(nbytes);
}
Result<std::string_view> Peek(int64_t nbytes) final {
auto guard = lock_.exclusive_guard();
return derived()->DoPeek(nbytes);
}
Status Seek(int64_t position) final {
auto guard = lock_.exclusive_guard();
return derived()->DoSeek(position);
}
Result<int64_t> GetSize() final {
auto guard = lock_.shared_guard();
return derived()->DoGetSize();
}
// NOTE: ReadAt doesn't use stream pointer, but it is allowed to update it
// (it's the case on Windows when using ReadFileEx).
// So any method that relies on the current position (even if it doesn't
// update it, such as Peek) cannot run in parallel with ReadAt and has
// to use the exclusive_guard.
Result<int64_t> ReadAt(int64_t position, int64_t nbytes, void* out) final {
auto guard = lock_.shared_guard();
return derived()->DoReadAt(position, nbytes, out);
}
Result<std::shared_ptr<Buffer>> ReadAt(int64_t position, int64_t nbytes) final {
auto guard = lock_.shared_guard();
return derived()->DoReadAt(position, nbytes);
}
/*
Methods to implement in derived class:
Status DoClose();
Result<int64_t> DoTell() const;
Result<int64_t> DoRead(int64_t nbytes, void* out);
Result<std::shared_ptr<Buffer>> DoRead(int64_t nbytes);
Status DoSeek(int64_t position);
Result<int64_t> DoGetSize()
Result<int64_t> DoReadAt(int64_t position, int64_t nbytes, void* out);
Result<std::shared_ptr<Buffer>> DoReadAt(int64_t position, int64_t nbytes);
And optionally:
Status DoAbort() override;
Result<std::string_view> DoPeek(int64_t nbytes) override;
These methods should be protected in the derived class and
RandomAccessFileConcurrencyWrapper declared as a friend with
friend RandomAccessFileConcurrencyWrapper<derived>;
*/
protected:
// Default implementations. They are virtual because the derived class may
// have derived classes itself.
virtual Status DoAbort() { return derived()->DoClose(); }
virtual Result<std::string_view> DoPeek(int64_t ARROW_ARG_UNUSED(nbytes)) {
return Status::NotImplemented("Peek not implemented");
}
Derived* derived() { return ::arrow::internal::checked_cast<Derived*>(this); }
const Derived* derived() const {
return ::arrow::internal::checked_cast<const Derived*>(this);
}
mutable SharedExclusiveChecker lock_;
};
} // namespace internal
} // namespace io
} // namespace arrow
|