1 | /* Copyright (c) 2001, Stanford University
|
---|
2 | * All rights reserved
|
---|
3 | *
|
---|
4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "server_dispatch.h"
|
---|
8 | #include "server.h"
|
---|
9 | #include "cr_error.h"
|
---|
10 | #include "cr_mem.h"
|
---|
11 | #include "state/cr_statetypes.h"
|
---|
12 |
|
---|
13 | #define DEBUG_BARRIERS 1
|
---|
14 |
|
---|
15 | void SERVER_DISPATCH_APIENTRY crServerDispatchBarrierCreateCR( GLuint name, GLuint count )
|
---|
16 | {
|
---|
17 | CRServerBarrier *barrier;
|
---|
18 | #if DEBUG_BARRIERS
|
---|
19 | char debug_buf[4096];
|
---|
20 | #endif
|
---|
21 |
|
---|
22 | if (cr_server.ignore_papi)
|
---|
23 | {
|
---|
24 | cr_server.head_spu->dispatch_table.BarrierCreateCR( name, count );
|
---|
25 | return;
|
---|
26 | }
|
---|
27 |
|
---|
28 | barrier = (CRServerBarrier *) crHashtableSearch( cr_server.barriers, name );
|
---|
29 |
|
---|
30 | #if DEBUG_BARRIERS
|
---|
31 | sprintf( debug_buf, "BarrierCreateCR( %d, %d )", name, count );
|
---|
32 | cr_server.head_spu->dispatch_table.ChromiumParametervCR( GL_PRINT_STRING_CR, GL_UNSIGNED_BYTE, sizeof(debug_buf), debug_buf );
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | if (count > CR_MAX_CLIENTS)
|
---|
36 | count = CR_MAX_CLIENTS;
|
---|
37 |
|
---|
38 | if (count == 0)
|
---|
39 | {
|
---|
40 | count = cr_server.numClients;
|
---|
41 | #if DEBUG_BARRIERS
|
---|
42 | sprintf( debug_buf, "changing count to %d", count );
|
---|
43 | cr_server.head_spu->dispatch_table.ChromiumParametervCR( GL_PRINT_STRING_CR, GL_UNSIGNED_BYTE, sizeof(debug_buf), debug_buf );
|
---|
44 | #endif
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | /* we use maxBarrierCount in Clear() and SwapBuffers() and also use it
|
---|
49 | * in __getNextClient() for deadlock detection. The issue is that all
|
---|
50 | * the existing clients may be blocked, but we might soon get another
|
---|
51 | * client connection to break the apparent deadlock.
|
---|
52 | */
|
---|
53 | if (count > cr_server.maxBarrierCount)
|
---|
54 | cr_server.maxBarrierCount = count;
|
---|
55 |
|
---|
56 | if ( barrier == NULL )
|
---|
57 | {
|
---|
58 | barrier = (CRServerBarrier *) crAlloc( sizeof(*barrier) );
|
---|
59 | barrier->count = count;
|
---|
60 | barrier->num_waiting = 0;
|
---|
61 | barrier->waiting = (RunQueue **)
|
---|
62 | crAlloc( count * sizeof(*(barrier->waiting)) );
|
---|
63 |
|
---|
64 | crHashtableAdd( cr_server.barriers, name, barrier );
|
---|
65 | #if DEBUG_BARRIERS
|
---|
66 | sprintf( debug_buf, "This was a new barrier!" );
|
---|
67 | cr_server.head_spu->dispatch_table.ChromiumParametervCR( GL_PRINT_STRING_CR, GL_UNSIGNED_BYTE, sizeof(debug_buf), debug_buf );
|
---|
68 | #endif
|
---|
69 | }
|
---|
70 | else
|
---|
71 | {
|
---|
72 | /* HACK -- this allows everybody to create a barrier, and all
|
---|
73 | but the first creation are ignored, assuming the count
|
---|
74 | match. */
|
---|
75 | #if DEBUG_BARRIERS
|
---|
76 | sprintf( debug_buf, "I already knew about this barrier." );
|
---|
77 | cr_server.head_spu->dispatch_table.ChromiumParametervCR( GL_PRINT_STRING_CR, GL_UNSIGNED_BYTE, sizeof(debug_buf), debug_buf );
|
---|
78 | #endif
|
---|
79 | if ( barrier->count != count )
|
---|
80 | {
|
---|
81 | #if DEBUG_BARRIERS
|
---|
82 | sprintf( debug_buf, "And someone messed up the count!." );
|
---|
83 | cr_server.head_spu->dispatch_table.ChromiumParametervCR( GL_PRINT_STRING_CR, GL_UNSIGNED_BYTE, sizeof(debug_buf), debug_buf );
|
---|
84 | #endif
|
---|
85 | crError( "Barrier name=%u created with count=%u, but already "
|
---|
86 | "exists with count=%u", name, count, barrier->count );
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (cr_server.debug_barriers)
|
---|
91 | crDebug("crserver: BarrierCreate(id=%d, count=%d)", name, barrier->count);
|
---|
92 | }
|
---|
93 |
|
---|
94 | void SERVER_DISPATCH_APIENTRY crServerDispatchBarrierDestroyCR( GLuint name )
|
---|
95 | {
|
---|
96 | if (cr_server.ignore_papi)
|
---|
97 | {
|
---|
98 | cr_server.head_spu->dispatch_table.BarrierDestroyCR( name );
|
---|
99 | return;
|
---|
100 | }
|
---|
101 |
|
---|
102 | crError( "NO BARRIER DESTROY FOR YOU! (name=%u)", name );
|
---|
103 | }
|
---|
104 |
|
---|
105 | void SERVER_DISPATCH_APIENTRY crServerDispatchBarrierExecCR( GLuint name )
|
---|
106 | {
|
---|
107 | CRServerBarrier *barrier;
|
---|
108 | #if DEBUG_BARRIERS
|
---|
109 | char debug_buf[4096];
|
---|
110 | #endif
|
---|
111 |
|
---|
112 | if (cr_server.ignore_papi)
|
---|
113 | {
|
---|
114 | cr_server.head_spu->dispatch_table.BarrierExecCR( name );
|
---|
115 | return;
|
---|
116 | }
|
---|
117 |
|
---|
118 | barrier = (CRServerBarrier *) crHashtableSearch( cr_server.barriers, name );
|
---|
119 | if ( barrier == NULL )
|
---|
120 | {
|
---|
121 | crError( "crServerDispatchBarrierExec: No such barrier: %d", name );
|
---|
122 | }
|
---|
123 |
|
---|
124 | #if DEBUG_BARRIERS
|
---|
125 | sprintf( debug_buf, "BarrierExec( %d )", name );
|
---|
126 | cr_server.head_spu->dispatch_table.ChromiumParametervCR( GL_PRINT_STRING_CR, GL_UNSIGNED_BYTE, sizeof(debug_buf), debug_buf );
|
---|
127 | sprintf( debug_buf, "num_waiting = %d", barrier->num_waiting );
|
---|
128 | cr_server.head_spu->dispatch_table.ChromiumParametervCR( GL_PRINT_STRING_CR, GL_UNSIGNED_BYTE, sizeof(debug_buf), debug_buf );
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | barrier->waiting[barrier->num_waiting++] = cr_server.run_queue;
|
---|
132 |
|
---|
133 | cr_server.run_queue->blocked = 1;
|
---|
134 |
|
---|
135 | if ( barrier->num_waiting == barrier->count )
|
---|
136 | {
|
---|
137 | GLuint i;
|
---|
138 |
|
---|
139 | if (cr_server.debug_barriers)
|
---|
140 | crDebug("crserver: BarrierExec(client=%p, id=%d, num_waiting=%d/%d) - release",
|
---|
141 | cr_server.curClient, name, barrier->num_waiting,
|
---|
142 | barrier->count);
|
---|
143 |
|
---|
144 | for ( i = 0; i < barrier->count; i++ )
|
---|
145 | {
|
---|
146 | barrier->waiting[i]->blocked = 0;
|
---|
147 | }
|
---|
148 | barrier->num_waiting = 0;
|
---|
149 | }
|
---|
150 | else if (cr_server.debug_barriers)
|
---|
151 | crDebug("crserver: BarrierExec(client=%p, id=%d, num_waiting=%d/%d) - block",
|
---|
152 | cr_server.curClient, name, barrier->num_waiting,
|
---|
153 | barrier->count);
|
---|
154 |
|
---|
155 | }
|
---|
156 |
|
---|
157 | void SERVER_DISPATCH_APIENTRY crServerDispatchSemaphoreCreateCR( GLuint name, GLuint count )
|
---|
158 | {
|
---|
159 | CRServerSemaphore *sema;
|
---|
160 |
|
---|
161 | if (cr_server.ignore_papi)
|
---|
162 | {
|
---|
163 | cr_server.head_spu->dispatch_table.SemaphoreCreateCR( name, count );
|
---|
164 | return;
|
---|
165 | }
|
---|
166 |
|
---|
167 | sema = crHashtableSearch(cr_server.semaphores, name);
|
---|
168 | if (sema)
|
---|
169 | return; /* already created */
|
---|
170 |
|
---|
171 | sema = (CRServerSemaphore *) crAlloc( sizeof( *sema ) );
|
---|
172 | crHashtableAdd( cr_server.semaphores, name, sema );
|
---|
173 | sema->count = count;
|
---|
174 | sema->waiting = sema->tail = NULL;
|
---|
175 | if (cr_server.debug_barriers)
|
---|
176 | crDebug("crserver: SemaphoreCreate(id=%d, count=%d)", name, count);
|
---|
177 | }
|
---|
178 |
|
---|
179 | void SERVER_DISPATCH_APIENTRY crServerDispatchSemaphoreDestroyCR( GLuint name )
|
---|
180 | {
|
---|
181 | if (cr_server.ignore_papi)
|
---|
182 | {
|
---|
183 | cr_server.head_spu->dispatch_table.SemaphoreDestroyCR( name );
|
---|
184 | return;
|
---|
185 | }
|
---|
186 |
|
---|
187 | crError( "NO DESTROY FOR YOU! (name=%u)", name );
|
---|
188 | }
|
---|
189 |
|
---|
190 | /* Semaphore wait */
|
---|
191 | void SERVER_DISPATCH_APIENTRY crServerDispatchSemaphorePCR( GLuint name )
|
---|
192 | {
|
---|
193 | CRServerSemaphore *sema;
|
---|
194 |
|
---|
195 | if (cr_server.ignore_papi)
|
---|
196 | {
|
---|
197 | cr_server.head_spu->dispatch_table.SemaphorePCR( name );
|
---|
198 | return;
|
---|
199 | }
|
---|
200 |
|
---|
201 | sema = (CRServerSemaphore *) crHashtableSearch( cr_server.semaphores, name );
|
---|
202 | if (!sema)
|
---|
203 | {
|
---|
204 | crError( "No such semaphore: %d", name );
|
---|
205 | }
|
---|
206 | if (sema->count)
|
---|
207 | {
|
---|
208 | /* go */
|
---|
209 | if (cr_server.debug_barriers)
|
---|
210 | crDebug("crserver: SemaphoreP(client=%p, id=%d, count=%d) decrement to %d",
|
---|
211 | cr_server.curClient, name, sema->count, sema->count - 1);
|
---|
212 | sema->count--;
|
---|
213 | }
|
---|
214 | else
|
---|
215 | {
|
---|
216 | /* block */
|
---|
217 | wqnode *node;
|
---|
218 | if (cr_server.debug_barriers)
|
---|
219 | crDebug("crserver: SemaphoreP(client=%p, id=%d, count=%d) - block.",
|
---|
220 | cr_server.curClient, name, sema->count);
|
---|
221 | cr_server.run_queue->blocked = 1;
|
---|
222 | node = (wqnode *) crAlloc( sizeof( *node ) );
|
---|
223 | node->q = cr_server.run_queue;
|
---|
224 | node->next = NULL;
|
---|
225 | if (sema->tail)
|
---|
226 | {
|
---|
227 | sema->tail->next = node;
|
---|
228 | }
|
---|
229 | else
|
---|
230 | {
|
---|
231 | sema->waiting = node;
|
---|
232 | }
|
---|
233 | sema->tail = node;
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | /* Semaphore signal */
|
---|
238 | void SERVER_DISPATCH_APIENTRY crServerDispatchSemaphoreVCR( GLuint name )
|
---|
239 | {
|
---|
240 | CRServerSemaphore *sema;
|
---|
241 |
|
---|
242 | if (cr_server.ignore_papi)
|
---|
243 | {
|
---|
244 | cr_server.head_spu->dispatch_table.SemaphoreVCR( name );
|
---|
245 | return;
|
---|
246 | }
|
---|
247 |
|
---|
248 | sema = (CRServerSemaphore *) crHashtableSearch( cr_server.semaphores, name );
|
---|
249 | if (!sema)
|
---|
250 | {
|
---|
251 | crError( "No such semaphore: %d", name );
|
---|
252 | }
|
---|
253 | if (sema->waiting)
|
---|
254 | {
|
---|
255 | wqnode *temp = sema->waiting;
|
---|
256 | if (cr_server.debug_barriers)
|
---|
257 | crDebug("crserver: SemaphoreV(client=%p, id=%d, count=%d) - unblock.",
|
---|
258 | cr_server.curClient, name, sema->count);
|
---|
259 | /* unblock one waiter */
|
---|
260 | temp->q->blocked = 0;
|
---|
261 | sema->waiting = temp->next;
|
---|
262 | crFree( temp );
|
---|
263 | if (!sema->waiting)
|
---|
264 | {
|
---|
265 | sema->tail = NULL;
|
---|
266 | }
|
---|
267 | }
|
---|
268 | else
|
---|
269 | {
|
---|
270 | /* nobody's waiting */
|
---|
271 | if (cr_server.debug_barriers)
|
---|
272 | crDebug("crserver: SemaphoreV(client=%p, id=%d, count=%d) - increment to %d",
|
---|
273 | cr_server.curClient, name, sema->count, sema->count + 1);
|
---|
274 | sema->count++;
|
---|
275 | }
|
---|
276 | }
|
---|