|
| 1 | +/* |
| 2 | +** Copyright 2018 Bloomberg Finance L.P. |
| 3 | +** |
| 4 | +** Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +** you may not use this file except in compliance with the License. |
| 6 | +** You may obtain a copy of the License at |
| 7 | +** |
| 8 | +** http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +** |
| 10 | +** Unless required by applicable law or agreed to in writing, software |
| 11 | +** distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +** See the License for the specific language governing permissions and |
| 14 | +** limitations under the License. |
| 15 | +*/ |
| 16 | +//NOTE: DO NOT INCLUDE DIRECTLY |
| 17 | + |
| 18 | +//############################################################################################## |
| 19 | +//#################################### IMPLEMENTATIONS ######################################### |
| 20 | +//############################################################################################## |
| 21 | + |
| 22 | +#include <quantum/util/quantum_sequence_key_statistics.h> |
| 23 | +#include <quantum/interface/quantum_icoro_context_base.h> |
| 24 | +#include <list> |
| 25 | +#include <vector> |
| 26 | +#include <atomic> |
| 27 | +#include <memory> |
| 28 | + |
| 29 | +namespace Bloomberg { |
| 30 | +namespace quantum { |
| 31 | + |
| 32 | +template <class SequenceKey> |
| 33 | +struct SequencerLiteKeyData; |
| 34 | + |
| 35 | +template <class SequenceKey> |
| 36 | +struct SequencerLiteTask |
| 37 | +{ |
| 38 | + SequencerLiteTask(std::function<int(VoidContextPtr)>&& func, |
| 39 | + bool universal, |
| 40 | + void* opaque, |
| 41 | + int queueId, |
| 42 | + bool isHighPriority) : |
| 43 | + _func(std::move(func)), |
| 44 | + _pendingKeyCount(0), |
| 45 | + _universal(universal), |
| 46 | + _opaque(opaque), |
| 47 | + _queueId(queueId), |
| 48 | + _isHighPriority(isHighPriority) |
| 49 | + {} |
| 50 | + |
| 51 | + std::function<int(VoidContextPtr)> _func; // the function to run |
| 52 | + std::vector<SequencerLiteKeyData<SequenceKey>*> _keyData; // pointers to the key data of my keys |
| 53 | + unsigned int _pendingKeyCount; // number of key queues where I am not at the head |
| 54 | + bool _universal; // true of universal tasks |
| 55 | + void* _opaque; // opaque pointer passed by user |
| 56 | + int _queueId; // the queue to enqueue the task |
| 57 | + bool _isHighPriority; // high priority task |
| 58 | +}; |
| 59 | + |
| 60 | +template <class SequenceKey> |
| 61 | +struct SequencerLiteKeyData |
| 62 | +{ |
| 63 | + SequencerLiteKeyData() : |
| 64 | + _stats(std::make_shared<SequenceKeyStatisticsWriter>()) |
| 65 | + {} |
| 66 | + std::list<std::shared_ptr<SequencerLiteTask<SequenceKey>>> _tasks; // task queue |
| 67 | + std::shared_ptr<SequenceKeyStatisticsWriter> _stats; // stats for all tasks sharing this key |
| 68 | +}; |
| 69 | + |
| 70 | +inline const std::string& |
| 71 | +SequencerLiteConfigurationSchemaProvider::getJsonSchema() |
| 72 | +{ |
| 73 | + static std::string schema = R"JSON( |
| 74 | + { |
| 75 | + "$schema" : "http://json-schema.org/draft-04/schema#", |
| 76 | + "$id" : "bloomberg:sequencerlite.quantum.json", |
| 77 | + "title": "Quantum sequencerlite settings", |
| 78 | + "type": "object", |
| 79 | + "properties": { |
| 80 | + "bucketCount": { |
| 81 | + "type": "number", |
| 82 | + "default": 100 |
| 83 | + } |
| 84 | + }, |
| 85 | + "additionalProperties": false, |
| 86 | + "required": [] |
| 87 | + } |
| 88 | + )JSON"; |
| 89 | + return schema; |
| 90 | +} |
| 91 | + |
| 92 | +inline const std::string& |
| 93 | +SequencerLiteConfigurationSchemaProvider::getJsonSchemaUri() |
| 94 | +{ |
| 95 | + static std::string uri = "bloomberg:sequencerlite.quantum.json"; |
| 96 | + return uri; |
| 97 | +} |
| 98 | + |
| 99 | +template <class SequenceKey, class Hash, class KeyEqual, class Allocator> |
| 100 | +SequencerLiteConfiguration<SequenceKey, Hash, KeyEqual, Allocator>& |
| 101 | +SequencerLiteConfiguration<SequenceKey, Hash, KeyEqual, Allocator>::setBucketCount(size_t bucketCount) |
| 102 | +{ |
| 103 | + _bucketCount = bucketCount; |
| 104 | + return *this; |
| 105 | +} |
| 106 | + |
| 107 | +template <class SequenceKey, class Hash, class KeyEqual, class Allocator> |
| 108 | +size_t |
| 109 | +SequencerLiteConfiguration<SequenceKey, Hash, KeyEqual, Allocator>::getBucketCount() const |
| 110 | +{ |
| 111 | + return _bucketCount; |
| 112 | +} |
| 113 | + |
| 114 | +template <class SequenceKey, class Hash, class KeyEqual, class Allocator> |
| 115 | +void |
| 116 | +SequencerLiteConfiguration<SequenceKey, Hash, KeyEqual, Allocator>::setHash(const Hash& hash) |
| 117 | +{ |
| 118 | + _hash = hash; |
| 119 | +} |
| 120 | + |
| 121 | +template <class SequenceKey, class Hash, class KeyEqual, class Allocator> |
| 122 | +const Hash& |
| 123 | +SequencerLiteConfiguration<SequenceKey, Hash, KeyEqual, Allocator>::getHash() const |
| 124 | +{ |
| 125 | + return _hash; |
| 126 | +} |
| 127 | + |
| 128 | +template <class SequenceKey, class Hash, class KeyEqual, class Allocator> |
| 129 | +void |
| 130 | +SequencerLiteConfiguration<SequenceKey, Hash, KeyEqual, Allocator>::setKeyEqual(const KeyEqual& keyEqual) |
| 131 | +{ |
| 132 | + _keyEqual = keyEqual; |
| 133 | +} |
| 134 | + |
| 135 | +template <class SequenceKey, class Hash, class KeyEqual, class Allocator> |
| 136 | +const KeyEqual& |
| 137 | +SequencerLiteConfiguration<SequenceKey, Hash, KeyEqual, Allocator>::getKeyEqual() const |
| 138 | +{ |
| 139 | + return _keyEqual; |
| 140 | +} |
| 141 | + |
| 142 | +template <class SequenceKey, class Hash, class KeyEqual, class Allocator> |
| 143 | +void |
| 144 | +SequencerLiteConfiguration<SequenceKey, Hash, KeyEqual, Allocator>::setAllocator(const Allocator& allocator) |
| 145 | +{ |
| 146 | + _allocator = allocator; |
| 147 | +} |
| 148 | + |
| 149 | +template <class SequenceKey, class Hash, class KeyEqual, class Allocator> |
| 150 | +const Allocator& |
| 151 | +SequencerLiteConfiguration<SequenceKey, Hash, KeyEqual, Allocator>::getAllocator() const |
| 152 | +{ |
| 153 | + return _allocator; |
| 154 | +} |
| 155 | + |
| 156 | +template <class SequenceKey, class Hash, class KeyEqual, class Allocator> |
| 157 | +void |
| 158 | +SequencerLiteConfiguration<SequenceKey, Hash, KeyEqual, Allocator>::setExceptionCallback( |
| 159 | + const ExceptionCallback& |
| 160 | + exceptionCallback) |
| 161 | + |
| 162 | +{ |
| 163 | + _exceptionCallback = exceptionCallback; |
| 164 | +} |
| 165 | + |
| 166 | +template <class SequenceKey, class Hash, class KeyEqual, class Allocator> |
| 167 | +const typename SequencerLiteConfiguration<SequenceKey, Hash, KeyEqual, Allocator>::ExceptionCallback& |
| 168 | +SequencerLiteConfiguration<SequenceKey, Hash, KeyEqual, Allocator>::getExceptionCallback() const |
| 169 | +{ |
| 170 | + return _exceptionCallback; |
| 171 | +} |
| 172 | + |
| 173 | +}} |
0 commit comments