source: branches/blt4/packages/vizservers/vtkvis/ResponseQueue.cpp @ 2543

Last change on this file since 2543 was 2543, checked in by gah, 13 years ago

update from trunk

File size: 2.1 KB
Line 
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
18using namespace Rappture::VtkVis;
19
20ResponseQueue::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
29ResponseQueue::~ResponseQueue()
30{
31    pthread_mutex_destroy(&_idle);
32    if (sem_destroy(&_ready) < 0) {
33        ERROR("can't destroy semaphore: %s", strerror(errno));
34    }
35}
36
37void
38ResponseQueue::enqueue(Response *response)
39{
40    if (pthread_mutex_lock(&_idle) != 0) {
41        ERROR("can't lock mutex: %s", strerror(errno));
42    }   
43    /* Examine the list and remove any queued responses of the same type. */
44    TRACE("before # of elements is %d\n", _list.size());
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        }
52    }
53    /* Add the new response to the end of the list. */
54    _list.push_back(response);
55    if (sem_post(&_ready) < 0) {
56        ERROR("can't post semaphore: %s", strerror(errno));
57    }
58    if (pthread_mutex_unlock(&_idle) != 0) {
59        ERROR("can't unlock mutex: %s", strerror(errno));
60    }   
61}
62
63Response *
64ResponseQueue::dequeue()
65{
66    Response *response;
67
68    if (sem_wait(&_ready) < 0) {
69        ERROR("can't wait on semaphore: %s", strerror(errno));
70    }
71    if (pthread_mutex_lock(&_idle) != 0) {
72        ERROR("can't lock mutex: %s", strerror(errno));
73    }   
74    response = _list.front();
75    _list.pop_front();
76    if (pthread_mutex_unlock(&_idle) != 0) {
77        ERROR("can't unlock mutex: %s", strerror(errno));
78    }   
79    return response;
80}
Note: See TracBrowser for help on using the repository browser.