v8threads.h 2.72 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
#ifndef V8_EXECUTION_V8THREADS_H_
#define V8_EXECUTION_V8THREADS_H_
7

Clemens Hammacher's avatar
Clemens Hammacher committed
8 9
#include <atomic>

10
#include "src/execution/isolate.h"
11

12 13
namespace v8 {
namespace internal {
14

15 16
class RootVisitor;
class ThreadLocalTop;
17 18 19

class ThreadState {
 public:
20
  // Returns nullptr after the last one.
21 22
  ThreadState* Next();

23
  enum List { FREE_LIST, IN_USE_LIST };
24 25 26 27

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

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

32 33
  // Get data area for archiving a thread.
  char* data() { return data_; }
34

35
 private:
36
  explicit ThreadState(ThreadManager* thread_manager);
37
  ~ThreadState();
38 39 40

  void AllocateSpace();

41
  ThreadId id_;
42 43 44
  char* data_;
  ThreadState* next_;
  ThreadState* previous_;
45

46 47 48
  ThreadManager* thread_manager_;

  friend class ThreadManager;
49 50
};

51 52 53
class ThreadVisitor {
 public:
  // ThreadLocalTop may be only available during this call.
54
  virtual void VisitThread(Isolate* isolate, ThreadLocalTop* top) = 0;
55 56

 protected:
57
  virtual ~ThreadVisitor() = default;
58 59
};

60
class ThreadManager {
61
 public:
62
  void Lock();
63
  V8_EXPORT_PRIVATE void Unlock();
64

65
  void InitThread(const ExecutionAccess&);
66 67 68 69
  void ArchiveThread();
  bool RestoreThread();
  void FreeThreadResources();
  bool IsArchived();
70

71
  void Iterate(RootVisitor* v);
72
  void IterateArchivedThreads(ThreadVisitor* v);
Clemens Hammacher's avatar
Clemens Hammacher committed
73 74
  bool IsLockedByCurrentThread() const {
    return mutex_owner_.load(std::memory_order_relaxed) == ThreadId::Current();
75
  }
76 77 78
  bool IsLockedByThread(ThreadId id) const {
    return mutex_owner_.load(std::memory_order_relaxed) == id;
  }
79

80
  ThreadId CurrentId();
81

82 83 84
  // Iterate over in-use states.
  ThreadState* FirstThreadStateInUse();
  ThreadState* GetFreeThreadState();
85

86
 private:
87
  explicit ThreadManager(Isolate* isolate);
88
  ~ThreadManager();
89

90 91
  void DeleteThreadStateList(ThreadState* anchor);

92 93
  void EagerlyArchiveThread();

94
  base::Mutex mutex_;
Clemens Hammacher's avatar
Clemens Hammacher committed
95 96 97
  // {ThreadId} must be trivially copyable to be stored in {std::atomic}.
  ASSERT_TRIVIALLY_COPYABLE(i::ThreadId);
  std::atomic<ThreadId> mutex_owner_;
98
  ThreadId lazily_archived_thread_;
99 100 101 102 103 104 105 106 107 108 109 110 111 112
  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;
113 114
};

115 116
}  // namespace internal
}  // namespace v8
117

118
#endif  // V8_EXECUTION_V8THREADS_H_