spthreadpool.cpp
5.0 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Copyright 2007 Stephen Liu
* For license terms, see the file COPYING along with this library.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "spporting.hpp"
#include "spthreadpool.hpp"
typedef struct tagSP_Thread {
sp_thread_t mId;
sp_thread_mutex_t mMutex;
sp_thread_cond_t mCond;
SP_ThreadPool::DispatchFunc_t mFunc;
void * mArg;
SP_ThreadPool * mParent;
} SP_Thread_t;
SP_ThreadPool :: SP_ThreadPool( int maxThreads, const char * tag )
{
if( maxThreads <= 0 ) maxThreads = 2;
sp_thread_mutex_init( &mMainMutex, NULL );
sp_thread_cond_init( &mIdleCond, NULL );
sp_thread_cond_init( &mFullCond, NULL );
sp_thread_cond_init( &mEmptyCond, NULL );
mMaxThreads = maxThreads;
mIndex = 0;
mIsShutdown = 0;
mTotal = 0;
tag = NULL == tag ? "unknown" : tag;
mTag = strdup( tag );
mThreadList = ( SP_Thread_t ** )malloc( sizeof( void * ) * mMaxThreads );
memset( mThreadList, 0, sizeof( void * ) * mMaxThreads );
}
SP_ThreadPool :: ~SP_ThreadPool()
{
sp_thread_mutex_lock( &mMainMutex );
if( mIndex < mTotal ) {
sp_syslog( LOG_NOTICE, "[tp@%s] waiting for %d thread(s) to finish\n", mTag, mTotal - mIndex );
sp_thread_cond_wait( &mFullCond, &mMainMutex );
}
mIsShutdown = 1;
int i = 0;
for( i = 0; i < mIndex; i++ ) {
SP_Thread_t * thread = mThreadList[ i ];
sp_thread_mutex_lock( &thread->mMutex );
sp_thread_cond_signal( &thread->mCond ) ;
sp_thread_mutex_unlock ( &thread->mMutex );
}
if( mTotal > 0 ) {
sp_syslog( LOG_NOTICE, "[tp@%s] waiting for %d thread(s) to exit\n", mTag, mTotal );
sp_thread_cond_wait( &mEmptyCond, &mMainMutex );
}
sp_syslog( LOG_NOTICE, "[tp@%s] destroy %d thread structure(s)\n", mTag, mIndex );
for( i = 0; i < mIndex; i++ ) {
SP_Thread_t * thread = mThreadList[ i ];
sp_thread_mutex_destroy( &thread->mMutex );
sp_thread_cond_destroy( &thread->mCond );
free( thread );
mThreadList[ i ] = NULL;
}
sp_thread_mutex_unlock( &mMainMutex );
mIndex = 0;
sp_thread_mutex_destroy( &mMainMutex );
sp_thread_cond_destroy( &mIdleCond );
sp_thread_cond_destroy( &mFullCond );
sp_thread_cond_destroy( &mEmptyCond );
free( mThreadList );
mThreadList = NULL;
free( mTag );
mTag = NULL;
}
int SP_ThreadPool :: getMaxThreads()
{
return mMaxThreads;
}
int SP_ThreadPool :: dispatch( DispatchFunc_t dispatchFunc, void *arg )
{
int ret = 0;
sp_thread_attr_t attr;
SP_Thread_t * thread = NULL;
sp_thread_mutex_lock( &mMainMutex );
for( ; mIndex <= 0 && mTotal >= mMaxThreads; ) {
sp_thread_cond_wait( &mIdleCond, &mMainMutex );
}
if( mIndex <= 0 ) {
SP_Thread_t * thread = ( SP_Thread_t * )malloc( sizeof( SP_Thread_t ) );
memset( &thread->mId, 0, sizeof( thread->mId ) );
sp_thread_mutex_init( &thread->mMutex, NULL );
sp_thread_cond_init( &thread->mCond, NULL );
thread->mFunc = dispatchFunc;
thread->mArg = arg;
thread->mParent = this;
sp_thread_attr_init( &attr );
sp_thread_attr_setdetachstate( &attr, SP_THREAD_CREATE_DETACHED );
if( 0 == sp_thread_create( &( thread->mId ), &attr, wrapperFunc, thread ) ) {
mTotal++;
sp_syslog( LOG_NOTICE, "[tp@%s] create thread#%ld\n", mTag, thread->mId );
} else {
ret = -1;
sp_syslog( LOG_WARNING, "[tp@%s] cannot create thread\n", mTag );
sp_thread_mutex_destroy( &thread->mMutex );
sp_thread_cond_destroy( &thread->mCond );
free( thread );
}
sp_thread_attr_destroy( &attr );
} else {
mIndex--;
thread = mThreadList[ mIndex ];
mThreadList[ mIndex ] = NULL;
thread->mFunc = dispatchFunc;
thread->mArg = arg;
thread->mParent = this;
sp_thread_mutex_lock( &thread->mMutex );
sp_thread_cond_signal( &thread->mCond ) ;
sp_thread_mutex_unlock ( &thread->mMutex );
}
sp_thread_mutex_unlock( &mMainMutex );
return ret;
}
sp_thread_result_t SP_THREAD_CALL SP_ThreadPool :: wrapperFunc( void * arg )
{
SP_Thread_t * thread = ( SP_Thread_t * )arg;
for( ; 0 == thread->mParent->mIsShutdown; ) {
thread->mFunc( thread->mArg );
if( 0 != thread->mParent->mIsShutdown ) break;
sp_thread_mutex_lock( &thread->mMutex );
if( 0 == thread->mParent->saveThread( thread ) ) {
sp_thread_cond_wait( &thread->mCond, &thread->mMutex );
sp_thread_mutex_unlock( &thread->mMutex );
} else {
sp_thread_mutex_unlock( &thread->mMutex );
sp_thread_cond_destroy( &thread->mCond );
sp_thread_mutex_destroy( &thread->mMutex );
free( thread );
thread = NULL;
break;
}
}
if( NULL != thread ) {
sp_thread_mutex_lock( &thread->mParent->mMainMutex );
thread->mParent->mTotal--;
if( thread->mParent->mTotal <= 0 ) {
sp_thread_cond_signal( &thread->mParent->mEmptyCond );
}
sp_thread_mutex_unlock( &thread->mParent->mMainMutex );
}
return 0;
}
int SP_ThreadPool :: saveThread( SP_Thread_t * thread )
{
int ret = -1;
sp_thread_mutex_lock( &mMainMutex );
if( mIndex < mMaxThreads ) {
mThreadList[ mIndex ] = thread;
mIndex++;
ret = 0;
sp_thread_cond_signal( &mIdleCond );
if( mIndex >= mTotal ) {
sp_thread_cond_signal( &mFullCond );
}
}
sp_thread_mutex_unlock( &mMainMutex );
return ret;
}