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