File: EmThreadSafeQueue.cpp

package info (click to toggle)
pose 3.5-1
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 13,108 kB
  • ctags: 29,485
  • sloc: cpp: 93,990; ansic: 62,838; sh: 1,970; perl: 1,891; python: 1,242; makefile: 616
file content (186 lines) | stat: -rw-r--r-- 4,876 bytes parent folder | download | duplicates (3)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 2000-2001 Palm, Inc. or its subsidiaries.
	All rights reserved.

	This file is part of the Palm OS Emulator.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
\* ===================================================================== */

#include "EmCommon.h"
#include "EmThreadSafeQueue.h"


// ---------------------------------------------------------------------------
//		 EmThreadSafeQueue
// ---------------------------------------------------------------------------

template <class T>
EmThreadSafeQueue<T>::EmThreadSafeQueue (int maxSize) :
	fMutex (),
#if HAS_OMNI_THREAD
	fAvailable (&fMutex),
#endif
	fContainer (),
	fMaxSize (maxSize)
{
}


// ---------------------------------------------------------------------------
//		 EmThreadSafeQueue
// ---------------------------------------------------------------------------

template <class T>
EmThreadSafeQueue<T>::~EmThreadSafeQueue (void)
{
}


// ---------------------------------------------------------------------------
//		 EmThreadSafeQueue::Put
// ---------------------------------------------------------------------------

template <class T>
void EmThreadSafeQueue<T>::Put (const T& value)
{
	omni_mutex_lock	lock (fMutex);

	EmAssert (fMaxSize == 0 || (long) fContainer.size () < fMaxSize);

	fContainer.push_back (value);

#if HAS_OMNI_THREAD
	// Tell clients that there may be new data in the buffer.
	fAvailable.signal ();
#endif
}


// ---------------------------------------------------------------------------
//		 EmThreadSafeQueue::Get
// ---------------------------------------------------------------------------

template <class T>
T EmThreadSafeQueue<T>::Get (void)
{
	omni_mutex_lock	lock (fMutex);

	// Make sure there's something in the queue (this shouldn't happen,
	// because the caller should always call GetUsed before Get).

	EmAssert (fContainer.size () > 0);

	T	result = fContainer[0];
	fContainer.pop_front ();

	return result;
}


// ---------------------------------------------------------------------------
//		 EmThreadSafeQueue::Peek
// ---------------------------------------------------------------------------

template <class T>
T EmThreadSafeQueue<T>::Peek (void)
{
	omni_mutex_lock	lock (fMutex);

	// Make sure there's something in the queue (this shouldn't happen,
	// because the caller should always call GetUsed before Get).

	EmAssert (fContainer.size () > 0);

	T	result = fContainer[0];

	return result;
}


// ---------------------------------------------------------------------------
//		 EmThreadSafeQueue::GetFree
// ---------------------------------------------------------------------------

template <class T>
int EmThreadSafeQueue<T>::GetFree (void)
{
	omni_mutex_lock	lock (fMutex);

	return fMaxSize - fContainer.size ();
}


// ---------------------------------------------------------------------------
//		 EmThreadSafeQueue::GetUsed
// ---------------------------------------------------------------------------

template <class T>
int EmThreadSafeQueue<T>::GetUsed (void)
{
	omni_mutex_lock	lock (fMutex);

	return fContainer.size ();
}


// ---------------------------------------------------------------------------
//		 EmThreadSafeQueue::WaitForDataAvailable
// ---------------------------------------------------------------------------

template <class T>
Bool EmThreadSafeQueue<T>::WaitForDataAvailable (long timeoutms)
{
#if HAS_OMNI_THREAD

	// calc absolute time to wait until:
	unsigned long	sec;
	unsigned long	nanosec;
	omni_thread::get_time (&sec, &nanosec, 0, timeoutms * 1000);

	return fAvailable.timedwait (sec, nanosec) == 1;

#else

	UNUSED_PARAM (timeoutms)
	return fContainer.size () > 0;

#endif
}


// ---------------------------------------------------------------------------
//		 EmThreadSafeQueue::Clear
// ---------------------------------------------------------------------------

template <class T>
void EmThreadSafeQueue<T>::Clear (void)
{
	omni_mutex_lock	lock (fMutex);

	fContainer.clear ();
}


// ---------------------------------------------------------------------------
//		 EmThreadSafeQueue::GetMaxSize
// ---------------------------------------------------------------------------

template <class T>
int EmThreadSafeQueue<T>::GetMaxSize (void)
{
	return fMaxSize;
}

// Instantiate the ones we want.

#include "EmSession.h"			// uint8 (Byte), EmButtonEvent, EmKeyEvent, EmPenEvent

template class EmThreadSafeQueue<uint8>;
template class EmThreadSafeQueue<EmButtonEvent>;
template class EmThreadSafeQueue<EmKeyEvent>;
template class EmThreadSafeQueue<EmPenEvent>;