|
| 1 | +#include "../include/thread_pool.h" |
| 2 | + |
| 3 | +ThreadPool::ThreadPool(int numberOfThreads, uv_loop_t *loop) { |
| 4 | + uv_mutex_init(&workMutex); |
| 5 | + uv_sem_init(&workSemaphore, 0); |
| 6 | + |
| 7 | + uv_async_init(loop, &loopAsync, RunLoopCallbacks); |
| 8 | + loopAsync.data = this; |
| 9 | + uv_unref((uv_handle_t *)&loopAsync); |
| 10 | + uv_mutex_init(&loopMutex); |
| 11 | + |
| 12 | + for(int i=0; i<numberOfThreads; i++) { |
| 13 | + uv_thread_t thread; |
| 14 | + uv_thread_create(&thread, RunEventQueue, this); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +void ThreadPool::QueueWork(Callback workCallback, Callback loopCallback, void *data) { |
| 19 | + // there is work on the thread pool - reference the handle so |
| 20 | + // node doesn't terminate |
| 21 | + uv_ref((uv_handle_t *)&loopAsync); |
| 22 | + uv_mutex_lock(&workMutex); |
| 23 | + workQueue.push(Work(workCallback, loopCallback, data)); |
| 24 | + uv_mutex_unlock(&workMutex); |
| 25 | + uv_sem_post(&workSemaphore); |
| 26 | +} |
| 27 | + |
| 28 | +void ThreadPool::RunEventQueue(void *threadPool) { |
| 29 | + static_cast<ThreadPool *>(threadPool)->RunEventQueue(); |
| 30 | +} |
| 31 | + |
| 32 | +void ThreadPool::RunEventQueue() { |
| 33 | + for ( ; ; ) { |
| 34 | + // wait until there is work to do |
| 35 | + uv_sem_wait(&workSemaphore); |
| 36 | + uv_mutex_lock(&workMutex); |
| 37 | + // the semaphore should guarantee that queue is not empty |
| 38 | + Work work = workQueue.front(); |
| 39 | + workQueue.pop(); |
| 40 | + uv_mutex_unlock(&workMutex); |
| 41 | + |
| 42 | + // perform the queued work |
| 43 | + (*work.workCallback)(work.data); |
| 44 | + |
| 45 | + // schedule the callback on the loop |
| 46 | + uv_mutex_lock(&loopMutex); |
| 47 | + loopQueue.push(work); |
| 48 | + uv_mutex_unlock(&loopMutex); |
| 49 | + uv_async_send(&loopAsync); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +void ThreadPool::RunLoopCallbacks(uv_async_t* handle) { |
| 54 | + static_cast<ThreadPool *>(handle->data)->RunLoopCallbacks(); |
| 55 | +} |
| 56 | + |
| 57 | +void ThreadPool::RunLoopCallbacks() { |
| 58 | + // uv_async_send can coalesce calls, so we are not guaranteed one |
| 59 | + // RunLoopCallbacks per uv_async_send call |
| 60 | + // so we always process the entire loopQueue |
| 61 | + uv_mutex_lock(&loopMutex); |
| 62 | + while(!loopQueue.empty()) { |
| 63 | + Work work = loopQueue.front(); |
| 64 | + loopQueue.pop(); |
| 65 | + uv_mutex_unlock(&loopMutex); |
| 66 | + // perform the queued loop callback |
| 67 | + (*work.loopCallback)(work.data); |
| 68 | + uv_mutex_lock(&loopMutex); |
| 69 | + } |
| 70 | + uv_mutex_lock(&workMutex); |
| 71 | + // if both the workQueue and the loopQueue are empty, node doesn't need |
| 72 | + // to be prevented from terminating |
| 73 | + if(workQueue.empty()) { |
| 74 | + uv_unref((uv_handle_t *)&loopAsync); |
| 75 | + } |
| 76 | + uv_mutex_unlock(&workMutex); |
| 77 | + uv_mutex_unlock(&loopMutex); |
| 78 | +} |
0 commit comments