got everything compiling (should still work too) with aos
diff --git a/aos/common/util/thread.cc b/aos/common/util/thread.cc
new file mode 100644
index 0000000..fab62eb
--- /dev/null
+++ b/aos/common/util/thread.cc
@@ -0,0 +1,39 @@
+#include "aos/common/util/thread.h"
+
+#include <pthread.h>
+#include <assert.h>
+
+namespace aos {
+namespace util {
+
+Thread::Thread() : started_(false), joined_(false), should_terminate_(false) {}
+
+Thread::~Thread() {
+ if (started_ && !joined_) {
+ assert(false);
+ }
+}
+
+void Thread::Start() {
+ assert(!started_);
+ started_ = true;
+ assert(pthread_create(&thread_, NULL, &Thread::StaticRun, this) == 0);
+}
+
+void Thread::Join() {
+ assert(!joined_ && started_);
+ joined_ = true;
+ {
+ MutexLocker locker(&should_terminate_mutex_);
+ should_terminate_ = true;
+ }
+ assert(pthread_join(thread_, NULL) == 0);
+}
+
+void *Thread::StaticRun(void *self) {
+ static_cast<Thread *>(self)->Run();
+ return NULL;
+}
+
+} // namespace util
+} // namespace aos
diff --git a/aos/common/util/thread.h b/aos/common/util/thread.h
new file mode 100644
index 0000000..10867ec
--- /dev/null
+++ b/aos/common/util/thread.h
@@ -0,0 +1,50 @@
+#ifndef AOS_COMMON_UTIL_THREAD_H_
+#define AOS_COMMON_UTIL_THREAD_H_
+
+#include "aos/common/mutex.h"
+
+namespace aos {
+namespace util {
+
+// A nice wrapper around a pthreads thread.
+//
+// TODO(aschuh): Test this.
+class Thread {
+ public:
+ Thread();
+ ~Thread();
+
+ // Actually creates the thread.
+ void Start();
+
+ // Asks the code to stop and then waits until it has done so.
+ void Join();
+
+ protected:
+ // Subclasses need to call this periodically if they are going to loop to
+ // check whether they have been asked to stop.
+ bool should_continue() {
+ MutexLocker locker(&should_terminate_mutex_);
+ return !should_terminate_;
+ }
+
+ private:
+ // Where subclasses actually do something.
+ //
+ // They should not block for long periods of time without checking
+ // should_continue().
+ virtual void Run() = 0;
+
+ static void *StaticRun(void *self);
+
+ pthread_t thread_;
+ bool started_;
+ bool joined_;
+ bool should_terminate_;
+ Mutex should_terminate_mutex_;
+};
+
+} // namespace util
+} // namespace aos
+
+#endif // AOS_COMMON_UTIL_THREAD_H_
diff --git a/aos/common/util/util.gyp b/aos/common/util/util.gyp
index 15f868f..c00b9e3 100644
--- a/aos/common/util/util.gyp
+++ b/aos/common/util/util.gyp
@@ -1,6 +1,19 @@
{
'targets': [
{
+ 'target_name': 'thread',
+ 'type': 'static_library',
+ 'sources': [
+ 'thread.cc',
+ ],
+ 'dependencies': [
+ '<(AOS)/common/common.gyp:mutex',
+ ],
+ 'export_dependent_settings': [
+ '<(AOS)/common/common.gyp:mutex',
+ ],
+ },
+ {
'target_name': 'trapezoid_profile',
'type': 'static_library',
'sources': [