aboutsummaryrefslogtreecommitdiffstats
path: root/staging_vespalib/src/vespa/vespalib/util/isequencedtaskexecutor.cpp
blob: d05702cc85b874ad8cedff8bc78bf2cf67bd9587 (plain) (blame)
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
// Copyright 2020 Oath inc.. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "isequencedtaskexecutor.h"
#include <vespa/vespalib/stllike/hash_fun.h>
#include <vespa/vespalib/stllike/hashtable.h>
#include <cassert>

namespace vespalib {

namespace {
    constexpr uint8_t MAGIC = 255;
}

ISequencedTaskExecutor::ISequencedTaskExecutor(uint32_t numExecutors)
    : _component2Id(vespalib::hashtable_base::getModuloStl(numExecutors*8), MAGIC),
      _mutex(),
      _numExecutors(numExecutors),
      _nextId(0)
{
    assert(numExecutors < 256);
}

ISequencedTaskExecutor::~ISequencedTaskExecutor() = default;

ISequencedTaskExecutor::ExecutorId
ISequencedTaskExecutor::getExecutorId(vespalib::stringref componentId) const {
    vespalib::hash<vespalib::stringref> hashfun;
    return getExecutorId(hashfun(componentId));
}

ISequencedTaskExecutor::ExecutorId
ISequencedTaskExecutor::getExecutorId(uint64_t componentId) const {
    uint32_t shrunkId = componentId % _component2Id.size();
    uint8_t executorId = _component2Id[shrunkId];
    if (executorId == MAGIC) {
        std::lock_guard guard(_mutex);
        if (_component2Id[shrunkId] == MAGIC) {
            _component2Id[shrunkId] = _nextId % getNumExecutors();
            _nextId++;
        }
        executorId = _component2Id[shrunkId];
    }
    return ExecutorId(executorId);
}

}