forked from NVIDIA/stdexec
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwindows_thread_pool.hpp
More file actions
1034 lines (867 loc) · 31.5 KB
/
windows_thread_pool.hpp
File metadata and controls
1034 lines (867 loc) · 31.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) 2025 NVIDIA Corporation
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#if !__has_include(<windows.h>)
# error "windows.h not found."
#else
// windows.h must be included before threadpoolapiset.h
// clang-format off
#include <windows.h>
#include <threadpoolapiset.h>
// clang-format on
# include "../../stdexec/__detail/__atomic.hpp"
# include "../../stdexec/__detail/__manual_lifetime.hpp"
# include "../../stdexec/__detail/__operation_states.hpp"
# include "../../stdexec/__detail/__receivers.hpp"
# include "../../stdexec/__detail/__schedulers.hpp"
# include "../../stdexec/__detail/__stop_token.hpp"
# include "../timed_scheduler.hpp" // IWYU pragma: keep
# include "./filetime_clock.hpp"
# include <system_error>
# include <utility>
namespace experimental::execution::__win32
{
class windows_thread_pool
{
struct attrs;
class scheduler;
class schedule_sender;
class schedule_op_base;
template <class Rcvr>
struct _schedule_op
{
class type;
};
template <class Rcvr>
using schedule_op = _schedule_op<Rcvr>::type;
template <class StopToken>
struct _cancellable_schedule_op_base
{
class type;
using __t = type;
};
template <class StopToken>
using cancellable_schedule_op_base = _cancellable_schedule_op_base<StopToken>::type;
template <class Rcvr>
struct _cancellable_schedule_op
{
class type;
using __t = type;
};
template <class Rcvr>
using cancellable_schedule_op = _cancellable_schedule_op<Rcvr>::type;
template <class StopToken>
struct _time_schedule_op
{
class type;
using __t = type;
};
template <class StopToken>
using time_schedule_op = _time_schedule_op<StopToken>::type;
template <class Rcvr>
struct _schedule_at_op
{
class type;
using __t = type;
};
template <class Rcvr>
using schedule_at_op = _schedule_at_op<Rcvr>::type;
template <class Duration, class Rcvr>
struct _schedule_after_op
{
class type;
using __t = type;
};
template <class Duration, class Rcvr>
using schedule_after_op = _schedule_after_op<Duration, Rcvr>::type;
class schedule_at_sender;
template <class Duration>
struct _schedule_after
{
class sender;
using __t = sender;
};
template <class Duration>
using schedule_after_sender = _schedule_after<Duration>::sender;
using clock_type = filetime_clock;
public:
// Initialise to use the process' default thread-pool.
windows_thread_pool() noexcept;
// Construct to an independend thread-pool with a dynamic number of
// threads that varies between a min and a max number of threads.
explicit windows_thread_pool(std::uint32_t minThreadCount, std::uint32_t maxThreadCount);
~windows_thread_pool();
auto get_scheduler() noexcept -> scheduler;
private:
PTP_POOL threadPool_;
};
/////////////////////////
// Non-cancellable schedule() operation
class windows_thread_pool::schedule_op_base
{
public:
schedule_op_base(schedule_op_base &&) = delete;
auto operator=(schedule_op_base &&) -> schedule_op_base & = delete;
~schedule_op_base();
void start() noexcept;
protected:
schedule_op_base(windows_thread_pool &pool, PTP_WORK_CALLBACK workCallback);
private:
TP_CALLBACK_ENVIRON environ_;
PTP_WORK work_;
};
template <class Rcvr>
class windows_thread_pool::_schedule_op<Rcvr>::type final
: public windows_thread_pool::schedule_op_base
{
public:
explicit type(windows_thread_pool &pool, Rcvr rcvr)
: schedule_op_base(pool, &work_callback)
, rcvr_(std::move(rcvr))
{}
private:
static void CALLBACK work_callback(PTP_CALLBACK_INSTANCE, void *workContext, PTP_WORK) noexcept
{
auto &op = *static_cast<type *>(workContext);
STDEXEC::set_value(std::move(op.rcvr_));
}
Rcvr rcvr_;
};
///////////////////////////
// Cancellable schedule() operation
template <class StopToken>
class windows_thread_pool::_cancellable_schedule_op_base<StopToken>::type
{
public:
using operation_state_concept = STDEXEC::operation_state_tag;
type(type &&) = delete;
auto operator=(type &&) -> type & = delete;
~type()
{
::CloseThreadpoolWork(work_);
::DestroyThreadpoolEnvironment(&environ_);
delete state_;
}
protected:
explicit type(windows_thread_pool &pool, bool isStopPossible)
{
::InitializeThreadpoolEnvironment(&environ_);
::SetThreadpoolCallbackPool(&environ_, pool.threadPool_);
work_ = ::CreateThreadpoolWork(isStopPossible ? &stoppable_work_callback
: &unstoppable_work_callback,
static_cast<void *>(this),
&environ_);
if (work_ == nullptr)
{
DWORD errorCode = ::GetLastError();
::DestroyThreadpoolEnvironment(&environ_);
throw std::system_error{static_cast<int>(errorCode),
std::system_category(),
"CreateThreadpoolWork()"};
}
if (isStopPossible)
{
state_ = new (std::nothrow) STDEXEC::__std::atomic<std::uint32_t>(not_started);
if (state_ == nullptr)
{
::CloseThreadpoolWork(work_);
::DestroyThreadpoolEnvironment(&environ_);
throw std::bad_alloc{};
}
}
else
{
state_ = nullptr;
}
}
void start_impl(StopToken const &stopToken) & noexcept
{
if (state_ != nullptr)
{
// Short-circuit all of this if stopToken.stop_requested() is already
// true.
//
// TODO: this means done can be delivered on "the wrong thread"
if (stopToken.stop_requested())
{
set_stopped_impl();
return;
}
stopCallback_.__construct(stopToken, stop_requested_callback{*this});
// Take a copy of the 'state' pointer prior to submitting the
// work as the operation-state may have already been destroyed
// on another thread by the time SubmitThreadpoolWork() returns.
auto *state = state_;
::SubmitThreadpoolWork(work_);
// Signal that SubmitThreadpoolWork() has returned and that it is
// now safe for the stop-request to request cancellation of the
// work items.
auto const prevState = state->fetch_add(submit_complete_flag,
STDEXEC::__std::memory_order_acq_rel);
if ((prevState & stop_requested_flag) != 0)
{
// stop was requested before the call to SubmitThreadpoolWork()
// returned and before the work started executing. It was not
// safe for the request_stop() method to cancel the work before
// it had finished being submitted so it has delegated responsibility
// for cancelling the just-submitted work to us to do once we
// finished submitting the work.
complete_with_done();
}
else if ((prevState & running_flag) != 0)
{
// Otherwise, it's possible that the work item may have started
// running on another thread already, prior to us returning.
// If this is the case then, to avoid leaving us with a
// dangling reference to the 'state' when the operation-state
// is destroyed, it will detach the 'state' from the operation-state
// and delegate the delete of the 'state' to us.
delete state;
}
}
else
{
// A stop-request is not possible so skip the extra
// synchronisation needed to support it.
::SubmitThreadpoolWork(work_);
}
}
private:
static void CALLBACK unstoppable_work_callback(PTP_CALLBACK_INSTANCE,
void *workContext,
PTP_WORK) noexcept
{
auto &op = *static_cast<type *>(workContext);
op.set_value_impl();
}
static void CALLBACK stoppable_work_callback(PTP_CALLBACK_INSTANCE,
void *workContext,
PTP_WORK) noexcept
{
auto &op = *static_cast<type *>(workContext);
// Signal that the work callback has started executing.
auto prevState = op.state_->fetch_add(starting_flag, STDEXEC::__std::memory_order_acq_rel);
if ((prevState & stop_requested_flag) != 0)
{
// request_stop() is already running and is waiting for this callback
// to finish executing. So we return immediately here without doing
// anything further so that we don't introduce a deadlock.
// In particular, we don't want to try to deregister the stop-callback
// which will block waiting for the request_stop() method to return.
return;
}
// Note that it's possible that stop might be requested after setting
// the 'starting' flag but before we deregister the stop callback.
// We're going to ignore these stop-requests as we already won the race
// in the fetch_add() above and ignoring them simplifies some of the
// cancellation logic.
op.stopCallback_.__destroy();
prevState = op.state_->fetch_add(running_flag, STDEXEC::__std::memory_order_acq_rel);
if (prevState == starting_flag)
{
// start() method has not yet finished submitting the work
// on another thread and so is still accessing the 'state'.
// This means we don't want to let the operation-state destructor
// free the state memory. Instead, we have just delegated
// responsibility for freeing this memory to the start() method
// and we clear the start_ member here to prevent the destructor
// from freeing it.
op.state_ = nullptr;
}
op.set_value_impl();
}
void request_stop() noexcept
{
auto prevState = state_->load(STDEXEC::__std::memory_order_relaxed);
do
{
STDEXEC_ASSERT((prevState & running_flag) == 0);
if ((prevState & starting_flag) != 0)
{
// Work callback won the race and will be waiting for
// us to return so it can deregister the stop-callback.
// Return immediately so we don't deadlock.
return;
}
}
while (!state_->compare_exchange_weak(prevState,
prevState | stop_requested_flag,
STDEXEC::__std::memory_order_acq_rel,
STDEXEC::__std::memory_order_relaxed));
STDEXEC_ASSERT((prevState & starting_flag) == 0);
if ((prevState & submit_complete_flag) != 0)
{
// start() has finished calling SubmitThreadpoolWork() and the work has
// not yet started executing the work so it's safe for this method to now
// try and cancel the work. While it's possible that the work callback
// will start executing concurrently on a thread-pool thread, we are
// guaranteed that it will see our write of the stop_requested_flag and
// will promptly return without blocking.
complete_with_done();
}
else
{
// Otherwise, as the start() method has not yet finished calling
// SubmitThreadpoolWork() we can't safely call
// WaitForThreadpoolWorkCallbacks(). In this case we are delegating
// responsibility for calling complete_with_done() to start() method when
// it eventually returns from SubmitThreadpoolWork().
}
}
void complete_with_done() noexcept
{
const BOOL cancelPending = TRUE;
::WaitForThreadpoolWorkCallbacks(work_, cancelPending);
// Destruct the stop-callback before calling set_stopped() as the call
// to set_stopped() will invalidate the stop-token and we need to
// make sure that
stopCallback_.__destroy();
// Now that the work has been successfully cancelled we can
// call the receiver's set_stopped().
set_stopped_impl();
}
virtual void set_stopped_impl() noexcept = 0;
virtual void set_value_impl() noexcept = 0;
struct stop_requested_callback
{
type &op_;
void operator()() noexcept
{
op_.request_stop();
}
};
/////////////////
// Flags to use for state_ member
// Initial state. start() not yet called.
static constexpr std::uint32_t not_started = 0;
// Flag set once start() has finished calling ThreadpoolSubmitWork()
static constexpr std::uint32_t submit_complete_flag = 1;
// Flag set by request_stop()
static constexpr std::uint32_t stop_requested_flag = 2;
// Flag set by cancellable_work_callback() when it starts executing.
// This is before deregistering the stop-callback.
static constexpr std::uint32_t starting_flag = 4;
// Flag set by cancellable_work_callback() after having deregistered
// the stop-callback, just before it calls the receiver.
static constexpr std::uint32_t running_flag = 8;
PTP_WORK work_;
TP_CALLBACK_ENVIRON environ_;
STDEXEC::__std::atomic<std::uint32_t> *state_;
STDEXEC::__manual_lifetime<STDEXEC::stop_callback_for_t<StopToken, stop_requested_callback>>
stopCallback_;
};
template <class Rcvr>
class windows_thread_pool::_cancellable_schedule_op<Rcvr>::type final
: public windows_thread_pool::cancellable_schedule_op_base<
STDEXEC::stop_token_of_t<STDEXEC::env_of_t<Rcvr>>>
{
using base = windows_thread_pool::cancellable_schedule_op_base<
STDEXEC::stop_token_of_t<STDEXEC::env_of_t<Rcvr>>>;
public:
explicit type(windows_thread_pool &pool, Rcvr rcvr)
: base(pool, STDEXEC::get_stop_token(rcvr).stop_possible())
, rcvr_(std::move(rcvr))
{}
void start() noexcept
{
this->start_impl(STDEXEC::get_stop_token(STDEXEC::get_env(rcvr_)));
}
private:
void set_value_impl() noexcept override
{
STDEXEC::set_value(std::move(rcvr_));
}
void set_stopped_impl() noexcept override
{
if constexpr (!STDEXEC::unstoppable_token<STDEXEC::stop_token_of_t<STDEXEC::env_of_t<Rcvr>>>)
{
STDEXEC::set_stopped(std::move(rcvr_));
}
else
{
STDEXEC_ASSERT(false);
}
}
Rcvr rcvr_;
};
////////////////////////////////////////////////////
// schedule senders' attributes
struct windows_thread_pool::attrs
{
[[nodiscard]]
auto
query(STDEXEC::get_completion_scheduler_t<STDEXEC::set_value_t>) const noexcept -> scheduler;
windows_thread_pool *pool_;
};
////////////////////////////////////////////////////
// schedule() sender
class windows_thread_pool::schedule_sender
{
public:
using sender_concept = STDEXEC::sender_tag;
using completion_signatures =
STDEXEC::completion_signatures<STDEXEC::set_value_t(),
STDEXEC::set_error_t(std::exception_ptr),
STDEXEC::set_stopped_t()>;
template <class Rcvr> //
requires STDEXEC::receiver_of<Rcvr, completion_signatures>
&& STDEXEC::unstoppable_token<STDEXEC::stop_token_of_t<STDEXEC::env_of_t<Rcvr>>>
auto connect(Rcvr rcvr) const -> schedule_op<Rcvr>
{
return schedule_op<Rcvr>{*pool_, static_cast<Rcvr &&>(rcvr)};
}
template <class Rcvr> //
requires STDEXEC::receiver_of<Rcvr, completion_signatures>
&& (!STDEXEC::unstoppable_token<STDEXEC::stop_token_of_t<STDEXEC::env_of_t<Rcvr>>>)
auto connect(Rcvr rcvr) const -> cancellable_schedule_op<Rcvr>
{
return cancellable_schedule_op<Rcvr>{*pool_, static_cast<Rcvr &&>(rcvr)};
}
[[nodiscard]]
auto get_env() const noexcept -> attrs
{
return attrs{pool_};
}
private:
friend scheduler;
explicit schedule_sender(windows_thread_pool &pool) noexcept
: pool_(&pool)
{}
windows_thread_pool *pool_;
};
/////////////////////////////////
// time_schedule_op
template <class StopToken>
class windows_thread_pool::_time_schedule_op<StopToken>::type
{
protected:
explicit type(windows_thread_pool &pool, bool isStopPossible)
{
::InitializeThreadpoolEnvironment(&environ_);
::SetThreadpoolCallbackPool(&environ_, pool.threadPool_);
// Give the optimiser a hand for cases where the parameter
// can never be true.
if constexpr (STDEXEC::unstoppable_token<StopToken>)
{
isStopPossible = false;
}
timer_ = ::CreateThreadpoolTimer(isStopPossible ? &stoppable_timer_callback : &timer_callback,
static_cast<void *>(this),
&environ_);
if (timer_ == nullptr)
{
DWORD errorCode = ::GetLastError();
::DestroyThreadpoolEnvironment(&environ_);
throw std::system_error{static_cast<int>(errorCode),
std::system_category(),
"CreateThreadpoolTimer()"};
}
if (isStopPossible)
{
state_ = new (std::nothrow) STDEXEC::__std::atomic<std::uint32_t>{not_started};
if (state_ == nullptr)
{
::CloseThreadpoolTimer(timer_);
::DestroyThreadpoolEnvironment(&environ_);
throw std::bad_alloc{};
}
}
}
public:
using operation_state_concept = STDEXEC::operation_state_tag;
~type()
{
::CloseThreadpoolTimer(timer_);
::DestroyThreadpoolEnvironment(&environ_);
delete state_;
}
protected:
void start_impl(StopToken const &stopToken, FILETIME dueTime) noexcept
{
auto startTimer = [&]() noexcept
{
const DWORD periodInMs = 0; // Single-shot
const DWORD maxDelayInMs = 0; // Max delay to allow timer coalescing
::SetThreadpoolTimer(timer_, &dueTime, periodInMs, maxDelayInMs);
};
if constexpr (!STDEXEC::unstoppable_token<StopToken>)
{
auto *const state = state_;
if (state != nullptr)
{
// Short-circuit extra work submitting the
// timer if stop has already been requested.
//
// TODO: this means done can be delivered on "the wrong thread"
if (stopToken.stop_requested())
{
set_stopped_impl();
return;
}
stopCallback_.__construct(stopToken, stop_requested_callback{*this});
startTimer();
auto const prevState = state->fetch_add(submit_complete_flag,
STDEXEC::__std::memory_order_acq_rel);
if ((prevState & stop_requested_flag) != 0)
{
complete_with_done();
}
else if ((prevState & running_flag) != 0)
{
delete state;
}
return;
}
}
startTimer();
}
private:
virtual void set_value_impl() noexcept = 0;
virtual void set_stopped_impl() noexcept = 0;
static void CALLBACK timer_callback([[maybe_unused]] PTP_CALLBACK_INSTANCE instance,
void *timerContext,
[[maybe_unused]] PTP_TIMER timer) noexcept
{
type &op = *static_cast<type *>(timerContext);
op.set_value_impl();
}
static void CALLBACK stoppable_timer_callback([[maybe_unused]] PTP_CALLBACK_INSTANCE instance,
void *timerContext,
[[maybe_unused]] PTP_TIMER timer) noexcept
{
type &op = *static_cast<type *>(timerContext);
auto prevState = op.state_->fetch_add(starting_flag, STDEXEC::__std::memory_order_acq_rel);
if ((prevState & stop_requested_flag) != 0)
{
return;
}
op.stopCallback_.__destroy();
prevState = op.state_->fetch_add(running_flag, STDEXEC::__std::memory_order_acq_rel);
if (prevState == starting_flag)
{
op.state_ = nullptr;
}
op.set_value_impl();
}
void request_stop() noexcept
{
auto prevState = state_->load(STDEXEC::__std::memory_order_relaxed);
do
{
STDEXEC_ASSERT((prevState & running_flag) == 0);
if ((prevState & starting_flag) != 0)
{
return;
}
}
while (!state_->compare_exchange_weak(prevState,
prevState | stop_requested_flag,
STDEXEC::__std::memory_order_acq_rel,
STDEXEC::__std::memory_order_relaxed));
STDEXEC_ASSERT((prevState & starting_flag) == 0);
if ((prevState & submit_complete_flag) != 0)
{
complete_with_done();
}
}
void complete_with_done() noexcept
{
const BOOL cancelPending = TRUE;
::WaitForThreadpoolTimerCallbacks(timer_, cancelPending);
stopCallback_.__destroy();
set_stopped_impl();
}
struct stop_requested_callback
{
type &op_;
void operator()() noexcept
{
op_.request_stop();
}
};
/////////////////
// Flags to use for state_ member
// Initial state. start() not yet called.
static constexpr std::uint32_t not_started = 0;
// Flag set once start() has finished calling ThreadpoolSubmitWork()
static constexpr std::uint32_t submit_complete_flag = 1;
// Flag set by request_stop()
static constexpr std::uint32_t stop_requested_flag = 2;
// Flag set by cancellable_work_callback() when it starts executing.
// This is before deregistering the stop-callback.
static constexpr std::uint32_t starting_flag = 4;
// Flag set by cancellable_work_callback() after having deregistered
// the stop-callback, just before it calls the receiver.
static constexpr std::uint32_t running_flag = 8;
PTP_TIMER timer_;
TP_CALLBACK_ENVIRON environ_;
STDEXEC::__std::atomic<std::uint32_t> *state_{nullptr};
STDEXEC::__manual_lifetime<typename StopToken::template callback_type<stop_requested_callback>>
stopCallback_;
};
/////////////////////////////////
// schedule_at() operation
template <class Rcvr>
class windows_thread_pool::_schedule_at_op<Rcvr>::type final
: public windows_thread_pool::time_schedule_op<
STDEXEC::stop_token_of_t<STDEXEC::env_of_t<Rcvr>>>
{
using base =
windows_thread_pool::time_schedule_op<STDEXEC::stop_token_of_t<STDEXEC::env_of_t<Rcvr>>>;
public:
explicit type(windows_thread_pool &pool,
windows_thread_pool::clock_type::time_point dueTime,
Rcvr rcvr)
: base(pool, STDEXEC::get_stop_token(rcvr).stop_possible())
, dueTime_(dueTime)
, rcvr_(std::move(rcvr))
{}
void start() noexcept
{
ULARGE_INTEGER ticks;
ticks.QuadPart = dueTime_.get_ticks();
FILETIME ft;
ft.dwLowDateTime = ticks.LowPart;
ft.dwHighDateTime = ticks.HighPart;
this->start_impl(STDEXEC::get_stop_token(STDEXEC::get_env(rcvr_)), ft);
}
private:
void set_value_impl() noexcept override
{
STDEXEC::set_value(std::move(rcvr_));
}
void set_stopped_impl() noexcept override
{
STDEXEC::set_stopped(std::move(rcvr_));
}
windows_thread_pool::clock_type::time_point dueTime_;
Rcvr rcvr_;
};
class windows_thread_pool::schedule_at_sender
{
public:
using sender_concept = STDEXEC::sender_tag;
using completion_signatures =
STDEXEC::completion_signatures<STDEXEC::set_value_t(),
STDEXEC::set_error_t(std::exception_ptr),
STDEXEC::set_stopped_t()>;
explicit schedule_at_sender(windows_thread_pool &pool, filetime_clock::time_point dueTime)
: pool_(&pool)
, dueTime_(dueTime)
{}
template <class Rcvr>
requires STDEXEC::receiver_of<Rcvr, completion_signatures>
auto connect(Rcvr rcvr) const -> schedule_at_op<Rcvr>
{
return schedule_at_op<Rcvr>{*pool_, dueTime_, static_cast<Rcvr &&>(rcvr)};
}
[[nodiscard]]
auto get_env() const noexcept -> attrs
{
return attrs{pool_};
}
private:
windows_thread_pool *pool_;
filetime_clock::time_point dueTime_;
};
//////////////////////////////////
// schedule_after()
template <class Duration, class Rcvr>
class windows_thread_pool::_schedule_after_op<Duration, Rcvr>::type final
: public windows_thread_pool::time_schedule_op<
STDEXEC::stop_token_of_t<STDEXEC::env_of_t<Rcvr>>>
{
using base =
windows_thread_pool::time_schedule_op<STDEXEC::stop_token_of_t<STDEXEC::env_of_t<Rcvr>>>;
public:
explicit type(windows_thread_pool &pool, Duration duration, Rcvr rcvr)
: base(pool, STDEXEC::get_stop_token(rcvr).stop_possible())
, duration_(duration)
, rcvr_(std::move(rcvr))
{}
void start() noexcept
{
auto dueTime = filetime_clock::now() + duration_;
ULARGE_INTEGER ticks;
ticks.QuadPart = dueTime.get_ticks();
FILETIME ft;
ft.dwLowDateTime = ticks.LowPart;
ft.dwHighDateTime = ticks.HighPart;
this->start_impl(STDEXEC::get_stop_token(STDEXEC::get_env(rcvr_)), ft);
}
private:
void set_value_impl() noexcept override
{
STDEXEC::set_value(std::move(rcvr_));
}
void set_stopped_impl() noexcept override
{
STDEXEC::set_stopped(std::move(rcvr_));
}
Duration duration_;
Rcvr rcvr_;
};
template <class Duration>
class windows_thread_pool::_schedule_after<Duration>::sender
{
public:
using sender_concept = STDEXEC::sender_tag;
using completion_signatures =
STDEXEC::completion_signatures<STDEXEC::set_value_t(),
STDEXEC::set_error_t(std::exception_ptr),
STDEXEC::set_stopped_t()>;
explicit sender(windows_thread_pool &pool, Duration duration)
: pool_(&pool)
, duration_(duration)
{}
template <class Rcvr>
requires STDEXEC::receiver_of<Rcvr, completion_signatures>
auto connect(Rcvr rcvr) const -> schedule_after_op<Duration, Rcvr>
{
return schedule_after_op<Duration, Rcvr>{*pool_, duration_, static_cast<Rcvr &&>(rcvr)};
}
[[nodiscard]]
auto get_env() const noexcept -> attrs
{
return attrs{pool_};
}
private:
windows_thread_pool *pool_;
Duration duration_;
};
/////////////////////////////////
// scheduler
class windows_thread_pool::scheduler
{
public:
using time_point = filetime_clock::time_point;
[[nodiscard]]
auto schedule() const noexcept -> schedule_sender
{
return schedule_sender{*pool_};
}
[[nodiscard]]
static auto now() noexcept -> time_point
{
return filetime_clock::now();
}
[[nodiscard]]
auto schedule_at(time_point tp) const noexcept -> schedule_at_sender
{
return schedule_at_sender{*pool_, tp};
}
template <class Duration>
[[nodiscard]]
auto schedule_after(Duration d) noexcept -> schedule_after_sender<Duration>
{
return schedule_after_sender<Duration>{*pool_, std::move(d)};
}
friend auto operator==(scheduler a, scheduler b) noexcept -> bool
{
return a.pool_ == b.pool_;
}
friend auto operator!=(scheduler a, scheduler b) noexcept -> bool
{
return a.pool_ != b.pool_;
}
private:
friend windows_thread_pool;
explicit scheduler(windows_thread_pool &pool) noexcept
: pool_(&pool)
{}
windows_thread_pool *pool_;
};
inline auto windows_thread_pool::attrs::query(
STDEXEC::get_completion_scheduler_t<STDEXEC::set_value_t>) const noexcept -> scheduler
{
return scheduler{*pool_};
}
/////////////////////////
// scheduler methods
inline auto windows_thread_pool::get_scheduler() noexcept -> windows_thread_pool::scheduler
{
return scheduler{*this};
}
/////////////////////////
inline windows_thread_pool::windows_thread_pool() noexcept
: threadPool_(nullptr)
{}
inline windows_thread_pool::windows_thread_pool(std::uint32_t minThreadCount,
std::uint32_t maxThreadCount)
: threadPool_(::CreateThreadpool(nullptr))
{
if (threadPool_ == nullptr)
{
DWORD errorCode = ::GetLastError();
throw std::system_error{static_cast<int>(errorCode),
std::system_category(),
"CreateThreadPool()"};
}
::SetThreadpoolThreadMaximum(threadPool_, maxThreadCount);
if (!::SetThreadpoolThreadMinimum(threadPool_, minThreadCount))
{
DWORD errorCode = ::GetLastError();
::CloseThreadpool(threadPool_);
throw std::system_error{static_cast<int>(errorCode),
std::system_category(),
"SetThreadpoolThreadMinimum()"};
}
}
inline windows_thread_pool::~windows_thread_pool()
{
if (threadPool_ != nullptr)
{
::CloseThreadpool(threadPool_);
}
}
inline windows_thread_pool::schedule_op_base::~schedule_op_base()
{
::CloseThreadpoolWork(work_);
::DestroyThreadpoolEnvironment(&environ_);
}
inline void windows_thread_pool::schedule_op_base::start() noexcept
{
::SubmitThreadpoolWork(work_);