tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
my_thread.hpp
Go to the documentation of this file.
1 /**
2  * Copyright (C) 2011
3  * University of Rochester Department of Computer Science
4  * and
5  * Lehigh University Department of Computer Science and Engineering
6  *
7  * License: Modified BSD
8  * Please see the file LICENSE.RSTM for licensing information
9  */
10 
11 /* my_thread.hpp (the name "thread.h" is taken)
12  *
13  * Simple wrapper for pthreads.
14  * Currently uses default attributes, except for PTHREAD_SCOPE_SYSTEM,
15  * which allows threads to be scheduled on multiple processors.
16  */
17 
18 #ifndef MY_THREAD_HPP__
19 #define MY_THREAD_HPP__
20 
21 #include <pthread.h>
22 #include <iostream>
23  using std::cout;
24 #include <sstream>
25  using std::stringstream;
26 #include <cassert>
27 
28 #include "common.hpp"
29 #include "macros.hpp"
30 #include "lock.hpp"
31 
32 /* abstract */
33 class runnable {
34 public:
35  virtual void operator()() = 0;
36  virtual ~runnable() { }
37 };
38 
39 extern void *call_runnable(void *f);
40 
41 class thread {
42  pthread_t my_pthread;
44  friend void *call_runnable(void *f);
46 public:
47  stringstream vout;
48  // Stream to which to send verbose output when stuck inside a txn.
49  // Not synchronized; should be used ONLY by the owner thread.
50 
53  }
56  }
57  bool in_transaction() {
58  return transaction_count > 0;
59  }
60 
61  // start immediately upon creation:
63  my_runnable = f;
65  pthread_attr_t attr;
66  pthread_attr_init(&attr);
67  pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
68  VERIFY(pthread_create(&my_pthread, &attr, call_runnable, (void*) this));
69  }
70  thread() {
71  // for main program
72  my_runnable = 0;
74  my_pthread = pthread_self();
76  }
78  (void) vout.str(std::string());
79  vout.clear();
80  }
82  with_lock cs(io_lock);
83  cout << vout.str();
84  }
85 
86  // join by deleting:
87  ~thread() {
88  assert(my_runnable); // not the main thread
89  VERIFY(pthread_join(my_pthread, 0));
90  }
91 };
92 
93 #endif // MY_THREAD_HPP__
int transaction_count
Definition: my_thread.hpp:45
virtual void operator()()=0
pthread_t my_pthread
Definition: my_thread.hpp:42
friend void * call_runnable(void *f)
Definition: my_thread.cpp:20
#define VERIFY(E)
Definition: macros.hpp:25
thread(runnable *f)
Definition: my_thread.hpp:62
runnable * my_runnable
Definition: my_thread.hpp:43
TRANSACTION_PURE void erase_buffered_output()
Definition: my_thread.hpp:77
Definition: my_thread.hpp:41
bool in_transaction()
Definition: my_thread.hpp:57
d_lock io_lock
Definition: mesh.cpp:33
Definition: lock.hpp:46
#define THREAD_SHUTDOWN()
Definition: common.hpp:93
void dump_buffered_output()
Definition: my_thread.hpp:81
thread()
Definition: my_thread.hpp:70
#define TRANSACTION_PURE
Definition: common.hpp:88
Definition: my_thread.hpp:33
void * call_runnable(void *f)
Definition: my_thread.cpp:20
void leave_transaction()
Definition: my_thread.hpp:54
stringstream vout
Definition: my_thread.hpp:47
void enter_transaction()
Definition: my_thread.hpp:51
virtual ~runnable()
Definition: my_thread.hpp:36
~thread()
Definition: my_thread.hpp:87