summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2011-10-13 23:52:31 -0700
committerPixel <pixel@nobis-crew.org>2011-10-14 00:00:31 -0700
commit13684e6c58ed3f0bd6fd811125e31fc3a37bda21 (patch)
tree8c25808ab48926aefb2f2bafa70422365665b312 /src
parent3cf44918b7d7f1cba6a71bdbe1ecb3742941b034 (diff)
Adding a Thread and a Queue class.
Diffstat (limited to 'src')
-rw-r--r--src/Threads.cc45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/Threads.cc b/src/Threads.cc
index fa3f0f3..5f07040 100644
--- a/src/Threads.cc
+++ b/src/Threads.cc
@@ -1,9 +1,20 @@
#include "Exceptions.h"
#include "Threads.h"
+#include "Local.h"
+
+namespace Balau {
+
+class ThreadHelper {
+ public:
+ static void * threadProc(void * arg);
+};
+
+}
Balau::Lock::Lock() {
int r;
pthread_mutexattr_t attr;
+
r = pthread_mutexattr_init(&attr);
Assert(r == 0);
r = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
@@ -11,3 +22,37 @@ Balau::Lock::Lock() {
r = pthread_mutex_init(&m_lock, &attr);
Assert(r == 0);
}
+
+void * Balau::ThreadHelper::threadProc(void * arg) {
+ void * tls = g_tlsManager->createTLS();
+ g_tlsManager->setTLS(tls);
+ Balau::Thread * thread = reinterpret_cast<Balau::Thread *>(arg);
+ void * r = thread->proc();
+ free(tls);
+ return r;
+}
+
+Balau::Thread::~Thread() {
+ join();
+}
+
+void * Balau::Thread::join() {
+ void * r = NULL;
+ if (!m_joined) {
+ m_joined = true;
+ pthread_join(m_thread, &r);
+ }
+ return r;
+}
+
+void Balau::Thread::threadStart() {
+ pthread_attr_t attr;
+ int r;
+
+ r = pthread_attr_init(&attr);
+ Assert(r == 0);
+ r = pthread_create(&m_thread, &attr, Balau::ThreadHelper::threadProc, this);
+ Assert(r == 0);
+ r = pthread_attr_destroy(&attr);
+ Assert(r == 0);
+}