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 | */ |
---|
7 | |
---|
8 | #include <pthread.h> |
---|
9 | #include <semaphore.h> |
---|
10 | #include <cerrno> |
---|
11 | #include <cstring> |
---|
12 | #include <cstdlib> |
---|
13 | #include <list> |
---|
14 | |
---|
15 | #include "Trace.h" |
---|
16 | #include "ResponseQueue.h" |
---|
17 | |
---|
18 | using namespace Rappture::VtkVis; |
---|
19 | |
---|
20 | ResponseQueue::ResponseQueue(void *clientData) : |
---|
21 | _clientData(clientData) |
---|
22 | { |
---|
23 | pthread_mutex_init(&_idle, NULL); |
---|
24 | if (sem_init(&_ready, 0, 0) < 0) { |
---|
25 | ERROR("can't initialize semaphore: %s", strerror(errno)); |
---|
26 | } |
---|
27 | } |
---|
28 | |
---|
29 | ResponseQueue::~ResponseQueue() |
---|
30 | { |
---|
31 | TRACE("Deleting ResponseQueue"); |
---|
32 | pthread_mutex_destroy(&_idle); |
---|
33 | if (sem_destroy(&_ready) < 0) { |
---|
34 | ERROR("can't destroy semaphore: %s", strerror(errno)); |
---|
35 | } |
---|
36 | for (std::list<Response *>::iterator itr = _list.begin(); |
---|
37 | itr != _list.end(); ++itr) { |
---|
38 | delete *itr; |
---|
39 | } |
---|
40 | _list.clear(); |
---|
41 | } |
---|
42 | |
---|
43 | void |
---|
44 | ResponseQueue::enqueue(Response *response) |
---|
45 | { |
---|
46 | if (pthread_mutex_lock(&_idle) != 0) { |
---|
47 | ERROR("can't lock mutex: %s", strerror(errno)); |
---|
48 | } |
---|
49 | /* Examine the list and remove any queued responses of the same type. */ |
---|
50 | TRACE("before # of elements is %d\n", _list.size()); |
---|
51 | for (std::list<Response *>::iterator itr = _list.begin(); |
---|
52 | itr != _list.end(); ++itr) { |
---|
53 | /* Remove any responses of the same type. There should be no more than |
---|
54 | * one. */ |
---|
55 | if ((*itr)->type() == response->type()) { |
---|
56 | _list.erase(itr); |
---|
57 | } |
---|
58 | } |
---|
59 | /* Add the new response to the end of the list. */ |
---|
60 | _list.push_back(response); |
---|
61 | if (sem_post(&_ready) < 0) { |
---|
62 | ERROR("can't post semaphore: %s", strerror(errno)); |
---|
63 | } |
---|
64 | if (pthread_mutex_unlock(&_idle) != 0) { |
---|
65 | ERROR("can't unlock mutex: %s", strerror(errno)); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | Response * |
---|
70 | ResponseQueue::dequeue() |
---|
71 | { |
---|
72 | Response *response = NULL; |
---|
73 | |
---|
74 | if (sem_wait(&_ready) < 0) { |
---|
75 | ERROR("can't wait on semaphore: %s", strerror(errno)); |
---|
76 | } |
---|
77 | if (pthread_mutex_lock(&_idle) != 0) { |
---|
78 | ERROR("can't lock mutex: %s", strerror(errno)); |
---|
79 | } |
---|
80 | if (_list.empty()) { |
---|
81 | ERROR("Empty queue"); |
---|
82 | } else { |
---|
83 | response = _list.front(); |
---|
84 | _list.pop_front(); |
---|
85 | } |
---|
86 | if (pthread_mutex_unlock(&_idle) != 0) { |
---|
87 | ERROR("can't unlock mutex: %s", strerror(errno)); |
---|
88 | } |
---|
89 | return response; |
---|
90 | } |
---|