spexecutor.cpp
3.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2007 Stephen Liu
* For license terms, see the file COPYING along with this library.
*/
#include <sys/types.h>
#include <assert.h>
#include "spporting.hpp"
#include "spexecutor.hpp"
#include "spthreadpool.hpp"
#include "sputils.hpp"
SP_Task :: ~SP_Task()
{
}
//===================================================================
SP_SimpleTask :: SP_SimpleTask( ThreadFunc_t func, void * arg, int deleteAfterRun )
{
mFunc = func;
mArg = arg;
mDeleteAfterRun = deleteAfterRun;
}
SP_SimpleTask :: ~SP_SimpleTask()
{
}
void SP_SimpleTask :: run()
{
mFunc( mArg );
if( mDeleteAfterRun ) delete this;
}
//===================================================================
SP_Executor :: SP_Executor( int maxThreads, const char * tag )
{
tag = NULL == tag ? "unknown" : tag;
mThreadPool = new SP_ThreadPool( maxThreads, tag );
mQueue = new SP_BlockingQueue();
mIsShutdown = 0;
sp_thread_mutex_init( &mMutex, NULL );
sp_thread_cond_init( &mCond, NULL );
sp_thread_attr_t attr;
sp_thread_attr_init( &attr );
assert( sp_thread_attr_setstacksize( &attr, 1024 * 1024 ) == 0 );
sp_thread_attr_setdetachstate( &attr, SP_THREAD_CREATE_DETACHED );
sp_thread_t thread;
int ret = sp_thread_create( &thread, &attr, eventLoop, this );
sp_thread_attr_destroy( &attr );
if( 0 == ret ) {
sp_syslog( LOG_NOTICE, "[ex@%s] Thread #%ld has been created for executor", tag, thread );
} else {
sp_syslog( LOG_WARNING, "[ex@%s] Unable to create a thread for executor", tag );
}
}
SP_Executor :: ~SP_Executor()
{
shutdown();
while( 2 != mIsShutdown ) {
sp_thread_mutex_lock( &mMutex );
sp_thread_cond_wait( &mCond, &mMutex );
sp_thread_mutex_unlock( &mMutex );
}
sp_thread_mutex_destroy( &mMutex );
sp_thread_cond_destroy( &mCond);
delete mThreadPool;
mThreadPool = NULL;
delete mQueue;
mQueue = NULL;
}
void SP_Executor :: shutdown()
{
sp_thread_mutex_lock( &mMutex );
if( 0 == mIsShutdown ) {
mIsShutdown = 1;
// signal the event loop to wake up
execute( worker, NULL );
}
sp_thread_mutex_unlock( &mMutex );
}
sp_thread_result_t SP_THREAD_CALL SP_Executor :: eventLoop( void * arg )
{
SP_Executor * executor = ( SP_Executor * )arg;
while( 0 == executor->mIsShutdown ) {
void * queueData = executor->mQueue->pop();
if( executor->mThreadPool->getMaxThreads() > 1 ) {
if( 0 != executor->mThreadPool->dispatch( worker, queueData ) ) {
worker( queueData );
}
} else {
worker( queueData );
}
}
sp_thread_mutex_lock( &executor->mMutex );
executor->mIsShutdown = 2;
sp_thread_cond_signal( &executor->mCond );
sp_thread_mutex_unlock( &executor->mMutex );
return 0;
}
void SP_Executor :: worker( void * arg )
{
if( NULL != arg ) {
SP_Task * task = ( SP_Task * )arg;
task->run();
}
}
void SP_Executor :: execute( SP_Task * task )
{
mQueue->push( task );
}
void SP_Executor :: execute( void ( * func ) ( void * ), void * arg )
{
SP_SimpleTask * task = new SP_SimpleTask( func, arg, 1 );
execute( task );
}
int SP_Executor :: getQueueLength()
{
return mQueue->getLength();
}