touched up QSE::Thread

This commit is contained in:
2018-01-27 05:58:20 +00:00
parent 5a3586232f
commit 8256cee77f
7 changed files with 158 additions and 83 deletions

View File

@ -35,13 +35,14 @@
QSE_BEGIN_NAMESPACE(QSE)
class Thread: protected qse_thr_t, public Uncopyable
{
public:
// native thread hadnle type
typedef qse_thr_hnd_t Handle;
typedef int (*ThreadRoutine) (Thread* thr);
enum State
{
INCUBATING = QSE_THR_INCUBATING,
@ -69,44 +70,58 @@ public:
void setStackSize (qse_size_t num) QSE_CPP_NOEXCEPT { qse_thr_setstacksize(this, num); }
#if 0
static int call_lambda (QSE::Thread* thr)
#if (__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER >= 1900) //C++11 or later
using ThreadLambda = std::function<int(QSE::Thread*)>;
static int call_lambda (qse_thr_t* thr, void* ctx)
{
return thr->x_func (thr);
Thread* t = (Thread*)ctx;
return t->__tmplam (t);
}
int startl (ThreadLambda&& f, int flags)
{
this->__tmplam = QSE_CPP_RVREF(f);
return qse_thr_start (this, (qse_thr_rtn_t)Thread::call_lambda, this, flags);
}
#if 0
static int call_lambda_lx (qse_thr_t* thr, void* ctx)
{
Thread* t = (Thread*)ctx;
//return ([]int(QSE::Thread*))t->__tmpvoid (t);
}
template <typename F>
int start (F&& f, int flags)
{
this->x_func = std::bind(f);
return qse_thr_start (this, (qse_thr_rtn_t)Thread::call_lambda, flags);
int startlx (F&& f, int flags)
{
this->__tmplam = QSE_CPP_RVREF(f);
auto xx = QSE_CPP_RVREF(f);
this->__tmpvoid = (void*)&xx;
return qse_thr_start (this, (qse_thr_rtn_t)Thread::call_lambda_lx, this, flags);
}
#endif
#if (__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER >= 1900) //C++11 or later
using lfunc_t = std::function<int(QSE::Thread*thr)>;
static int call_lambda (QSE::Thread* thr)
{
return thr->x_func (thr);
}
int start (lfunc_t f, int flags)
{
this->x_func = std::move(f);
return qse_thr_start (this, (qse_thr_rtn_t)Thread::call_lambda, flags);
}
//std::function<int(QSE::Thread*)> x_func;
lfunc_t x_func;
#endif
// execute the given function in a thread.
virtual int start (ThreadRoutine rtn, int flags = 0) QSE_CPP_NOEXCEPT;
// execute the main method defined in this class in a thread.
virtual int start (int flags = 0) QSE_CPP_NOEXCEPT;
virtual int stop () QSE_CPP_NOEXCEPT;
virtual int main () { return 0; }
// return the context pointer value
const void* getContext () const { return this->__exctx; }
void* getContext () { return this->__exctx; }
// change the context pointer value
void setContext (void* ctx) { this->__exctx = ctx; }
int join () QSE_CPP_NOEXCEPT { return qse_thr_join(this); }
int detach () QSE_CPP_NOEXCEPT { return qse_thr_detach(this); }
@ -125,7 +140,16 @@ public:
int unblockAllSignals () QSE_CPP_NOEXCEPT { return qse_thr_unblockallsigs (this); }
protected:
void* __exctx;
ThreadRoutine __tmprtn;
#if (__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER >= 1900) //C++11 or later
ThreadLambda __tmplam;
// void* __tmpvoid;
#endif
static Handle INVALID_HANDLE;
static int thr_func_call_rtn (qse_thr_t* rtn, void* ctx);
};

View File

@ -40,7 +40,7 @@ typedef struct qse_thr_t qse_thr_t;
* qse_thr_open() and qse_thr_start(). When it is executed, the pointer to the
* calling thread object is passed as its first argument.
*/
typedef int (*qse_thr_rtn_t) (qse_thr_t*);
typedef int (*qse_thr_rtn_t) (qse_thr_t* thr, void* ctx);
enum qse_thr_state_t
{
@ -98,11 +98,13 @@ struct qse_thr_t
{
qse_mmgr_t* mmgr;
qse_thr_rtn_t __main_routine;
qse_thr_rtn_t __temp_routine;
unsigned int __flags;
qse_size_t __stacksize;
qse_thr_rtn_t __main_routine;
void* __ctx;
qse_thr_hnd_t __handle;
qse_thr_state_t __state;
@ -172,6 +174,7 @@ QSE_EXPORT void qse_thr_setstacksize (
QSE_EXPORT int qse_thr_start (
qse_thr_t* thr,
qse_thr_rtn_t func,
void* ctx,
int flags /**< 0 or bitwise-or of the #qse_thr_flag_t enumerators */
);