STL Memory Versioning
vs_thread.h
Go to the documentation of this file.
1 #ifndef _VS_THREAD_H
2 #define _VS_THREAD_H
3 
4 #include <thread>
5 #include "revision.h"
6 #include "segment.h"
7 
8 namespace vs {
9 
13  class thread : private std::thread {
14  public:
15 
24  template <typename Function, typename... Args>
25  explicit thread(Function&& f, Args&&... args) : std::thread(&thread::threadFunctionWrapper<Function, Args...>, this,
26  std::forward<Function>(f), std::forward<Args>(args)...) {
27  auto s = std::make_shared<Segment>(Revision::currentRevision->current);
28  threadRevision = std::make_shared<Revision>(Revision::currentRevision->current, s);
29  Revision::currentRevision->current->Release();
30  Revision::currentRevision->current = std::make_shared<Segment>(Revision::currentRevision->current);
31  }
32 
38  void join() {
39  std::thread::join();
40 
41  std::shared_ptr<Segment> s = threadRevision->current;
42  while (s != threadRevision->root) {
43  for (auto v: s->written) {
44  v->Merge(Revision::currentRevision, threadRevision, s);
45  }
46  s = s->parent;
47  }
48 
49  threadRevision->current->Release();
51  }
52 
53  // Don't allow to detach thread
54  // using std::thread::detach;
55 
56  using std::thread::get_id;
57 
58  private:
64  std::shared_ptr<Revision> threadRevision;
65 
71  template <class Function, class... Args>
72  static void threadFunctionWrapper(thread* self, Function&& f, Args&&... args) {
73  Revision::currentRevision = self->threadRevision;
74 
75  std::invoke(std::forward<Function>(f), std::forward<Args>(args)...);
76  }
77 
78 
79  };
80 
81 }
82 
83 #endif
static thread_local std::shared_ptr< Revision > currentRevision
The current Revision for current thread.
Definition: revision.h:65
std::thread wrapper with additional logic for managing Revisions
Definition: vs_thread.h:13
thread(Function &&f, Args &&... args)
Construcs thread and creates new Revision for it.
Definition: vs_thread.h:25
void join()
Joins thread and it's Revision.
Definition: vs_thread.h:38
Definition: strategy.h:6
Implementation of the class Revision.
Implementation of the class Segment.