File: ThreadQueue.hh

package info (click to toggle)
3dwm 0.3.1-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,368 kB
  • ctags: 2,547
  • sloc: cpp: 11,981; sh: 7,850; ansic: 851; makefile: 287; yacc: 135; lex: 92
file content (128 lines) | stat: -rw-r--r-- 3,437 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* ------------------------------------------------------------------------
 * $Id: ThreadQueue.hh,v 1.6 2001/07/18 07:47:49 elm Exp $
 *
 * This file is part of 3Dwm: The Three-Dimensional User Environment.
 *
 * 3Dwm: The Three-Dimensional User Environment:
 *	<http://www.3dwm.org>
 *
 * Chalmers Medialab
 * 	<http://www.medialab.chalmers.se>
 * 
 * ------------------------------------------------------------------------
 * File created 2000-06-19 by Niklas Elmqvist.
 *
 * Copyright (c) 2000 Niklas Elmqvist <elm@3dwm.org>.
 * ------------------------------------------------------------------------
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 * ------------------------------------------------------------------------
 */

#ifndef _ThreadQueue_hh_
#define _ThreadQueue_hh_

// -- System Includes
#include <queue>

// -- Celsius Includes
#include "Mutex.hh"
#include "Semaphore.hh"
#include "Exception.hh"
#include "Guard.hh"

// -- Template Definitions

/**
 * Simple thread queue implemented using POSIX mutexes and semaphores.
 * This is the basic building-block of any producer-consumer scheme
 * for making two threads talk to each other.
 **/
template <class T>
class ThreadQueue : private std::queue<T> {
    
private:
    
    typedef std::queue<T> rep_type;
    
public: 

    /**
     * Constructor.
     *
     * @param capacity the maximum number of items in the queue.
     **/
    ThreadQueue(size_t capacity) : _allowWrite(capacity), _allowRead(0) { }

    /**
     * Push an element to the end of the queue.
     * 
     * @param t element to push.
     **/
    void push(const T &t) {
	_allowWrite.lock();
	rep_type::push(t);
	_allowRead.unlock();
    }

    /**
     * Erase the element at the front of the queue.
     **/
    T pop() {
	_allowRead.lock();
	Guard<Mutex> guard(_mutex);
	T t = rep_type::front();
	rep_type::pop();
	_allowWrite.unlock();
	return t;
    }

    /**
     * Try to retrieve (and remove) the element at the front of the
     * queue. If there is nothing to retrieve (i.e. the queue is
     * empty), signal this to the caller.
     *
     * @param t element at the top of the queue.
     * @return true if the operation was successful, false if not.
     **/
    bool try_top_pop(T &t) {
	if (_allowRead.tryLock() == true) {
	    Guard<Mutex> guard(_mutex);
	    t = rep_type::top();
	    rep_type::pop();
	    _allowWrite.unlock();
	    return true;
	}
	else
	    return false;
    }
    
    /**
     * Retrieve the size of the queue.
     *
     * @return the queue size.
     **/
    size_t size() const {
	Guard<Mutex> guard(_mutex);
	return rep_type::size(); 
    }
    
private:
    Semaphore _allowWrite;
    Semaphore _allowRead;
    Mutex _mutex;
};

#endif /* ThreadQueue.hh */