v8threads.h 2.6 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_V8THREADS_H_
#define V8_V8THREADS_H_

8 9
#include "src/isolate.h"

10 11
namespace v8 {
namespace internal {
12

13 14
class RootVisitor;
class ThreadLocalTop;
15 16 17 18 19 20 21 22 23 24 25

class ThreadState {
 public:
  // Returns NULL after the last one.
  ThreadState* Next();

  enum List {FREE_LIST, IN_USE_LIST};

  void LinkInto(List list);
  void Unlink();

26
  // Id of thread.
27 28
  void set_id(ThreadId id) { id_ = id; }
  ThreadId id() { return id_; }
29

30 31 32 33 34 35
  // Should the thread be terminated when it is restored?
  bool terminate_on_restore() { return terminate_on_restore_; }
  void set_terminate_on_restore(bool terminate_on_restore) {
    terminate_on_restore_ = terminate_on_restore;
  }

36 37
  // Get data area for archiving a thread.
  char* data() { return data_; }
38

39
 private:
40
  explicit ThreadState(ThreadManager* thread_manager);
41
  ~ThreadState();
42 43 44

  void AllocateSpace();

45
  ThreadId id_;
46
  bool terminate_on_restore_;
47 48 49
  char* data_;
  ThreadState* next_;
  ThreadState* previous_;
50

51 52 53
  ThreadManager* thread_manager_;

  friend class ThreadManager;
54 55
};

56 57 58
class ThreadVisitor {
 public:
  // ThreadLocalTop may be only available during this call.
59
  virtual void VisitThread(Isolate* isolate, ThreadLocalTop* top) = 0;
60 61 62 63 64

 protected:
  virtual ~ThreadVisitor() {}
};

65
class ThreadManager {
66
 public:
67 68
  void Lock();
  void Unlock();
69

70 71 72 73
  void ArchiveThread();
  bool RestoreThread();
  void FreeThreadResources();
  bool IsArchived();
74

75
  void Iterate(RootVisitor* v);
76
  void IterateArchivedThreads(ThreadVisitor* v);
77 78 79
  bool IsLockedByCurrentThread() {
    return mutex_owner_.Equals(ThreadId::Current());
  }
80

81
  ThreadId CurrentId();
82

83
  void TerminateExecution(ThreadId thread_id);
84 85 86 87

  // Iterate over in-use states.
  ThreadState* FirstThreadStateInUse();
  ThreadState* GetFreeThreadState();
88

89
 private:
90 91
  ThreadManager();
  ~ThreadManager();
92

93 94
  void DeleteThreadStateList(ThreadState* anchor);

95 96
  void EagerlyArchiveThread();

97
  base::Mutex mutex_;
98 99
  ThreadId mutex_owner_;
  ThreadId lazily_archived_thread_;
100 101 102 103 104 105 106 107 108 109 110 111 112 113
  ThreadState* lazily_archived_thread_state_;

  // In the following two lists there is always at least one object on the list.
  // The first object is a flying anchor that is only there to simplify linking
  // and unlinking.
  // Head of linked list of free states.
  ThreadState* free_anchor_;
  // Head of linked list of states in use.
  ThreadState* in_use_anchor_;

  Isolate* isolate_;

  friend class Isolate;
  friend class ThreadState;
114 115 116
};


117 118
}  // namespace internal
}  // namespace v8
119 120

#endif  // V8_V8THREADS_H_