include/ansidecl.h: import from binutils-gdb
[newlib-cygwin.git] / winsup / cygserver / threaded_queue.cc
blob72f2a5fdd97cf53208839e72cf563eb867d3a95f
1 /* threaded_queue.cc
3 Written by Robert Collins <rbtcollins@hotmail.com>
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
9 details. */
11 #ifdef __OUTSIDE_CYGWIN__
12 #include "woutsup.h"
14 #include <assert.h>
15 #include <errno.h>
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <stdlib.h>
20 #include "threaded_queue.h"
22 /*****************************************************************************/
24 /* queue_request */
26 queue_request::~queue_request ()
29 /*****************************************************************************/
31 /* threaded_queue */
33 threaded_queue::threaded_queue (const size_t initial_workers)
34 : _workers_count (0),
35 _workers_busy (0),
36 _running (false),
37 _submitters_head (NULL),
38 _requests_count (0),
39 _requests_head (NULL),
40 _requests_sem (NULL)
42 InitializeCriticalSection (&_queue_lock);
44 // This semaphore's count is the number of requests on the queue.
45 // The maximum count (129792) is calculated as MAXIMUM_WAIT_OBJECTS
46 // multiplied by max. threads per process (2028?), which is (a few)
47 // more requests than could ever be pending with the current design.
49 _requests_sem = CreateSemaphore (NULL, // SECURITY_ATTRIBUTES
50 0, // Initial count
51 129792, // Maximum count
52 NULL); // Anonymous
54 if (!_requests_sem)
56 system_printf (("failed to create the request queue semaphore, "
57 "error = %u"),
58 GetLastError ());
59 abort ();
62 create_workers (initial_workers);
65 threaded_queue::~threaded_queue ()
67 if (_running)
68 stop ();
70 debug_printf ("deleting all pending queue requests");
71 queue_request *reqptr = _requests_head;
72 while (reqptr)
74 queue_request *const ptr = reqptr;
75 reqptr = reqptr->_next;
76 delete ptr;
79 DeleteCriticalSection (&_queue_lock);
80 if (_requests_sem)
81 (void) CloseHandle (_requests_sem);
84 /* FIXME: return success or failure rather than quitting */
85 void
86 threaded_queue::add_submission_loop (queue_submission_loop *const submitter)
88 assert (submitter);
89 assert (submitter->_queue == this);
90 assert (!submitter->_next);
92 submitter->_next =
93 TInterlockedExchangePointer (&_submitters_head, submitter);
95 if (_running)
96 submitter->start ();
99 bool
100 threaded_queue::start ()
102 EnterCriticalSection (&_queue_lock);
103 const bool was_running = _running;
104 _running = true;
105 queue_submission_loop *loopptr = _submitters_head;
106 LeaveCriticalSection (&_queue_lock);
108 if (!was_running)
110 debug_printf ("starting all queue submission loops");
112 while (loopptr)
114 queue_submission_loop *const ptr = loopptr;
115 loopptr = loopptr->_next;
116 ptr->start ();
120 return was_running;
123 bool
124 threaded_queue::stop ()
126 EnterCriticalSection (&_queue_lock);
127 const bool was_running = _running;
128 _running = false;
129 queue_submission_loop *loopptr = _submitters_head;
130 LeaveCriticalSection (&_queue_lock);
132 if (was_running)
134 debug_printf ("stopping all queue submission loops");
135 while (loopptr)
137 queue_submission_loop *const ptr = loopptr;
138 loopptr = loopptr->_next;
139 ptr->stop ();
142 ReleaseSemaphore (_requests_sem, _workers_count, NULL);
143 while (_workers_count)
145 debug_printf (("waiting for worker threads to terminate: "
146 "%u still running"),
147 _workers_count);
148 Sleep (1000);
150 debug_printf ("all worker threads have terminated");
153 return was_running;
156 /* FIXME: return success or failure */
157 void
158 threaded_queue::add (queue_request *const therequest)
160 assert (therequest);
161 assert (!therequest->_next);
163 EnterCriticalSection (&_queue_lock);
164 if (!_requests_head)
165 _requests_head = therequest;
166 else
168 /* Add to the queue end. */
169 queue_request *reqptr = _requests_head;
170 for (; reqptr->_next; reqptr = reqptr->_next)
172 assert (reqptr);
173 assert (!reqptr->_next);
174 reqptr->_next = therequest;
177 _requests_count += 1;
178 assert (_requests_count > 0);
179 LeaveCriticalSection (&_queue_lock);
181 (void) ReleaseSemaphore (_requests_sem, 1, NULL);
183 if (_workers_busy >= _workers_count)
185 create_workers (1);
186 system_printf ("All threads busy, added one (now %u)", _workers_count);
190 /*static*/ DWORD WINAPI
191 threaded_queue::start_routine (const LPVOID lpParam)
193 class threaded_queue *const queue = (class threaded_queue *) lpParam;
194 assert (queue);
196 queue->worker_loop ();
198 const long count = InterlockedDecrement (&queue->_workers_count);
199 assert (count >= 0);
201 if (queue->_running)
202 debug_printf ("worker loop has exited; thread about to terminate");
204 return 0;
207 void
208 threaded_queue::create_workers (const size_t initial_workers)
210 assert (initial_workers > 0);
212 for (unsigned int i = 0; i < initial_workers; i++)
214 const long count = InterlockedIncrement (&_workers_count);
215 assert (count > 0);
217 DWORD tid;
218 const HANDLE hThread =
219 CreateThread (NULL, 0, start_routine, this, 0, &tid);
221 if (!hThread)
223 system_printf ("failed to create thread, error = %u",
224 GetLastError ());
225 abort ();
228 (void) CloseHandle (hThread);
232 void
233 threaded_queue::worker_loop ()
235 while (true)
237 const DWORD rc = WaitForSingleObject (_requests_sem, INFINITE);
238 if (rc == WAIT_FAILED)
240 system_printf ("wait for request semaphore failed, error = %u",
241 GetLastError ());
242 return;
244 assert (rc == WAIT_OBJECT_0);
246 EnterCriticalSection (&_queue_lock);
247 if (!_running)
249 LeaveCriticalSection (&_queue_lock);
250 return;
253 assert (_requests_head);
254 queue_request *const reqptr = _requests_head;
255 _requests_head = reqptr->_next;
257 _requests_count -= 1;
258 assert (_requests_count >= 0);
259 LeaveCriticalSection (&_queue_lock);
261 assert (reqptr);
262 InterlockedIncrement (&_workers_busy);
263 reqptr->process ();
264 InterlockedDecrement (&_workers_busy);
265 delete reqptr;
269 /*****************************************************************************/
271 /* queue_submission_loop */
273 queue_submission_loop::queue_submission_loop (threaded_queue *const queue,
274 const bool ninterruptible)
275 : _running (false),
276 _interrupt_event (NULL),
277 _queue (queue),
278 _interruptible (ninterruptible),
279 _hThread (NULL),
280 _tid (0),
281 _next (NULL)
283 if (_interruptible)
285 // verbose: debug_printf ("creating an interruptible processing thread");
287 _interrupt_event = CreateEvent (NULL, // SECURITY_ATTRIBUTES
288 FALSE, // Auto-reset
289 FALSE, // Initially non-signalled
290 NULL); // Anonymous
292 if (!_interrupt_event)
294 system_printf ("failed to create interrupt event, error = %u",
295 GetLastError ());
296 abort ();
301 queue_submission_loop::~queue_submission_loop ()
303 if (_running)
304 stop ();
305 if (_interrupt_event)
306 (void) CloseHandle (_interrupt_event);
307 if (_hThread)
308 (void) CloseHandle (_hThread);
311 bool
312 queue_submission_loop::start ()
314 assert (!_hThread);
316 const bool was_running = _running;
318 if (!was_running)
320 _running = true;
322 _hThread = CreateThread (NULL, 0, start_routine, this, 0, &_tid);
323 if (!_hThread)
325 system_printf ("failed to create thread, error = %u",
326 GetLastError ());
327 abort ();
331 return was_running;
334 bool
335 queue_submission_loop::stop ()
337 assert (_hThread && _hThread != INVALID_HANDLE_VALUE);
339 const bool was_running = _running;
341 if (_running)
343 _running = false;
345 if (_interruptible)
347 assert (_interrupt_event
348 && _interrupt_event != INVALID_HANDLE_VALUE);
350 SetEvent (_interrupt_event);
352 if (WaitForSingleObject (_hThread, 1000) == WAIT_TIMEOUT)
354 system_printf (("request loop thread %u failed to shutdown "
355 "when asked politely: about to get heavy"),
356 _tid);
358 if (!TerminateThread (_hThread, 0))
360 system_printf (("failed to kill request loop thread %u"
361 ", error = %u"),
362 _tid, GetLastError ());
363 abort ();
367 else
369 // FIXME: could wait to see if the request loop notices that
370 // the submission loop is no longer running and shuts down
371 // voluntarily.
373 debug_printf ("killing request loop thread %u", _tid);
375 if (!TerminateThread (_hThread, 0))
376 system_printf (("failed to kill request loop thread %u"
377 ", error = %u"),
378 _tid, GetLastError ());
382 return was_running;
385 /*static*/ DWORD WINAPI
386 queue_submission_loop::start_routine (const LPVOID lpParam)
388 class queue_submission_loop *const submission_loop =
389 (class queue_submission_loop *) lpParam;
390 assert (submission_loop);
392 submission_loop->request_loop ();
394 debug_printf ("submission loop has exited; thread about to terminate");
396 submission_loop->stop ();
398 return 0;
401 /*****************************************************************************/
402 #endif /* __OUTSIDE_CYGWIN__ */