include/ansidecl.h: import from binutils-gdb
[newlib-cygwin.git] / winsup / cygserver / threaded_queue.h
blob44819d2fb6f8c1a4f0de3c534cd79ce6200cd31a
1 /* threaded_queue.h
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 #ifndef _THREADED_QUEUE_
12 #define _THREADED_QUEUE_
14 /*****************************************************************************/
16 /* a specific request */
18 class queue_request
20 public:
21 queue_request *_next;
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;
35 class threaded_queue
37 public:
38 threaded_queue (size_t initial_workers = 1);
39 ~threaded_queue ();
41 void add_submission_loop (queue_submission_loop *);
43 bool running () const { return _running; }
45 bool start ();
46 bool stop ();
48 void add (queue_request *);
50 private:
51 LONG _workers_count;
52 LONG _workers_busy;
53 bool _running;
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);
66 void worker_loop ();
69 /*****************************************************************************/
71 /* parameters for a request finding and submitting loop */
73 class queue_submission_loop
75 friend class threaded_queue;
77 public:
78 queue_submission_loop (threaded_queue *, bool ninterruptible);
79 virtual ~queue_submission_loop ();
81 bool start ();
82 bool stop ();
84 threaded_queue *queue () { return _queue; };
86 protected:
87 bool _running;
88 HANDLE _interrupt_event;
89 threaded_queue *const _queue;
91 private:
92 bool _interruptible;
93 HANDLE _hThread;
94 DWORD _tid;
95 queue_submission_loop *_next;
97 static DWORD WINAPI start_routine (LPVOID /* this */);
98 virtual void request_loop () = 0;
101 #ifdef __cplusplus
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_ */