Home | History | Annotate | Download | only in libplatform
      1 // Copyright 2013 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
      6 #define V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
      7 
      8 #include <functional>
      9 #include <map>
     10 #include <queue>
     11 #include <vector>
     12 
     13 #include "include/v8-platform.h"
     14 #include "src/base/macros.h"
     15 #include "src/base/platform/mutex.h"
     16 #include "src/libplatform/task-queue.h"
     17 
     18 namespace v8 {
     19 namespace platform {
     20 
     21 class TaskQueue;
     22 class Thread;
     23 class WorkerThread;
     24 
     25 class DefaultPlatform : public Platform {
     26  public:
     27   DefaultPlatform();
     28   virtual ~DefaultPlatform();
     29 
     30   void SetThreadPoolSize(int thread_pool_size);
     31 
     32   void EnsureInitialized();
     33 
     34   bool PumpMessageLoop(v8::Isolate* isolate);
     35 
     36   // v8::Platform implementation.
     37   void CallOnBackgroundThread(Task* task,
     38                               ExpectedRuntime expected_runtime) override;
     39   void CallOnForegroundThread(v8::Isolate* isolate, Task* task) override;
     40   void CallDelayedOnForegroundThread(Isolate* isolate, Task* task,
     41                                      double delay_in_seconds) override;
     42   void CallIdleOnForegroundThread(Isolate* isolate, IdleTask* task) override;
     43   bool IdleTasksEnabled(Isolate* isolate) override;
     44   double MonotonicallyIncreasingTime() override;
     45   const uint8_t* GetCategoryGroupEnabled(const char* name) override;
     46   const char* GetCategoryGroupName(
     47       const uint8_t* category_enabled_flag) override;
     48   uint64_t AddTraceEvent(char phase, const uint8_t* category_enabled_flag,
     49                          const char* name, uint64_t id, uint64_t bind_id,
     50                          int32_t num_args, const char** arg_names,
     51                          const uint8_t* arg_types, const uint64_t* arg_values,
     52                          unsigned int flags) override;
     53   void UpdateTraceEventDuration(const uint8_t* category_enabled_flag,
     54                                 const char* name, uint64_t handle) override;
     55 
     56 
     57  private:
     58   static const int kMaxThreadPoolSize;
     59 
     60   Task* PopTaskInMainThreadQueue(v8::Isolate* isolate);
     61   Task* PopTaskInMainThreadDelayedQueue(v8::Isolate* isolate);
     62 
     63   base::Mutex lock_;
     64   bool initialized_;
     65   int thread_pool_size_;
     66   std::vector<WorkerThread*> thread_pool_;
     67   TaskQueue queue_;
     68   std::map<v8::Isolate*, std::queue<Task*> > main_thread_queue_;
     69 
     70   typedef std::pair<double, Task*> DelayedEntry;
     71   std::map<v8::Isolate*,
     72            std::priority_queue<DelayedEntry, std::vector<DelayedEntry>,
     73                                std::greater<DelayedEntry> > >
     74       main_thread_delayed_queue_;
     75 
     76   DISALLOW_COPY_AND_ASSIGN(DefaultPlatform);
     77 };
     78 
     79 
     80 }  // namespace platform
     81 }  // namespace v8
     82 
     83 
     84 #endif  // V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
     85