spexecutor.hpp
1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Copyright 2007 Stephen Liu
* For license terms, see the file COPYING along with this library.
*/
#ifndef __spexecutor_hpp__
#define __spexecutor_hpp__
#include "spthread.hpp"
class SP_ThreadPool;
class SP_BlockingQueue;
class SP_Task {
public:
virtual ~SP_Task();
virtual void run() = 0;
};
class SP_SimpleTask : public SP_Task {
public:
typedef void ( * ThreadFunc_t ) ( void * );
SP_SimpleTask( ThreadFunc_t func, void * arg, int deleteAfterRun );
virtual ~SP_SimpleTask();
virtual void run();
private:
ThreadFunc_t mFunc;
void * mArg;
int mDeleteAfterRun;
};
class SP_Executor {
public:
SP_Executor( int maxThreads, const char * tag = 0 );
~SP_Executor();
void execute( SP_Task * task );
void execute( void ( * func ) ( void * ), void * arg );
int getQueueLength();
void shutdown();
private:
static void msgQueueCallback( void * queueData, void * arg );
static void worker( void * arg );
static sp_thread_result_t SP_THREAD_CALL eventLoop( void * arg );
SP_ThreadPool * mThreadPool;
SP_BlockingQueue * mQueue;
int mIsShutdown;
sp_thread_mutex_t mMutex;
sp_thread_cond_t mCond;
};
#endif