forked from pplux/px
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpx_sched.h
executable file
·1314 lines (1170 loc) · 40.7 KB
/
px_sched.h
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) 2017-2019 Jose L. Hidalgo (PpluX)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
----------------------------------------------------------------------------- */
// USAGE
// Include this file in any file it needs to refer to it, but if you need
// to change any define, or use a custom job description is preferable to
// create a header that sets the defines, defines the job description and
// finally includes this file.
//
// In *ONE* C++ file you need to declare
// #define PX_SCHED_IMPLEMENTATION 1
// before including the file that contains px_sched.h
#ifndef PX_SCHED
#define PX_SCHED
// -- Job Object ----------------------------------------------------------
// if you want to use your own job objects, define the following
// macro and provide a struct px_sched::Job object with an operator() method.
//
// Example, a C-like pointer to function:
//
// #define PX_SCHED_CUSTOM_JOB_DEFINITION
// namespace px_sched {
// struct Job {
// void (*func)(void *arg);
// void *arg;
// void operator()() { func(arg); }
// };
// } // px namespace
//
// By default Jobs are simply std::function<void()>
//
#ifndef PX_SCHED_CUSTOM_JOB_DEFINITION
#include <functional>
namespace px_sched {
typedef std::function<void()> Job;
} // px namespace
#endif
// -----------------------------------------------------------------------------
// -- Backend selection --------------------------------------------------------
// Right now there is only two backends(single-threaded, and regular threads),
// in the future we will add windows-fibers and posix-ucontext. Meanwhile try
// to avoid waitFor(...) and use more runAfter if possible. Try not to suspend
// threads on external mutexes.
#if !defined(PX_SCHED_CONFIG_SINGLE_THREAD) && \
!defined(PX_SCHED_CONFIG_REGULAR_THREADS)
# define PX_SCHED_CONFIG_REGULAR_THREADS 1
#endif
#ifdef PX_SCHED_CONFIG_REGULAR_THREADS
# define PX_SCHED_IMP_REGULAR_THREADS 1
#else
# define PX_SCHED_IMP_REGULAR_THREADS 0
#endif
#ifdef PX_SCHED_CONFIG_SINGLE_THREAD
# define PX_SCHED_IMP_SINGLE_THREAD 1
#else
# define PX_SCHED_IMP_SINGLE_THREAD 0
#endif
#if ( PX_SCHED_IMP_SINGLE_THREAD \
+ PX_SCHED_IMP_REGULAR_THREADS \
) != 1
#error "PX_SCHED: Only one backend must be enabled (and at least one)"
#endif
// -----------------------------------------------------------------------------
#ifndef PX_SCHED_CACHE_LINE_SIZE
#define PX_SCHED_CACHE_LINE_SIZE 64
#endif
// **WARNING** ---> WORK IN PROGRESS <--- **WARNING**
// Enable if you want the threads to track resource locking, this might slow
// down things a bit.
#ifndef PX_SCHED_CHECK_DEADLOCKS
#define PX_SCHED_CHECK_DEADLOCKS 0
#endif
// -----------------------------------------------------------------------------
// some checks, can be omitted if you're confident there is no
// misuse of the library.
#ifndef PX_SCHED_DOES_CHECKS
#define PX_SCHED_DOES_CHECKS 1
#endif
// PX_SCHED_CHECK_FN -----------------------------------------------------------
// used to check conditions, to test for erros
// defined only if PX_SCHED_DOES_CHECKS is evaluated to true. By
// default uses printf and abort, use your own implementation by defining
// PX_SCHED_CHECK_FN before including this header.
#ifndef PX_SCHED_CHECK_FN
# if PX_SCHED_DOES_CHECKS
# include <stdlib.h>
# include <stdio.h>
# define PX_SCHED_CHECK_FN(cond, ...) \
if (!(cond)) { \
printf("-- PX_SCHED ERROR: -------------------\n"); \
printf("-- %s:%d\n", __FILE__, __LINE__); \
printf("--------------------------------------\n"); \
printf(__VA_ARGS__);\
printf("\n--------------------------------------\n"); \
abort(); \
}
# else
# define PX_SCHED_CHECK_FN(...) /*NO CHECK*/
# endif // PX_SCHED_DOES_CHECKS
#endif // PX_SCHED_CHECK_FN
// Function called at the begining of some functions to be able to
// monitor/trace the scheduler. It will be called as PX_SCHED_TRACE_FN("Name") and
// always in the scope to measure.
#ifndef PX_SCHED_TRACE_FN
#define PX_SCHED_TRACE_FN(...) /* NO TRACING */
#endif
#include <atomic>
#include <condition_variable>
#include <thread>
namespace px_sched {
// Sync object
class Sync {
uint32_t hnd = 0;
friend class Scheduler;
};
struct MemCallbacks {
void* (*alloc_fn)(size_t amount) = ::malloc;
void (*free_fn)(void *ptr) = ::free;
};
struct SchedulerParams {
uint16_t num_threads = 16; // num OS threads created
uint16_t max_running_threads = 0; // 0 --> will be set to max hardware concurrency
uint16_t max_number_tasks = 1024; // max number of simultaneous tasks
uint16_t thread_num_tries_on_idle = 16; // number of tries before suspend the thread
uint32_t thread_sleep_on_idle_in_microseconds = 5; // time spent waiting between tries
MemCallbacks mem_callbacks;
};
// -- Atomic -----------------------------------------------------------------
template<class T>
struct Atomic {
#if PX_SCHED_IMP_SINGLE_THREAD
T data = {};
Atomic() = default;
~Atomic() = default;
explicit Atomic(const T&d) : data(d) {}
bool compare_exchange_strong(T ¤t, const T &next) {
if (data == current) {
data = next;
return true;
}
current = data;
return false;
}
bool compare_exchange_weak(T ¤t, const T &next) {
return compare_exchange_strong(current,next);
}
const T load() const { return data; }
void store(const T& v) { data = v; }
T fetch_add(const T&v) {
T fetch(data);
data += v;
return fetch;
}
T fetch_sub(const T&v) {
T fetch(data);
data -= v;
return fetch;
}
T exchange(const T&v) {
T old(data);
data = v;
return old;
}
bool operator==(const Atomic &other) const { return other.data == data; }
bool operator==(const T &other) const { return other== data; }
#else
std::atomic<T> data = {};
Atomic() = default;
~Atomic() = default;
explicit Atomic(const T&d) : data(d) {}
bool compare_exchange_strong(T ¤t, const T &next) {
return data.compare_exchange_strong(current, next);
}
bool compare_exchange_weak(T ¤t, const T &next) {
return data.compare_exchange_weak(current, next);
}
const T load() const { return data.load(); }
void store(const T& v) { data = v; }
T fetch_add(const T&v) { return data.fetch_add(v); }
T fetch_sub(const T&v) { return data.fetch_sub(v); }
T exchange(const T&v) { return data.exchange(v); }
bool operator==(const Atomic &other) const { return other.data == data; }
bool operator==(const T &other) const { return other== data; }
#endif
};
// -- ObjectPool -------------------------------------------------------------
// holds up to 2^20 objects with ref counting and versioning
// used internally by the Scheduler for tasks and counters, but can also
// be used as a thread-safe object pool
template<class T>
struct ObjectPool {
const uint32_t kPosMask = 0x000FFFFF; // 20 bits
const uint32_t kRefMask = kPosMask; // 20 bits
const uint32_t kVerMask = 0xFFF00000; // 12 bits
const uint32_t kVerDisp = 20;
~ObjectPool();
void init(uint32_t count, const MemCallbacks &mem = MemCallbacks());
void reset();
// only access objects you've previously referenced
T& get(uint32_t hnd);
// only access objects you've previously referenced
const T& get(uint32_t hnd) const;
// given a position inside the pool returns the handler and also current ref
// count and current version number (only used for debugging)
uint32_t info(uint32_t pos, uint32_t *count, uint32_t *ver) const;
// max number of elements hold by the object pool
uint32_t size() const { return count_; }
// returns the handler of an object in the pool that can be used
// it also increments in one the number of references (no need to call ref)
uint32_t adquireAndRef();
void unref(uint32_t hnd) const;
// decrements the counter, if the object is no longer valid (last ref)
// the given function will be executed with the element
template<class F>
void unref(uint32_t hnd, F f) const;
// returns true if the given position was a valid object
bool ref(uint32_t hnd) const;
uint32_t refCount(uint32_t hnd) const;
uint32_t in_use() const { return in_use_.load();}
private:
void newElement(uint32_t pos) const;
void deleteElement(uint32_t pos) const;
struct D {
mutable Atomic<uint32_t> state;
uint32_t version = 0;
T element;
#if PX_SCHED_CACHE_LINE_SIZE
// Avoid false sharing between threads
static const size_t PADDING_ADJUSTMENT =
( PX_SCHED_CACHE_LINE_SIZE
- ((sizeof(state)+sizeof(version)+sizeof(element))%PX_SCHED_CACHE_LINE_SIZE)
) % PX_SCHED_CACHE_LINE_SIZE;
char padding[PADDING_ADJUSTMENT];
#endif
}; // D struct
mutable Atomic<uint32_t> in_use_;
Atomic<uint32_t> next_;
D *data_ = nullptr;
uint32_t count_ = 0;
MemCallbacks mem_;
};
class Scheduler {
public:
Scheduler();
~Scheduler();
void init(const SchedulerParams ¶ms = SchedulerParams());
void stop();
void run(const Job &job, Sync *out_sync_obj = nullptr);
void runAfter(Sync sync,const Job &job, Sync *out_sync_obj = nullptr);
void waitFor(Sync sync); //< suspend current thread
// returns the number of tasks not yet finished associated to the sync object
// thus 0 means all of them has finished (or the sync object was empty, or
// unused)
uint32_t numPendingTasks(Sync s);
bool hasFinished(Sync s) { return numPendingTasks(s) == 0; }
// Call this only to print the internal state of the scheduler, mainly if it
// stops working and want to see who is waiting for what, and so on.
void getDebugStatus(char *buffer, size_t buffer_size);
// manually increment the value of a Sync object. Sync objects triggers
// when they reach 0.
// *WARNING*: calling increment without a later decrement might leave
// tasks waiting forever, and will leak resources.
void incrementSync(Sync *s);
// manually decrement the value of a Sync object.
// *WARNING*: never call decrement on a sync object wihtout calling
// @incrementSync first.
void decrementSync(Sync *s);
// By default workers will be named as Worker-id
// The pointer passed here must be valid until set_current_thread is called
// again...
static void set_current_thread_name(const char *name);
static const char *current_thread_name();
// Call this method before a mutex/lock/etc... to notify the scheduler
static void CurrentThreadSleeps();
// call this again to notify the thread is again running
static void CurrentThreadWakesUp();
// Call this method before locking a resource, this will be used by the
// scheduler to wakeup another thread as a worker, and also can be used
// later to detect deadlocks (if compiled with PX_SCHED_CHECK_DEADLOCKS 1,
// WIP!)
static void CurrentThreadBeforeLockResource(const void *resource_ptr, const char *name = nullptr);
// Call this method after calling CurrentThreadBeforeLockResource, this will be
// used to notify the scheduler that this thread can continue working.
// If success is true, the lock was successful, false if the thread was not
// blocked but also didn't adquired the lock (try_lock)
static void CurrentThreadAfterLockResource(bool success);
// Call this method once the resouce is unlocked.
static void CurrentThreadReleasesResource(const void *resource_ptr);
const SchedulerParams& params() const { return params_; }
// Number of active threads (executing tasks)
uint32_t active_threads() const { return active_threads_.load(); }
uint32_t num_tasks() const { return tasks_.in_use(); }
uint32_t num_counters() const { return counters_.in_use(); }
#if PX_SCHED_IMP_REGULAR_THREADS
uint32_t num_tasks_ready() { return ready_tasks_.in_use(); }
#endif
#if PX_SCHED_IMP_SINGLE_THREAD
uint32_t num_tasks_ready() { return 0; }
#endif
private:
struct TLS;
static TLS* tls();
void wakeUpOneThread();
SchedulerParams params_;
Atomic<uint32_t> active_threads_;
Atomic<uint32_t> running_;
struct WaitFor;
struct Task {
Job job;
uint32_t counter_id = 0;
Atomic<uint32_t> next_sibling_task;
};
struct Counter {
Atomic<uint32_t> task_id;
Atomic<uint32_t> user_count;
WaitFor *wait_ptr = nullptr;
};
ObjectPool<Task> tasks_;
ObjectPool<Counter> counters_;
uint32_t createTask(const Job &job, Sync *out_sync_obj);
uint32_t createCounter();
void unrefCounter(uint32_t counter_hnd);
#if PX_SCHED_IMP_REGULAR_THREADS
struct IndexQueue {
~IndexQueue() {
PX_SCHED_CHECK_FN(list_ == nullptr, "IndexQueue Resources leaked...");
}
void reset() {
if (list_) {
mem_.free_fn(list_);
list_ = nullptr;
}
size_ = 0;
in_use_ = 0;
}
void init(uint16_t max, const MemCallbacks &mem_cb = MemCallbacks()) {
_lock();
reset();
mem_ = mem_cb;
size_ = max;
in_use_ = 0;
list_ = static_cast<uint32_t*>(mem_.alloc_fn(sizeof(uint32_t)*size_));
_unlock();
}
void push(uint32_t p) {
_lock();
PX_SCHED_CHECK_FN(in_use_ < size_, "IndexQueue Overflow total in use %hu (max %hu)", in_use_, size_);
uint16_t pos = (current_ + in_use_)%size_;
list_[pos] = p;
in_use_++;
_unlock();
}
uint16_t in_use() {
_lock();
uint16_t result = in_use_;
_unlock();
return result;
}
bool pop(uint32_t *res) {
_lock();
bool result = false;
if (in_use_) {
if (res) *res = list_[current_];
current_ = (current_+1)%size_;
in_use_--;
result = true;
}
_unlock();
return result;
}
void _unlock() { lock_.clear(std::memory_order_release); }
void _lock() {
while(lock_.test_and_set(std::memory_order_acquire)) {
std::this_thread::yield();
}
}
uint32_t *list_ = nullptr;
std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
MemCallbacks mem_;
volatile uint16_t size_ = 0;
volatile uint16_t in_use_ = 0;
volatile uint16_t current_ = 0;
};
struct WaitFor {
explicit WaitFor()
: owner(std::this_thread::get_id())
, ready(false) {}
void wait() {
PX_SCHED_TRACE_FN("WaitFor");
PX_SCHED_CHECK_FN(std::this_thread::get_id() == owner,
"WaitFor::wait can only be invoked from the thread "
"that created the object");
std::unique_lock<std::mutex> lk(mutex);
if(!ready) {
condition_variable.wait(lk);
}
}
void signal() {
if (owner != std::this_thread::get_id()) {
std::lock_guard<std::mutex> lk(mutex);
ready = true;
condition_variable.notify_all();
} else {
ready = true;
}
}
private:
std::thread::id const owner;
std::mutex mutex;
std::condition_variable condition_variable;
bool ready;
};
struct Worker {
std::thread thread;
// setted by the thread when is sleep
Atomic<WaitFor*> wake_up;
TLS *thread_tls = nullptr;
uint16_t thread_index = 0xFFFF;
};
uint16_t wakeUpThreads(uint16_t max_num_threads);
Worker *workers_ = nullptr;
IndexQueue ready_tasks_;
static void WorkerThreadMain(Scheduler *schd, Worker *);
#endif
};
//-- Optional: Mutex template to encapsultae scheduler notification ----------
template<class M>
class Mutex {
public:
~Mutex() { lock(); }
void lock() {
std::thread::id tid = std::this_thread::get_id();
if (owner_ == tid) {
count_++;
return;
}
Scheduler::CurrentThreadBeforeLockResource(&mutex_);
mutex_.lock();
owner_ = tid;
count_ = 1;
Scheduler::CurrentThreadAfterLockResource(true);
}
void unlock() {
std::thread::id tid = std::this_thread::get_id();
PX_SCHED_CHECK_FN(owner_ == tid, "Invalid Mutex::unlock owner mistmatch");
count_--;
if (count_ == 0) {
owner_ = std::thread::id();
Scheduler::CurrentThreadReleasesResource(&mutex_);
mutex_.unlock();
}
}
bool try_lock() {
std::thread::id tid = std::this_thread::get_id();
if (owner_ == tid) {
count_++;
return true;
}
Scheduler::CurrentThreadBeforeLockResource(&mutex_);
bool result = mutex_.try_lock();
if (result) {
owner_ = tid;
count_ = 1;
}
Scheduler::CurrentThreadAfterLockResource(result);
return result;
}
private:
std::thread::id owner_ ;
uint32_t count_ = 0;
M mutex_;
};
//-- Optional: Spinlock ------------------------------------------------------
class Spinlock {
public:
Spinlock() : owner_(std::thread::id()), count_(0) {}
~Spinlock() {
lock();
}
void lock() {
while(!try_lock()) { std::this_thread::yield(); }
}
void unlock() {
std::thread::id tid = std::this_thread::get_id();
PX_SCHED_CHECK_FN(owner_ == tid, "Invalid Spinlock::unlock owner mistmatch");
count_--;
if (count_ == 0) {
owner_.store(std::thread::id());
}
}
bool try_lock() {
std::thread::id tid = std::this_thread::get_id();
if (owner_ == tid) {
count_++;
return true;
}
std::thread::id expected;
if (owner_.compare_exchange_weak(expected, tid)) {
count_ = 1;
return true;
}
return false;
}
private:
Atomic<std::thread::id> owner_ ;
uint32_t count_;
};
//-- Object pool implementation ----------------------------------------------
template<class T>
inline ObjectPool<T>::~ObjectPool() {
reset();
}
template<class T>
void ObjectPool<T>::newElement(uint32_t pos) const {
new (&data_[pos].element) T;
in_use_.fetch_add(1);
}
template<class T>
void ObjectPool<T>::deleteElement(uint32_t pos) const {
data_[pos].element.~T();
in_use_.fetch_sub(1);
}
template<class T>
inline void ObjectPool<T>::init(uint32_t count, const MemCallbacks &mem_cb) {
reset();
mem_ = mem_cb;
data_ = static_cast<D*>(mem_.alloc_fn(sizeof(D)*count));
for(uint32_t i = 0; i < count; ++i) {
data_[i].state.store(0xFFFu<< kVerDisp);
}
count_ = count;
next_.store(0);
}
template<class T>
inline void ObjectPool<T>::reset() {
count_ = 0;
next_.store(0);
if (data_) {
mem_.free_fn(data_);
data_ = nullptr;
}
}
// only access objects you've previously referenced
template<class T>
inline T& ObjectPool<T>::get(uint32_t hnd) {
uint32_t pos = hnd & kPosMask;
PX_SCHED_CHECK_FN(pos < count_, "Invalid access to pos %u hnd:%u", pos, count_);
return data_[pos].element;
}
// only access objects you've previously referenced
template< class T>
inline const T& ObjectPool<T>::get(uint32_t hnd) const {
uint32_t pos = hnd & kPosMask;
PX_SCHED_CHECK_FN(pos < count_, "Invalid access to pos %u hnd:%u", pos, count_);
return data_[pos].element;
}
template< class T>
inline uint32_t ObjectPool<T>::info(uint32_t pos, uint32_t *count, uint32_t *ver) const {
PX_SCHED_CHECK_FN(pos < count_, "Invalid access to pos %u hnd:%u", pos, count_);
uint32_t s = data_[pos].state.load();
if (count) *count = (s & kRefMask);
if (ver) *ver = (s & kVerMask) >> kVerDisp;
return (s&kVerMask) | pos;
}
template<class T>
inline uint32_t ObjectPool<T>::adquireAndRef() {
PX_SCHED_TRACE_FN("ObjectPool<T>::adquireAndRef");
uint32_t tries = 0;
for(;;) {
uint32_t pos = (next_.fetch_add(1)%count_);
D& d = data_[pos];
uint32_t version = (d.state.load() & kVerMask) >> kVerDisp;
// note: avoid 0 as version
uint32_t newver = (version+1) & 0xFFF;
if (newver == 0) newver = 1;
// instead of using 1 as initial ref, we use 2, when we see 1
// in the future we know the object must be freed, but it wont
// be actually freed until it reaches 0
uint32_t newvalue = (newver << kVerDisp) + 2;
uint32_t expected = version << kVerDisp;
if (d.state.compare_exchange_strong(expected, newvalue)) {
newElement(pos); //< initialize
return (newver << kVerDisp) | (pos & kPosMask);
}
tries++;
// TODO... make this, optional...
PX_SCHED_CHECK_FN(tries < count_*count_, "It was not possible to find a valid index after %u tries", tries);
}
}
template< class T>
inline void ObjectPool<T>::unref(uint32_t hnd) const {
uint32_t pos = hnd & kPosMask;
uint32_t ver = (hnd & kVerMask);
D& d = data_[pos];
for(;;) {
uint32_t prev = d.state.load();
uint32_t next = prev - 1;
PX_SCHED_CHECK_FN((prev & kVerMask) == ver,
"Invalid unref HND = %u(%u), Versions: %u vs %u",
pos, hnd, prev & kVerMask, ver);
PX_SCHED_CHECK_FN((prev & kRefMask) > 1,
"Invalid unref HND = %u(%u), invalid ref count",
pos, hnd);
if (d.state.compare_exchange_strong(prev, next)) {
if ((next & kRefMask) == 1) {
deleteElement(pos);
d.state.store(0);
}
return;
}
}
}
// decrements the counter, if the objet is no longer valid (las ref)
// the given function will be executed with the element
template<class T>
template<class F>
inline void ObjectPool<T>::unref(uint32_t hnd, F f) const {
uint32_t pos = hnd & kPosMask;
uint32_t ver = (hnd & kVerMask);
D& d = data_[pos];
for(;;) {
uint32_t prev = d.state.load();
uint32_t next = prev - 1;
PX_SCHED_CHECK_FN((prev & kVerMask) == ver,
"Invalid unref HND = %u(%u), Versions: %u vs %u",
pos, hnd, prev & kVerMask, ver);
PX_SCHED_CHECK_FN((prev & kRefMask) > 1,
"Invalid unref HND = %u(%u), invalid ref count",
pos, hnd);
if (d.state.compare_exchange_strong(prev, next)) {
if ((next & kRefMask) == 1) {
f(d.element);
deleteElement(pos);
d.state.store(0);
}
return;
}
}
}
template< class T>
inline bool ObjectPool<T>::ref(uint32_t hnd) const{
if (!hnd) return false;
uint32_t pos = hnd & kPosMask;
uint32_t ver = (hnd & kVerMask);
D& d = data_[pos];
for (;;) {
uint32_t prev = d.state.load();
uint32_t next_c =((prev & kRefMask) +1);
if ((prev & kVerMask) != ver || next_c <= 2) return false;
PX_SCHED_CHECK_FN(next_c == (next_c & kRefMask), "Too many references...");
uint32_t next = (prev & kVerMask) | next_c ;
if (d.state.compare_exchange_strong(prev, next)) {
return true;
}
}
}
template< class T>
inline uint32_t ObjectPool<T>::refCount(uint32_t hnd) const{
if (!hnd) return 0;
uint32_t pos = hnd & kPosMask;
uint32_t ver = (hnd & kVerMask);
D& d = data_[pos];
uint32_t current = d.state.load();
if ((current & kVerMask) != ver ) return 0;
return (current & kRefMask);
}
} // end of px namespace
#endif // PX_SCHED
//----------------------------------------------------------------------------
// -- IMPLEMENTATION ---------------------------------------------------------
//----------------------------------------------------------------------------
#ifdef PX_SCHED_IMPLEMENTATION
#ifndef PX_SCHED_IMPLEMENTATION_DONE
#define PX_SCHED_IMPLEMENTATION_DONE 1
#ifdef PX_SCHED_ATLERNATIVE_TLS
// used to store custom TLS... some platform might have problems with
// TLS (iOS used to be one)
#include <unordered_map>
#endif
#if PX_SCHED_CHECK_DEADLOCKS
#include <vector>
#include <algorithm>
#endif
namespace px_sched {
struct Scheduler::TLS {
const char *name = nullptr;
Scheduler *scheduler = nullptr;
struct Resource {
const void *ptr;
const char *name;
};
Resource next_lock = {nullptr, nullptr};
#if PX_SCHED_CHECK_DEADLOCKS
std::mutex adquired_locks_m;
std::vector<Resource> adquired_locks;
#endif
};
Scheduler::TLS* Scheduler::tls() {
#ifdef PX_SCHED_ATLERNATIVE_TLS
static std::unordered_map<std::thread::id, TLS> data;
static Atomic<uint32_t> in_use(0);
for(;;) {
uint32_t expected = 0;
if (in_use.compare_exchange_weak(expected, 1)) break;
}
auto result = &data[std::this_thread::get_id()];
in_use.store(0);
return result;
#else
static thread_local TLS tls;
return &tls;
#endif
}
void Scheduler::set_current_thread_name(const char *name) {
TLS *d = tls();
d->name = name;
}
const char *Scheduler::current_thread_name() {
TLS *d = tls();
return d->name;
}
void Scheduler::CurrentThreadSleeps() {
CurrentThreadBeforeLockResource(nullptr);
}
void Scheduler::CurrentThreadWakesUp() {
CurrentThreadAfterLockResource(false);
}
void Scheduler::CurrentThreadBeforeLockResource(const void *resource_ptr, const char *name) {
// if the lock might work, wake up one thread to replace this one
TLS *d = tls();
if (d->scheduler && d->scheduler->running_.load()) {
d->scheduler->active_threads_.fetch_sub(1);
d->scheduler->wakeUpOneThread();
}
d->next_lock = {resource_ptr, name};
}
void Scheduler::CurrentThreadAfterLockResource(bool success) {
// mark this thread as active (so eventually one thread will step down)
TLS *d = tls();
if (d->scheduler && d->scheduler->running_.load()) {
d->scheduler->active_threads_.fetch_add(1);
}
if (success && d->next_lock.ptr) {
#if PX_SCHED_CHECK_DEADLOCKS
std::lock_guard<std::mutex> l(d->adquired_locks_m);
d->adquired_locks.push_back(d->next_lock);
#endif
}
d->next_lock = {nullptr,nullptr}; // reset
}
void Scheduler::CurrentThreadReleasesResource(const void *resource_ptr) {
#if PX_SCHED_CHECK_DEADLOCKS
TLS *d = tls();
if (resource_ptr) {
std::lock_guard<std::mutex> l(d->adquired_locks_m);
auto f = d->adquired_locks.begin();
while (f != d->adquired_locks.end()) {
if (f->ptr == resource_ptr) break;
f++;
};
PX_SCHED_CHECK_FN(f != d->adquired_locks.end(), "Can't find resource %p as adquired", resource_ptr);
// replace resource with last, and pop (to avoid shifting elements in the vector)
std::swap(*f, d->adquired_locks.back());
d->adquired_locks.pop_back();
}
#else
(void)resource_ptr;
#endif
}
}
// Common to all implementations of px_sched (Single Threaded and Multi Threaded)
namespace px_sched {
uint32_t Scheduler::createCounter() {
PX_SCHED_TRACE_FN("CreateCounter");
uint32_t hnd = counters_.adquireAndRef();
Counter *c = &counters_.get(hnd);
c->task_id.store(0);
c->user_count.store(0);
c->wait_ptr = nullptr;
return hnd;
}
uint32_t Scheduler::createTask(const Job &job, Sync *sync_obj) {
PX_SCHED_TRACE_FN("CreateTask");
uint32_t ref = tasks_.adquireAndRef();
Task *task = &tasks_.get(ref);
task->job = job;
task->counter_id = 0;
task->next_sibling_task.store(0);
if (sync_obj) {
bool new_counter = !counters_.ref(sync_obj->hnd);
if (new_counter) {
sync_obj->hnd = createCounter();
}
task->counter_id = sync_obj->hnd;
}
return ref;
}
void Scheduler::incrementSync(Sync *s) {
PX_SCHED_TRACE_FN("IncrementSync");
if (!counters_.ref(s->hnd)) {
s->hnd = createCounter();
}
Counter &c = counters_.get(s->hnd);
uint32_t prev = c.user_count.fetch_add(1);
if (prev != 0) {
unrefCounter(s->hnd);
}
}
void Scheduler::decrementSync(Sync *s) {
PX_SCHED_TRACE_FN("DecrementSync");
if (counters_.ref(s->hnd)) {
Counter &c = counters_.get(s->hnd);
uint32_t prev = c.user_count.fetch_sub(1);
if (prev == 1) {
// last one should unref twice
unrefCounter(s->hnd);
}
unrefCounter(s->hnd);
}
}
}
#if PX_SCHED_IMP_SINGLE_THREAD
namespace px_sched {
Scheduler::Scheduler() {}
Scheduler::~Scheduler() {}
void Scheduler::init(const SchedulerParams ¶ms) {
params_ = params;
tasks_.init(params_.max_number_tasks, params_.mem_callbacks);
counters_.init(params_.max_number_tasks, params_.mem_callbacks);
}
void Scheduler::stop() {
tasks_.reset();
counters_.reset();
}
void Scheduler::run(const Job &job, Sync *s) {
Job j(job);
j();
if (s) decrementSync(s);
}
void Scheduler::runAfter(Sync trigger, const Job &job, Sync *s) {
if (counters_.ref(trigger.hnd)) {
uint32_t t_ref = createTask(job, s);
Counter *c = &counters_.get(trigger.hnd);
for(;;) {
uint32_t current = c->task_id.load();
if (c->task_id.compare_exchange_strong(current, t_ref)) {
Task *task = &tasks_.get(t_ref);
task->next_sibling_task.store(current);
break;
}
}
unrefCounter(trigger.hnd);
} else {
run(job, s);
}
}
void Scheduler::waitFor(Sync s) {
PX_SCHED_CHECK_FN(!(counters_.ref(s.hnd)), "Invalid, on SingleThreaded mode we can not wait for a sync object...");
}
uint32_t Scheduler::numPendingTasks(Sync s){
return counters_.refCount(s.hnd);
}