Skyld AV  0.6
On access virus scanning for Linux
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ThreadPool.cc
Go to the documentation of this file.
1 /*
2  * File: ThreadPool.cc
3  *
4  * Copyright 2013 Heinrich Schuchardt <xypron.glpk@gmx.de>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
23 #include <pthread.h>
24 #include <time.h>
25 #include "ThreadPool.h"
26 
33 ThreadPool::ThreadPool(int nThreads, void* (*workRoutine) (void *)) {
34  int i;
35 
36  thread_count = 0;
37  status = RUNNING;
38  this->workRoutine = workRoutine;
39  pthread_mutex_init(&mutexThread, NULL);
40  pthread_mutex_init(&mutexWorker, NULL);
41  pthread_mutex_init(&mutexWorkItem, NULL);
42  pthread_cond_init(&cond, NULL);
43 
44  for (i = 0; i < nThreads; i++) {
45  createThread();
46  }
47  return;
48 }
49 
55 void ThreadPool::add(void *workItem) {
56  pthread_mutex_lock(&mutexWorkItem);
57  worklist.push_back(workItem);
58  pthread_cond_signal(&cond);
59  pthread_mutex_unlock(&mutexWorkItem);
60 }
61 
68  int ret;
69  pthread_t thread;
70 
71  if (pthread_create(&thread, NULL, worker, this)) {
72  ret = 1;
73  } else {
74  pthread_mutex_lock(&mutexThread);
75  thread_count++;
76  pthread_mutex_unlock(&mutexThread);
77  ret = 0;
78  }
79  return ret;
80 }
81 
87 void ThreadPool::exitThread(void *retval) {
88  pthread_mutex_lock(&mutexThread);
89  thread_count--;
90  pthread_mutex_unlock(&mutexThread);
91  pthread_cond_signal(&cond);
92  pthread_exit(retval);
93 }
94 
101  void *ret = NULL;
102 
103  if (pthread_mutex_lock(&mutexWorkItem)) {
104  return NULL;
105  }
106  if (worklist.size()) {
107  ret = worklist[0];
108  worklist.pop_front();
109  }
110  pthread_mutex_unlock(&mutexWorkItem);
111  return ret;
112 }
113 
120  long ret = -1;
121  if (pthread_mutex_lock(&mutexWorkItem)) {
122  return ret;
123  }
124  ret = worklist.size();
125  pthread_mutex_unlock(&mutexWorkItem);
126  return ret;
127 }
128 
135  return status == STOPPING;
136 }
137 
144 void * ThreadPool::worker(void *threadPool) {
145  ThreadPool *tp;
146 
147  tp = static_cast<ThreadPool *> (threadPool);
148 
149  for (;;) {
150  void *workitem;
151  pthread_mutex_lock(&tp->mutexWorker);
152  while (!tp->isStopping() && tp->worklist.size() == 0) {
153  pthread_cond_wait(&tp->cond, &tp->mutexWorker);
154  }
155  pthread_mutex_unlock(&tp->mutexWorker);
156  workitem = tp->getWorkItem();
157  if (workitem != NULL) {
158  if (tp->workRoutine) {
159  (*tp->workRoutine)(workitem);
160  }
161  } else if (tp->isStopping()) {
162  break;
163  }
164  }
165  tp->exitThread(NULL);
166  return NULL;
167 }
168 
173  int n;
174  struct timespec interval = {
175  0,
176  1000000
177  };
178 
179  pthread_mutex_lock(&mutexWorker);
180  status = STOPPING;
181  pthread_mutex_unlock(&mutexWorker);
182 
183  do {
184  pthread_cond_signal(&cond);
185  nanosleep(&interval, NULL);
186  pthread_mutex_lock(&mutexThread);
187  n = thread_count;
188  pthread_mutex_unlock(&mutexThread);
189  } while (n > 0);
190 
191  pthread_cond_destroy(&cond);
192  pthread_mutex_destroy(&mutexWorkItem);
193  pthread_mutex_destroy(&mutexWorker);
194  pthread_mutex_destroy(&mutexThread);
195  return;
196 }
197 
Implements the thread pool pattern.
pthread_mutex_t mutexThread
Definition: ThreadPool.h:54
static pthread_t thread
thread.
Definition: MountPolling.cc:51
void *(* workRoutine)(void *)
Definition: ThreadPool.h:59
static void * worker(void *)
Working thread.
Definition: ThreadPool.cc:144
pthread_mutex_t mutexWorker
Definition: ThreadPool.h:55
pthread_mutex_t mutexWorkItem
Definition: ThreadPool.h:56
ThreadPool(int nThreads, void *(*workRoutine)(void *))
Creates a new thread pool.
Definition: ThreadPool.cc:33
virtual ~ThreadPool()
Deletes thread pool.
Definition: ThreadPool.cc:172
std::deque< void * > worklist
Definition: ThreadPool.h:58
Implements the thread pool pattern.
Definition: ThreadPool.h:35
int isStopping()
Is thread pool stopping.
Definition: ThreadPool.cc:134
long getWorklistSize()
Gets size of worklist.
Definition: ThreadPool.cc:119
void exitThread(void *retval)
Exits a worker thread.
Definition: ThreadPool.cc:87
void * getWorkItem()
Gets a work item.
Definition: ThreadPool.cc:100
int createThread()
Creates a new worker thread.
Definition: ThreadPool.cc:67
void add(void *workItem)
Adds a work item to the work list.
Definition: ThreadPool.cc:55
pthread_cond_t cond
Definition: ThreadPool.h:52
int thread_count
Definition: ThreadPool.h:57