blob: c0af72dbbb14600f214cc67a58eb96aae0f13a73 (
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
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "document_runnable.h"
#include <vespa/vespalib/util/exceptions.h>
#include <cassert>
namespace document {
Runnable::Runnable()
: _stateLock(),
_stateCond(),
_state(NOT_RUNNING)
{
}
Runnable::~Runnable() {
std::lock_guard monitorGuard(_stateLock);
assert(getState() == NOT_RUNNING);
}
bool Runnable::start(FastOS_ThreadPool& pool)
{
std::unique_lock guard(_stateLock);
_stateCond.wait(guard, [&](){ return (getState() != STOPPING);});
if (getState() != NOT_RUNNING) return false;
set_state(STARTING);
if (pool.NewThread(this) == nullptr) {
throw vespalib::IllegalStateException("Failed starting a new thread", VESPA_STRLOC);
}
return true;
}
void Runnable::set_state(State new_state) noexcept
{
_state.store(new_state, std::memory_order_relaxed);
}
bool Runnable::stopping() const noexcept
{
State s(getState());
return (s == STOPPING) || (s == RUNNING && GetThread()->GetBreakFlag());
}
bool Runnable::running() const noexcept
{
State s(getState());
// Must check break-flag too, as threadpool will use that to close
// down.
return (s == STARTING || (s == RUNNING && !GetThread()->GetBreakFlag()));
}
bool Runnable::stop()
{
std::lock_guard monitor(_stateLock);
if (getState() == STOPPING || getState() == NOT_RUNNING) return false;
GetThread()->SetBreakFlag();
set_state(STOPPING);
return onStop();
}
bool Runnable::onStop()
{
return true;
}
bool Runnable::join() const
{
std::unique_lock guard(_stateLock);
assert ((getState() != STARTING) && (getState() != RUNNING));
_stateCond.wait(guard, [&](){ return (getState() == NOT_RUNNING);});
return true;
}
FastOS_ThreadId Runnable::native_thread_id() const noexcept
{
return GetThread()->GetThreadId();
}
void Runnable::Run(FastOS_ThreadInterface*, void*)
{
{
std::lock_guard guard(_stateLock);
// Don't set state if its already at stopping. (And let run() be
// called even though about to stop for consistency)
if (getState() == STARTING) {
set_state(RUNNING);
}
}
// By not catching exceptions, they should abort whole application.
// We should thus not need to have a catch-all to set state to not
// running.
run();
{
std::lock_guard guard(_stateLock);
set_state(NOT_RUNNING);
_stateCond.notify_all();
}
}
}
|