|
0
|
1 #ifndef CYGONCE_DEVS_COMMON_QUEUET_HXX |
|
|
2 #define CYGONCE_DEVS_COMMON_QUEUET_HXX |
|
|
3 |
|
|
4 //========================================================================== |
|
|
5 // |
|
|
6 // queuet.hxx |
|
|
7 // |
|
|
8 // Generic queue template |
|
|
9 // |
|
|
10 //========================================================================== |
|
|
11 //####COPYRIGHTBEGIN#### |
|
|
12 // |
|
|
13 // ------------------------------------------- |
|
|
14 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
15 // Version 1.0 (the "License"); you may not use this file except in |
|
|
16 // compliance with the License. You may obtain a copy of the License at |
|
|
17 // http://sourceware.cygnus.com/ecos |
|
|
18 // |
|
|
19 // Software distributed under the License is distributed on an "AS IS" |
|
|
20 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
21 // License for the specific language governing rights and limitations under |
|
|
22 // the License. |
|
|
23 // |
|
|
24 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
25 // September 30, 1998. |
|
|
26 // |
|
|
27 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
|
28 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. |
|
|
29 // ------------------------------------------- |
|
|
30 // |
|
|
31 //####COPYRIGHTEND#### |
|
|
32 //========================================================================== |
|
|
33 //#####DESCRIPTIONBEGIN#### |
|
|
34 // |
|
|
35 // Author(s): proven |
|
|
36 // Contributors: proven |
|
|
37 // Date: 1998-04-21 |
|
|
38 // Purpose: Create a template atomic queue |
|
|
39 // Description: This file contains the implementations of |
|
|
40 // Cyg_Queuet |
|
|
41 // |
|
|
42 // |
|
|
43 //####DESCRIPTIONEND#### |
|
|
44 // |
|
|
45 //========================================================================== |
|
|
46 |
|
|
47 #include <cyg/kernel/ktypes.h> // base kernel types |
|
|
48 #include <cyg/infra/cyg_ass.h> // assertion macros |
|
|
49 #include <cyg/infra/cyg_trac.h> // tracing macros |
|
|
50 |
|
|
51 |
|
|
52 /* |
|
|
53 * Queue that allows two threads to interact with a queue |
|
|
54 * without locking. One thread does queueing of useable buffers |
|
|
55 * and dequeing of done buffers and one thread is the consumer. |
|
|
56 * |
|
|
57 * Only n-1 elements may be queued at any time for a queue of n bytes. |
|
|
58 */ |
|
|
59 |
|
|
60 template <class T, cyg_count32 QUEUE_SIZE> |
|
|
61 class Cyg_Queuet |
|
|
62 { |
|
|
63 private: |
|
|
64 cyg_ucount32 next_free; |
|
|
65 cyg_ucount32 last_free; |
|
|
66 cyg_ucount32 next_inuse; |
|
|
67 cyg_ucount32 last_inuse; |
|
|
68 static const cyg_ucount32 queue_size = QUEUE_SIZE; |
|
|
69 T * queue[ QUEUE_SIZE ]; |
|
|
70 |
|
|
71 public: /* Routines for dealing with the queue */ |
|
|
72 inline T * |
|
|
73 get_next_inuse(T * done) |
|
|
74 { |
|
|
75 T * ret; |
|
|
76 |
|
|
77 CYG_ASSERT (next_inuse < queue_size, "next_inuse out of bounds"); |
|
|
78 CYG_ASSERT (last_inuse < queue_size, "last_inuse out of bounds"); |
|
|
79 |
|
|
80 if (done) { |
|
|
81 (++last_inuse) %= queue_size; |
|
|
82 } |
|
|
83 if ((ret = queue[next_inuse]) != NULL) { |
|
|
84 (++next_inuse) %= queue_size; |
|
|
85 } |
|
|
86 return ret; |
|
|
87 } |
|
|
88 |
|
|
89 inline void |
|
|
90 enqueue(T * next) |
|
|
91 { |
|
|
92 CYG_ASSERT (next_free < queue_size, "next_free out of bounds"); |
|
|
93 CYG_ASSERT (queue[next_free] == NULL, "next_free is not NULL"); |
|
|
94 CYG_ASSERT (min_free(), "not enough space to enqueue"); |
|
|
95 |
|
|
96 queue[next_free++] = next; |
|
|
97 next_free %= queue_size; |
|
|
98 } |
|
|
99 |
|
|
100 inline T * |
|
|
101 Cyg_Queuet::dequeue() |
|
|
102 { |
|
|
103 T * ret; |
|
|
104 |
|
|
105 CYG_ASSERT (last_free < queue_size, "last_free out of bounds"); |
|
|
106 CYG_ASSERT (last_free != last_inuse, "no elements to dequeue"); |
|
|
107 CYG_ASSERT (queue[last_free] != NULL, "queued element is NULL"); |
|
|
108 |
|
|
109 ret = queue[last_free]; |
|
|
110 queue[last_free++] = NULL; |
|
|
111 last_free %= queue_size; |
|
|
112 return ret; |
|
|
113 } |
|
|
114 |
|
|
115 /* Assume producer is checking */ |
|
|
116 inline cyg_ucount32 |
|
|
117 Cyg_Queuet::min_done() |
|
|
118 { |
|
|
119 cyg_count32 tmp; |
|
|
120 cyg_ucount32 ret; |
|
|
121 |
|
|
122 if (queue[last_free] != NULL) { |
|
|
123 tmp = last_inuse - last_free; |
|
|
124 ret = (tmp < 0 ? tmp + queue_size : tmp); |
|
|
125 } else { |
|
|
126 CYG_ASSERT (last_free == next_free, |
|
|
127 "last_free element is bogus"); |
|
|
128 ret = 0; |
|
|
129 } |
|
|
130 return ret; |
|
|
131 } |
|
|
132 |
|
|
133 /* Assume producer is checking */ |
|
|
134 cyg_ucount32 |
|
|
135 Cyg_Queuet::min_free() |
|
|
136 { |
|
|
137 cyg_count32 tmp; |
|
|
138 cyg_ucount32 ret; |
|
|
139 |
|
|
140 CYG_ASSERT (next_free < queue_size, "next_free out of bounds"); |
|
|
141 CYG_ASSERT (last_free < queue_size, "last_free out of bounds"); |
|
|
142 |
|
|
143 tmp = last_free - next_free; |
|
|
144 ret = (tmp <= 0 ? tmp + queue_size : tmp); |
|
|
145 /* Only n - 1 elements may be queued */ |
|
|
146 return (ret - 1); |
|
|
147 } |
|
|
148 }; |
|
|
149 |
|
|
150 // ------------------------------------------------------------------------- |
|
|
151 #endif // CYGONCE_DEVS_COMMON_QUEUET_HXX |
|
|
152 // End of queuet.hxx |
|
|
153 |
|
|
154 |