Changeset 2495 for branches/vtkvis_threaded/ResponseQueue.cpp
- Timestamp:
- Sep 8, 2011, 8:03:53 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/vtkvis_threaded/ResponseQueue.cpp
r2488 r2495 1 2 #ifdef USE_THREADS 1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * Copyright (C) 2011, Purdue Research Foundation 4 * 5 * Author: George A. Howlett <gah@purdue.edu> 6 */ 3 7 4 8 #include <pthread.h> 5 9 #include <semaphore.h> 6 #include <errno.h> 7 #include <list> 8 #include <stdlib.h> 9 #include <errno.h> 10 #include <cerrno> 10 11 #include <cstring> 11 12 #include <cstdlib> 13 #include <list> 14 12 15 #include "Trace.h" 13 16 #include "ResponseQueue.h" 14 17 15 ResponseQueue::ResponseQueue(void *clientData) 18 using namespace Rappture::VtkVis; 19 20 ResponseQueue::ResponseQueue(void *clientData) : 21 _clientData(clientData) 16 22 { 17 23 pthread_mutex_init(&_idle, NULL); 18 24 if (sem_init(&_ready, 0, 0) < 0) { 19 25 ERROR("can't initialize semaphore: %s", strerror(errno)); 20 26 } 21 _clientData = clientData;22 27 } 23 28 24 ResponseQueue::~ResponseQueue( void)29 ResponseQueue::~ResponseQueue() 25 30 { 26 31 pthread_mutex_destroy(&_idle); 27 32 if (sem_destroy(&_ready) < 0) { 28 33 ERROR("can't destroy semaphore: %s", strerror(errno)); 29 34 } 30 35 } 31 36 32 37 void 33 ResponseQueue:: Enqueue(Response *responsePtr)38 ResponseQueue::enqueue(Response *response) 34 39 { 35 40 if (pthread_mutex_lock(&_idle) != 0) { 36 41 ERROR("can't lock mutex: %s", strerror(errno)); 37 42 } 38 43 /* Examine the list and remove any queued responses of the same type. */ 39 44 INFO("before # of elements is %d\n", _list.size()); 40 std::list<Response *>::iterator iter;41 for (iter = _list.begin(); iter != _list.end(); iter++) {42 43 44 if ((*iter)->Type() == responsePtr->Type()) {45 _list.erase(iter);46 45 for (std::list<Response *>::iterator itr = _list.begin(); 46 itr != _list.end(); ++itr) { 47 /* Remove any responses of the same type. There should be no more than 48 * one. */ 49 if ((*itr)->type() == response->type()) { 50 _list.erase(itr); 51 } 47 52 } 48 53 /* Add the new response to the end of the list. */ 49 _list.push_back(response Ptr);54 _list.push_back(response); 50 55 if (sem_post(&_ready) < 0) { 51 56 ERROR("can't post semaphore: %s", strerror(errno)); 52 57 } 53 58 if (pthread_mutex_unlock(&_idle) != 0) { 54 59 ERROR("can't unlock mutex: %s", strerror(errno)); 55 60 } 56 61 } 57 62 58 63 Response * 59 ResponseQueue:: Dequeue(void)64 ResponseQueue::dequeue() 60 65 { 61 Response *response Ptr;66 Response *response; 62 67 63 68 if (sem_wait(&_ready) < 0) { 64 69 ERROR("can't wait on semaphore: %s", strerror(errno)); 65 70 } 66 71 if (pthread_mutex_lock(&_idle) != 0) { 67 72 ERROR("can't lock mutex: %s", strerror(errno)); 68 73 } 69 response Ptr= _list.front();74 response = _list.front(); 70 75 _list.pop_front(); 71 76 if (pthread_mutex_unlock(&_idle) != 0) { 72 77 ERROR("can't unlock mutex: %s", strerror(errno)); 73 78 } 74 return response Ptr;79 return response; 75 80 } 76 77 #endif /*USE_THREADS*/
Note: See TracChangeset
for help on using the changeset viewer.