summaryrefslogtreecommitdiff
path: root/src/mt/stackque.h
diff options
context:
space:
mode:
authorHenrik Rydberg <rydberg@euromail.se>2011-10-08 20:30:28 +0200
committerHenrik Rydberg <rydberg@euromail.se>2011-10-08 20:30:28 +0200
commit5df79c53745fde5d6c3340a2979b1429cd5892c1 (patch)
tree1a81af141708b826e9c61e8a04019994fcca8298 /src/mt/stackque.h
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/mt/stackque.h')
-rw-r--r--src/mt/stackque.h64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/mt/stackque.h b/src/mt/stackque.h
new file mode 100644
index 0000000..349cb4f
--- /dev/null
+++ b/src/mt/stackque.h
@@ -0,0 +1,64 @@
1/*************************************************************************
2 *
3 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifndef STACKQUEH
21#define STACKQUEH
22
23#include <mt/mstring.h>
24#include <mt/mlock.h>
25
26template <class T> class stackque {
27public:
28 stackque(int m) {
29 put=get=n=0;
30 base=new T[caps=m];
31 memset(&mutex,0,sizeof(mutex_t));
32 }
33 ~stackque() { delete base; }
34
35 int push(const T& a) throw(merror_t) {
36 MLOCK(mutex);
37 if(n>=caps) THROW("stackque: Full");
38 base[put++]=a;
39 if(put==caps) put=0;
40 return ++n;
41 }
42
43 int pop(T& a) {
44 MLOCK(mutex);
45 if(n) {
46 a=base[get++];
47 if(get==caps) get=0;
48 return n--;
49 }
50 else return 0;
51 }
52
53 int capacity() const { return caps; }
54 int size() const { return n; }
55 int empty() const { return n==0; }
56 int full() const { return n==caps; }
57 void clear() { put=get=n=0; }
58private:
59 int put,get,n,caps;
60 T* base;
61 mutex_t mutex;
62};
63
64#endif