tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

WorkerThread.h (2569B)


      1 //
      2 // Copyright 2016 The ANGLE Project Authors. All rights reserved.
      3 // Use of this source code is governed by a BSD-style license that can be
      4 // found in the LICENSE file.
      5 //
      6 // WorkerThread:
      7 //   Asychronous tasks/threads for ANGLE, similar to a TaskRunner in Chromium.
      8 //   Can be implemented as different targets, depending on platform.
      9 //
     10 
     11 #ifndef LIBANGLE_WORKER_THREAD_H_
     12 #define LIBANGLE_WORKER_THREAD_H_
     13 
     14 #include <array>
     15 #include <memory>
     16 #include <vector>
     17 
     18 #include "common/debug.h"
     19 #include "libANGLE/features.h"
     20 
     21 namespace angle
     22 {
     23 
     24 class WorkerThreadPool;
     25 
     26 // A callback function with no return value and no arguments.
     27 class Closure
     28 {
     29  public:
     30    virtual ~Closure()        = default;
     31    virtual void operator()() = 0;
     32 };
     33 
     34 // An event that we can wait on, useful for joining worker threads.
     35 class WaitableEvent : angle::NonCopyable
     36 {
     37  public:
     38    WaitableEvent();
     39    virtual ~WaitableEvent();
     40 
     41    // Waits indefinitely for the event to be signaled.
     42    virtual void wait() = 0;
     43 
     44    // Peeks whether the event is ready. If ready, wait() will not block.
     45    virtual bool isReady() = 0;
     46    void setWorkerThreadPool(std::shared_ptr<WorkerThreadPool> pool) { mPool = pool; }
     47 
     48    template <size_t Count>
     49    static void WaitMany(std::array<std::shared_ptr<WaitableEvent>, Count> *waitables)
     50    {
     51        ASSERT(Count > 0);
     52        for (size_t index = 0; index < Count; ++index)
     53        {
     54            (*waitables)[index]->wait();
     55        }
     56    }
     57 
     58  private:
     59    std::shared_ptr<WorkerThreadPool> mPool;
     60 };
     61 
     62 // A mock waitable event.
     63 class WaitableEventDone final : public WaitableEvent
     64 {
     65  public:
     66    void wait() override;
     67    bool isReady() override;
     68 };
     69 
     70 // Request WorkerThreads from the WorkerThreadPool. Each pool can keep worker threads around so
     71 // we avoid the costly spin up and spin down time.
     72 class WorkerThreadPool : angle::NonCopyable
     73 {
     74  public:
     75    WorkerThreadPool();
     76    virtual ~WorkerThreadPool();
     77 
     78    static std::shared_ptr<WorkerThreadPool> Create(bool multithreaded);
     79    static std::shared_ptr<WaitableEvent> PostWorkerTask(std::shared_ptr<WorkerThreadPool> pool,
     80                                                         std::shared_ptr<Closure> task);
     81 
     82    virtual void setMaxThreads(size_t maxThreads) = 0;
     83 
     84    virtual bool isAsync() = 0;
     85 
     86  private:
     87    // Returns an event to wait on for the task to finish.
     88    // If the pool fails to create the task, returns null.
     89    virtual std::shared_ptr<WaitableEvent> postWorkerTask(std::shared_ptr<Closure> task) = 0;
     90 };
     91 
     92 }  // namespace angle
     93 
     94 #endif  // LIBANGLE_WORKER_THREAD_H_