Home | History | Annotate | Download | only in src
      1 // Copyright 2012 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_V8THREADS_H_
      6 #define V8_V8THREADS_H_
      7 
      8 namespace v8 {
      9 namespace internal {
     10 
     11 
     12 class ThreadState {
     13  public:
     14   // Returns NULL after the last one.
     15   ThreadState* Next();
     16 
     17   enum List {FREE_LIST, IN_USE_LIST};
     18 
     19   void LinkInto(List list);
     20   void Unlink();
     21 
     22   // Id of thread.
     23   void set_id(ThreadId id) { id_ = id; }
     24   ThreadId id() { return id_; }
     25 
     26   // Should the thread be terminated when it is restored?
     27   bool terminate_on_restore() { return terminate_on_restore_; }
     28   void set_terminate_on_restore(bool terminate_on_restore) {
     29     terminate_on_restore_ = terminate_on_restore;
     30   }
     31 
     32   // Get data area for archiving a thread.
     33   char* data() { return data_; }
     34 
     35  private:
     36   explicit ThreadState(ThreadManager* thread_manager);
     37   ~ThreadState();
     38 
     39   void AllocateSpace();
     40 
     41   ThreadId id_;
     42   bool terminate_on_restore_;
     43   char* data_;
     44   ThreadState* next_;
     45   ThreadState* previous_;
     46 
     47   ThreadManager* thread_manager_;
     48 
     49   friend class ThreadManager;
     50 };
     51 
     52 
     53 // Defined in isolate.h.
     54 class ThreadLocalTop;
     55 
     56 
     57 class ThreadVisitor {
     58  public:
     59   // ThreadLocalTop may be only available during this call.
     60   virtual void VisitThread(Isolate* isolate, ThreadLocalTop* top) = 0;
     61 
     62  protected:
     63   virtual ~ThreadVisitor() {}
     64 };
     65 
     66 
     67 class ThreadManager {
     68  public:
     69   void Lock();
     70   void Unlock();
     71 
     72   void ArchiveThread();
     73   bool RestoreThread();
     74   void FreeThreadResources();
     75   bool IsArchived();
     76 
     77   void Iterate(ObjectVisitor* v);
     78   void IterateArchivedThreads(ThreadVisitor* v);
     79   bool IsLockedByCurrentThread() {
     80     return mutex_owner_.Equals(ThreadId::Current());
     81   }
     82 
     83   ThreadId CurrentId();
     84 
     85   void TerminateExecution(ThreadId thread_id);
     86 
     87   // Iterate over in-use states.
     88   ThreadState* FirstThreadStateInUse();
     89   ThreadState* GetFreeThreadState();
     90 
     91  private:
     92   ThreadManager();
     93   ~ThreadManager();
     94 
     95   void DeleteThreadStateList(ThreadState* anchor);
     96 
     97   void EagerlyArchiveThread();
     98 
     99   Mutex mutex_;
    100   ThreadId mutex_owner_;
    101   ThreadId lazily_archived_thread_;
    102   ThreadState* lazily_archived_thread_state_;
    103 
    104   // In the following two lists there is always at least one object on the list.
    105   // The first object is a flying anchor that is only there to simplify linking
    106   // and unlinking.
    107   // Head of linked list of free states.
    108   ThreadState* free_anchor_;
    109   // Head of linked list of states in use.
    110   ThreadState* in_use_anchor_;
    111 
    112   Isolate* isolate_;
    113 
    114   friend class Isolate;
    115   friend class ThreadState;
    116 };
    117 
    118 
    119 } }  // namespace v8::internal
    120 
    121 #endif  // V8_V8THREADS_H_
    122