Changeset f9b2e73
- Timestamp:
- Nov 18, 2020, 4:51:39 PM (4 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 7192145, 7f8fbe3
- Parents:
- 7c9ac4a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
benchmark/readyQ/rq_bench.hpp
r7c9ac4a rf9b2e73 1 1 #include <cassert> 2 #include <climits> 3 #include <cstdint> 4 #include <cstdio> 2 5 3 6 #include <time.h> // timespec … … 5 8 6 9 enum { TIMEGRAN = 1000000000LL }; // nanosecond granularity, except for timeval 7 8 9 #include <libfibre/fibre.h>10 11 10 12 11 … … 57 56 uint64_t from_fseconds(double sec) { return sec * TIMEGRAN; } 58 57 58 template<typename Sleeper> 59 59 void wait(const uint64_t & start, bool is_tty) { 60 60 for(;;) { 61 Fibre::usleep(100000);61 Sleeper::usleep(100000); 62 62 uint64_t end = getTimeNsec(); 63 63 uint64_t delta = end - start; … … 75 75 } 76 76 77 class __attribute__((aligned(128))) bench_sem {78 Fibre * volatile ptr = nullptr;79 public:80 inline bool wait() {81 static Fibre * const ready = reinterpret_cast<Fibre * const>(1ull);82 for(;;) {83 Fibre * expected = this->ptr;84 if(expected == ready) {85 if(__atomic_compare_exchange_n(&this->ptr, &expected, nullptr, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {86 return false;87 }88 }89 else {90 /* paranoid */ assert( expected == nullptr );91 if(__atomic_compare_exchange_n(&this->ptr, &expected, fibre_self(), false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {92 fibre_park();93 return true;94 }95 }96 97 }98 }99 100 inline bool post() {101 static Fibre * const ready = reinterpret_cast<Fibre * const>(1ull);102 for(;;) {103 Fibre * expected = this->ptr;104 if(expected == ready) return false;105 if(expected == nullptr) {106 if(__atomic_compare_exchange_n(&this->ptr, &expected, ready, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {107 return false;108 }109 }110 else {111 if(__atomic_compare_exchange_n(&this->ptr, &expected, nullptr, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {112 fibre_unpark( expected );113 return true;114 }115 }116 }117 }118 };119 120 77 // ========================================================================================== 78 #include <cstdlib> 121 79 #include <cstring> 80 81 #include <algorithm> 122 82 123 83 //-----------------------------------------------------------------------------
Note: See TracChangeset
for help on using the changeset viewer.