/**************************************************************************
*
- * Copyright 2011 Jose Fonseca
+ * Copyright 2011-2012 Jose Fonseca
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
};
+ /**
+ * Same interface as std::thread
+ */
+ class thread {
+ public:
+#ifdef _WIN32
+ /* FIXME */
+#else
+ typedef pthread_t native_handle_type;
+#endif
+
+ template< class Function, class Arg >
+ explicit thread( Function& f, Arg & arg ) {
+#ifdef _WIN32
+ /* FIXME */
+#else
+ pthread_create(&_native_handle, NULL, f, arg);
+#endif
+ }
+
+ inline void
+ join() {
+#ifdef _WIN32
+ /* FIXME */
+#else
+ pthread_join(_native_handle, NULL);
+#endif
+ }
+
+ private:
+ native_handle_type _native_handle;
+ };
+
} /* namespace os */
#endif /* _OS_THREAD_HPP_ */
class WorkQueueWork {
protected:
- friend class WorkQueue;
+ friend class WorkQueue;
public:
- virtual void run(void) = 0;
- virtual ~WorkQueueWork(void) { }
+ virtual void run(void) = 0;
+ virtual ~WorkQueueWork(void) { }
};
extern "C"
void *WorkQueue__entry_thunk(void *data);
class WorkQueue {
- pthread_t handle;
- std::queue<WorkQueueWork *> work_queue;
+ std::queue<WorkQueueWork *> work_queue;
- bool busy;
- bool exit_workqueue;
- os::condition_variable wake_cond;
- os::condition_variable complete_cond;
+ bool busy;
+ bool exit_workqueue;
+ os::condition_variable wake_cond;
+ os::condition_variable complete_cond;
- os::mutex mutex;
+ os::mutex mutex;
- void wake_up_thread(void);
- void thread_entry(void);
- int run_tasks(void);
- friend void *WorkQueue__entry_thunk(void *data);
+ os::thread thread;
+
+ void wake_up_thread(void);
+ void thread_entry(void);
+ int run_tasks(void);
+ friend void *WorkQueue__entry_thunk(void *data);
public:
- void queue_work(WorkQueueWork *work);
- void flush(void);
- void destroy(void);
+ void queue_work(WorkQueueWork *work);
+ void flush(void);
+ void destroy(void);
- WorkQueue(void);
- ~WorkQueue();
+ WorkQueue(void);
+ ~WorkQueue();
};
}
-#include <pthread.h>
#include <queue>
#include <assert.h>
mutex.unlock();
}
-extern "C"
void *WorkQueue__entry_thunk(void *data)
{
WorkQueue *thread = static_cast<WorkQueue *>(data);
}
WorkQueue::WorkQueue(void) :
- busy(false), exit_workqueue(false)
+ busy(false),
+ exit_workqueue(false),
+ thread(WorkQueue__entry_thunk, this)
{
- int err;
-
- err = pthread_create(&handle, NULL, WorkQueue__entry_thunk, this);
- assert(!err);
}
WorkQueue::~WorkQueue(void)
{
- pthread_join(handle, NULL);
+ thread.join();
}
}