3 Copyright 2001, 2002, 2003, 2012 Red Hat Inc.
5 Written by Robert Collins <rbtcollins@hotmail.com>
7 This file is part of Cygwin.
9 This software is a copyrighted work licensed under the terms of the
10 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
13 #ifndef _THREADED_QUEUE_
14 #define _THREADED_QUEUE_
16 /*****************************************************************************/
18 /* a specific request */
25 queue_request () : _next (NULL
) {}
26 virtual ~queue_request ();
28 virtual void process () = 0;
31 /*****************************************************************************/
33 /* a queue to allocate requests from n submission loops to x worker threads */
35 class queue_submission_loop
;
40 threaded_queue (size_t initial_workers
= 1);
43 void add_submission_loop (queue_submission_loop
*);
45 bool running () const { return _running
; }
50 void add (queue_request
*);
56 queue_submission_loop
*_submitters_head
;
58 long _requests_count
; // Informational only.
59 queue_request
*_requests_head
;
61 CRITICAL_SECTION _queue_lock
;
62 HANDLE _requests_sem
; // == _requests_count
64 static DWORD WINAPI
start_routine (LPVOID
/* this */);
66 void create_workers (size_t initial_workers
);
70 /*****************************************************************************/
72 /* parameters for a request finding and submitting loop */
74 class queue_submission_loop
76 friend class threaded_queue
;
79 queue_submission_loop (threaded_queue
*, bool ninterruptible
);
80 virtual ~queue_submission_loop ();
85 threaded_queue
*queue () { return _queue
; };
89 HANDLE _interrupt_event
;
90 threaded_queue
*const _queue
;
96 queue_submission_loop
*_next
;
98 static DWORD WINAPI
start_routine (LPVOID
/* this */);
99 virtual void request_loop () = 0;
104 /*---------------------------------------------------------------------------*
105 * Some type-safe versions of the various interlocked functions.
106 *---------------------------------------------------------------------------*/
108 template <typename T
> T
*
109 TInterlockedExchangePointer (T
**lvalue
, T
*rvalue
)
111 return reinterpret_cast<T
*>
112 (InterlockedExchangePointer (reinterpret_cast<void **> (lvalue
),
113 reinterpret_cast<void *> (rvalue
)));
116 template <typename T
> T
*
117 TInterlockedCompareExchangePointer (T
**lvalue
, T
*rvalue1
, T
*rvalue2
)
119 return reinterpret_cast<T
*>
120 (InterlockedCompareExchangePointer (reinterpret_cast<void **> (lvalue
),
121 reinterpret_cast<void *> (rvalue1
),
122 reinterpret_cast<void *> (rvalue2
)));
125 #endif /* __cplusplus */
127 #endif /* _THREADED_QUEUE_ */