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