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
11 #ifndef _THREADED_QUEUE_
12 #define _THREADED_QUEUE_
14 /*****************************************************************************/
16 /* a specific request */
23 queue_request () : _next (NULL
) {}
24 virtual ~queue_request ();
26 virtual void process () = 0;
29 /*****************************************************************************/
31 /* a queue to allocate requests from n submission loops to x worker threads */
33 class queue_submission_loop
;
38 threaded_queue (size_t initial_workers
= 1);
41 void add_submission_loop (queue_submission_loop
*);
43 bool running () const { return _running
; }
48 void add (queue_request
*);
55 queue_submission_loop
*_submitters_head
;
57 long _requests_count
; // Informational only.
58 queue_request
*_requests_head
;
60 CRITICAL_SECTION _queue_lock
;
61 HANDLE _requests_sem
; // == _requests_count
63 static DWORD WINAPI
start_routine (LPVOID
/* this */);
65 void create_workers (size_t initial_workers
);
69 /*****************************************************************************/
71 /* parameters for a request finding and submitting loop */
73 class queue_submission_loop
75 friend class threaded_queue
;
78 queue_submission_loop (threaded_queue
*, bool ninterruptible
);
79 virtual ~queue_submission_loop ();
84 threaded_queue
*queue () { return _queue
; };
88 HANDLE _interrupt_event
;
89 threaded_queue
*const _queue
;
95 queue_submission_loop
*_next
;
97 static DWORD WINAPI
start_routine (LPVOID
/* this */);
98 virtual void request_loop () = 0;
103 /*---------------------------------------------------------------------------*
104 * Some type-safe versions of the various interlocked functions.
105 *---------------------------------------------------------------------------*/
107 template <typename T
> T
*
108 TInterlockedExchangePointer (T
**lvalue
, T
*rvalue
)
110 return reinterpret_cast<T
*>
111 (InterlockedExchangePointer (reinterpret_cast<void **> (lvalue
),
112 reinterpret_cast<void *> (rvalue
)));
115 template <typename T
> T
*
116 TInterlockedCompareExchangePointer (T
**lvalue
, T
*rvalue1
, T
*rvalue2
)
118 return reinterpret_cast<T
*>
119 (InterlockedCompareExchangePointer (reinterpret_cast<void **> (lvalue
),
120 reinterpret_cast<void *> (rvalue1
),
121 reinterpret_cast<void *> (rvalue2
)));
124 #endif /* __cplusplus */
126 #endif /* _THREADED_QUEUE_ */