Hash :
e157e5a9
Author :
Date :
2016-11-29T13:55:56
Fix missing virtual destructor warning on OSX standalone BUG=angleproject:422 Change-Id: I1a1c4551f2fc55b74afd66e7f18ea61e77250eb6 Reviewed-on: https://chromium-review.googlesource.com/414634 Commit-Queue: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org>
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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
//
// Copyright 2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// WorkerThread:
// Asychronous tasks/threads for ANGLE, similar to a TaskRunner in Chromium.
// Can be implemented as different targets, depending on platform.
//
#ifndef LIBANGLE_WORKER_THREAD_H_
#define LIBANGLE_WORKER_THREAD_H_
#include <array>
#include <vector>
#include "common/debug.h"
#include "libANGLE/features.h"
#if (ANGLE_STD_ASYNC_WORKERS == ANGLE_ENABLED)
#include <future>
#endif // (ANGLE_STD_ASYNC_WORKERS == ANGLE_ENABLED)
namespace angle
{
// Indicates whether a WaitableEvent should automatically reset the event state after a single
// waiting thread has been released or remain signaled until reset() is manually invoked.
enum class EventResetPolicy
{
Manual,
Automatic
};
// Specify the initial state on creation.
enum class EventInitialState
{
NonSignaled,
Signaled
};
// A callback function with no return value and no arguments.
class Closure
{
public:
virtual ~Closure() = default;
virtual void operator()() = 0;
};
namespace priv
{
// An event that we can wait on, useful for joining worker threads.
template <typename Impl>
class WaitableEventBase : angle::NonCopyable
{
public:
WaitableEventBase(EventResetPolicy resetPolicy, EventInitialState initialState);
WaitableEventBase(WaitableEventBase &&other);
// Puts the event in the un-signaled state.
void reset();
// Waits indefinitely for the event to be signaled.
void wait();
// Puts the event in the signaled state, causing any thread blocked on Wait to be woken up.
// The event state is reset to non-signaled after a waiting thread has been released.
void signal();
protected:
Impl ©Base(Impl &&other);
template <size_t Count>
static size_t WaitManyBase(std::array<Impl, Count> *waitables);
EventResetPolicy mResetPolicy;
bool mSignaled;
};
template <typename Impl>
WaitableEventBase<Impl>::WaitableEventBase(EventResetPolicy resetPolicy,
EventInitialState initialState)
: mResetPolicy(resetPolicy), mSignaled(initialState == EventInitialState::Signaled)
{
}
template <typename Impl>
WaitableEventBase<Impl>::WaitableEventBase(WaitableEventBase &&other)
: mResetPolicy(other.mResetPolicy), mSignaled(other.mSignaled)
{
}
template <typename Impl>
void WaitableEventBase<Impl>::reset()
{
static_cast<Impl *>(this)->resetImpl();
}
template <typename Impl>
void WaitableEventBase<Impl>::wait()
{
static_cast<Impl *>(this)->waitImpl();
}
template <typename Impl>
void WaitableEventBase<Impl>::signal()
{
static_cast<Impl *>(this)->signalImpl();
}
template <typename Impl>
template <size_t Count>
// static
size_t WaitableEventBase<Impl>::WaitManyBase(std::array<Impl, Count> *waitables)
{
ASSERT(Count > 0);
for (size_t index = 0; index < Count; ++index)
{
(*waitables)[index].wait();
}
return 0;
}
template <typename Impl>
Impl &WaitableEventBase<Impl>::copyBase(Impl &&other)
{
std::swap(mSignaled, other.mSignaled);
std::swap(mResetPolicy, other.mResetPolicy);
return *static_cast<Impl *>(this);
}
class SingleThreadedWaitableEvent : public WaitableEventBase<SingleThreadedWaitableEvent>
{
public:
SingleThreadedWaitableEvent();
SingleThreadedWaitableEvent(EventResetPolicy resetPolicy, EventInitialState initialState);
~SingleThreadedWaitableEvent();
SingleThreadedWaitableEvent(SingleThreadedWaitableEvent &&other);
SingleThreadedWaitableEvent &operator=(SingleThreadedWaitableEvent &&other);
void resetImpl();
void waitImpl();
void signalImpl();
// Wait, synchronously, on multiple events.
// returns the index of a WaitableEvent which has been signaled.
template <size_t Count>
static size_t WaitMany(std::array<SingleThreadedWaitableEvent, Count> *waitables);
};
template <size_t Count>
// static
size_t SingleThreadedWaitableEvent::WaitMany(
std::array<SingleThreadedWaitableEvent, Count> *waitables)
{
return WaitableEventBase<SingleThreadedWaitableEvent>::WaitManyBase(waitables);
}
#if (ANGLE_STD_ASYNC_WORKERS == ANGLE_ENABLED)
class AsyncWaitableEvent : public WaitableEventBase<AsyncWaitableEvent>
{
public:
AsyncWaitableEvent();
AsyncWaitableEvent(EventResetPolicy resetPolicy, EventInitialState initialState);
~AsyncWaitableEvent();
AsyncWaitableEvent(AsyncWaitableEvent &&other);
AsyncWaitableEvent &operator=(AsyncWaitableEvent &&other);
void resetImpl();
void waitImpl();
void signalImpl();
// Wait, synchronously, on multiple events.
// returns the index of a WaitableEvent which has been signaled.
template <size_t Count>
static size_t WaitMany(std::array<AsyncWaitableEvent, Count> *waitables);
private:
friend class AsyncWorkerPool;
void setFuture(std::future<void> &&future);
std::future<void> mFuture;
};
template <size_t Count>
// static
size_t AsyncWaitableEvent::WaitMany(std::array<AsyncWaitableEvent, Count> *waitables)
{
return WaitableEventBase<AsyncWaitableEvent>::WaitManyBase(waitables);
}
#endif // (ANGLE_STD_ASYNC_WORKERS == ANGLE_ENABLED)
// The traits class allows the the thread pool to return the "Typed" waitable event from postTask.
// Otherwise postTask would always think it returns the current active type, so the unit tests
// could not run on multiple worker types in the same compilation.
template <typename Impl>
struct WorkerThreadPoolTraits;
class SingleThreadedWorkerPool;
template <>
struct WorkerThreadPoolTraits<SingleThreadedWorkerPool>
{
using WaitableEventType = SingleThreadedWaitableEvent;
};
#if (ANGLE_STD_ASYNC_WORKERS == ANGLE_ENABLED)
class AsyncWorkerPool;
template <>
struct WorkerThreadPoolTraits<AsyncWorkerPool>
{
using WaitableEventType = AsyncWaitableEvent;
};
#endif // (ANGLE_STD_ASYNC_WORKERS == ANGLE_ENABLED)
// Request WorkerThreads from the WorkerThreadPool. Each pool can keep worker threads around so
// we avoid the costly spin up and spin down time.
template <typename Impl>
class WorkerThreadPoolBase : angle::NonCopyable
{
public:
WorkerThreadPoolBase(size_t maxThreads);
~WorkerThreadPoolBase();
using WaitableEventType = typename WorkerThreadPoolTraits<Impl>::WaitableEventType;
// Returns an event to wait on for the task to finish.
// If the pool fails to create the task, returns null.
WaitableEventType postWorkerTask(Closure *task);
};
template <typename Impl>
WorkerThreadPoolBase<Impl>::WorkerThreadPoolBase(size_t maxThreads)
{
}
template <typename Impl>
WorkerThreadPoolBase<Impl>::~WorkerThreadPoolBase()
{
}
template <typename Impl>
typename WorkerThreadPoolBase<Impl>::WaitableEventType WorkerThreadPoolBase<Impl>::postWorkerTask(
Closure *task)
{
return static_cast<Impl *>(this)->postWorkerTaskImpl(task);
}
class SingleThreadedWorkerPool : public WorkerThreadPoolBase<SingleThreadedWorkerPool>
{
public:
SingleThreadedWorkerPool(size_t maxThreads);
~SingleThreadedWorkerPool();
SingleThreadedWaitableEvent postWorkerTaskImpl(Closure *task);
};
#if (ANGLE_STD_ASYNC_WORKERS == ANGLE_ENABLED)
class AsyncWorkerPool : public WorkerThreadPoolBase<AsyncWorkerPool>
{
public:
AsyncWorkerPool(size_t maxThreads);
~AsyncWorkerPool();
AsyncWaitableEvent postWorkerTaskImpl(Closure *task);
};
#endif // (ANGLE_STD_ASYNC_WORKERS == ANGLE_ENABLED)
} // namespace priv
#if (ANGLE_STD_ASYNC_WORKERS == ANGLE_ENABLED)
using WaitableEvent = priv::AsyncWaitableEvent;
using WorkerThreadPool = priv::AsyncWorkerPool;
#else
using WaitableEvent = priv::SingleThreadedWaitableEvent;
using WorkerThreadPool = priv::SingleThreadedWorkerPool;
#endif // (ANGLE_STD_ASYNC_WORKERS == ANGLE_ENABLED)
} // namespace angle
#endif // LIBANGLE_WORKER_THREAD_H_