Skip to content

Commit 8d66352

Browse files
Arsentiy Milchakovmpimenov
authored andcommitted
[base][platform] return type is changed for ThreadPool
1 parent bb83367 commit 8d66352

24 files changed

+138
-193
lines changed

android/UnitTests/jni/mock.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,6 @@ void AndroidThreadDetachFromJVM()
217217
LOG(LWARNING, ("AndroidThreadDetachFromJVM() is not implemented."));
218218
}
219219

220-
void Platform::RunOnGuiThread(TFunctor const & fn)
221-
{
222-
LOG(LWARNING, ("Platform::RunOnGuiThread() is not implemented."));
223-
}
224-
225220
Platform::EConnectionType Platform::ConnectionStatus()
226221
{
227222
LOG(LWARNING, ("Platform::ConnectionStatus() is not implemented."));

android/jni/com/mapswithme/platform/GuiThread.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ void GuiThread::ProcessTask(jlong task)
3030
(*t)();
3131
}
3232

33-
base::TaskLoop::TaskId GuiThread::Push(Task && task)
33+
base::TaskLoop::PushResult GuiThread::Push(Task && task)
3434
{
3535
// Pointer will be deleted in ProcessTask.
3636
auto t = new Task(std::move(task));
3737
jni::GetEnv()->CallVoidMethod(m_object, m_method, reinterpret_cast<jlong>(t));
38-
return kIncorrectId;
38+
return {true, kIncorrectId};
3939
}
4040

41-
base::TaskLoop::TaskId GuiThread::Push(Task const & task)
41+
base::TaskLoop::PushResult GuiThread::Push(Task const & task)
4242
{
4343
// Pointer will be deleted in ProcessTask.
4444
auto t = new Task(task);
4545
jni::GetEnv()->CallVoidMethod(m_object, m_method, reinterpret_cast<jlong>(t));
46-
return kIncorrectId;
46+
return {true, kIncorrectId};
4747
}
4848
} // namespace android

android/jni/com/mapswithme/platform/GuiThread.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class GuiThread : public base::TaskLoop
1515
static void ProcessTask(jlong task);
1616

1717
// TaskLoop overrides:
18-
TaskId Push(Task && task) override;
19-
TaskId Push(Task const & task) override;
18+
PushResult Push(Task && task) override;
19+
PushResult Push(Task const & task) override;
2020

2121
private:
2222
jobject m_object = nullptr;

android/jni/com/mapswithme/platform/Platform.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,6 @@ std::string Platform::DeviceModel() const
103103
return jni::ToNativeString(env, deviceModel);
104104
}
105105

106-
void Platform::RunOnGuiThread(base::TaskLoop::Task && task)
107-
{
108-
android::Platform::Instance().RunOnGuiThread(std::move(task));
109-
}
110-
111-
void Platform::RunOnGuiThread(base::TaskLoop::Task const & task)
112-
{
113-
android::Platform::Instance().RunOnGuiThread(task);
114-
}
115-
116106
Platform::EConnectionType Platform::ConnectionStatus()
117107
{
118108
JNIEnv * env = jni::GetEnv();
@@ -158,11 +148,6 @@ uint8_t Platform::GetBatteryLevel()
158148
return static_cast<uint8_t>(env->CallStaticIntMethod(clazzBatteryState, getLevelMethodId));
159149
}
160150

161-
void Platform::SetGuiThread(std::unique_ptr<base::TaskLoop> guiThread)
162-
{
163-
android::Platform::Instance().SetGuiThread(move(guiThread));
164-
}
165-
166151
namespace platform
167152
{
168153
platform::NetworkPolicy GetCurrentNetworkPolicy()
@@ -310,11 +295,6 @@ void Platform::SendMarketingEvent(std::string const & tag,
310295
jni::TScopedLocalObjectArrayRef(env, jni::ToKeyValueArray(env, params)).get());
311296
}
312297

313-
void Platform::SetGuiThread(std::unique_ptr<base::TaskLoop> guiThread)
314-
{
315-
m_guiThread = std::move(guiThread);
316-
}
317-
318298
void Platform::AndroidSecureStorage::Init(JNIEnv * env)
319299
{
320300
if (m_secureStorageClass != nullptr)

android/jni/com/mapswithme/platform/Platform.hpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ class Platform : public ::Platform
3535

3636
bool HasAvailableSpaceForWriting(uint64_t size) const;
3737

38-
template <typename Task>
39-
void RunOnGuiThread(Task && task)
40-
{
41-
ASSERT(m_guiThread, ());
42-
m_guiThread->Push(std::forward<Task>(task));
43-
}
44-
4538
void SendPushWooshTag(std::string const & tag, std::vector<std::string> const & values);
4639
void SendMarketingEvent(std::string const & tag, std::map<std::string, std::string> const & params);
4740

@@ -71,8 +64,6 @@ class Platform : public ::Platform
7164
jmethodID m_sendPushWooshTagsMethod = nullptr;
7265
jmethodID m_sendAppsFlyerTagsMethod = nullptr;
7366
AndroidSecureStorage m_secureStorage;
74-
75-
std::unique_ptr<base::TaskLoop> m_guiThread;
7667
};
7768

7869
extern int GetAndroidSdkVersion();

base/base_tests/thread_pool_delayed_tests.cpp

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,25 @@ UNIT_TEST(ThreadPoolDelayed_SimpleSync)
4040
bool done = false;
4141

4242
ThreadPool thread;
43-
TEST_NOT_EQUAL(thread.Push([&value]() { ++value; }), ThreadPool::kIncorrectId, ());
44-
TEST_NOT_EQUAL(thread.Push([&value]() { value *= 2; }), ThreadPool::kIncorrectId, ());
45-
TEST_NOT_EQUAL(thread.Push([&value]() { value = value * value * value; }), ThreadPool::kIncorrectId, ());
46-
TEST_NOT_EQUAL(thread.Push([&]() {
43+
auto pushResult = thread.Push([&value]() { ++value; });
44+
TEST(pushResult.m_isSuccess, ());
45+
TEST_NOT_EQUAL(pushResult.m_id, ThreadPool::kIncorrectId, ());
46+
47+
pushResult = thread.Push([&value]() { value *= 2; });
48+
TEST(pushResult.m_isSuccess, ());
49+
TEST_NOT_EQUAL(pushResult.m_id, ThreadPool::kIncorrectId, ());
50+
51+
pushResult = thread.Push([&value]() { value = value * value * value; });
52+
TEST(pushResult.m_isSuccess, ());
53+
TEST_NOT_EQUAL(pushResult.m_id, ThreadPool::kIncorrectId, ());
54+
55+
pushResult = thread.Push([&]() {
4756
lock_guard<mutex> lk(mu);
4857
done = true;
4958
cv.notify_one();
50-
}), ThreadPool::kIncorrectId, ());
59+
});
60+
TEST(pushResult.m_isSuccess, ());
61+
TEST_NOT_EQUAL(pushResult.m_id, ThreadPool::kIncorrectId, ());
5162

5263
{
5364
unique_lock<mutex> lk(mu);
@@ -62,11 +73,17 @@ UNIT_TEST(ThreadPoolDelayed_SimpleFlush)
6273
int value = 0;
6374
{
6475
ThreadPool thread;
65-
TEST_NOT_EQUAL(thread.Push([&value]() { ++value; }), ThreadPool::kIncorrectId, ());
66-
TEST_NOT_EQUAL(thread.Push([&value]() {
76+
auto pushResult = thread.Push([&value]() { ++value; });
77+
TEST(pushResult.m_isSuccess, ());
78+
TEST_NOT_EQUAL(pushResult.m_id, ThreadPool::kIncorrectId, ());
79+
80+
pushResult = thread.Push([&value]() {
6781
for (int i = 0; i < 10; ++i)
6882
value *= 2;
69-
}), ThreadPool::kIncorrectId, ());
83+
});
84+
TEST(pushResult.m_isSuccess, ());
85+
TEST_NOT_EQUAL(pushResult.m_id, ThreadPool::kIncorrectId, ());
86+
7087
TEST(thread.Shutdown(ThreadPool::Exit::ExecPending), ());
7188
}
7289
TEST_EQUAL(value, 1024, ());
@@ -80,12 +97,15 @@ UNIT_TEST(ThreadPoolDelayed_PushFromPendingTask)
8097
auto f = p.get_future();
8198

8299
ThreadPool thread;
83-
auto const id = thread.Push([&f, &thread]() {
100+
auto const pushResult = thread.Push([&f, &thread]() {
84101
f.get();
85-
auto const id = thread.Push([]() { TEST(false, ("This task should not be executed")); });
86-
TEST_EQUAL(id, ThreadPool::kIncorrectId, ());
102+
auto const pushResult = thread.Push([]() { TEST(false, ("This task should not be executed")); });
103+
TEST(!pushResult.m_isSuccess, ());
104+
TEST_EQUAL(pushResult.m_id, ThreadPool::kIncorrectId, ());
87105
});
88-
TEST_NOT_EQUAL(id, ThreadPool::kIncorrectId, ());
106+
TEST(pushResult.m_isSuccess, ());
107+
TEST_NOT_EQUAL(pushResult.m_id, ThreadPool::kIncorrectId, ());
108+
89109
thread.Shutdown(ThreadPool::Exit::ExecPending);
90110
p.set_value();
91111
}
@@ -112,15 +132,19 @@ UNIT_TEST(ThreadPoolDelayed_DelayedAndImmediateTasks)
112132
auto & entry = delayedEntries[i];
113133
entry.m_start = thread.Now();
114134
entry.m_delay = milliseconds(i + 1);
115-
auto const id = thread.PushDelayed(entry.m_delay, [&]() { entry.m_end = thread.Now(); });
116-
TEST_NOT_EQUAL(id, ThreadPool::kIncorrectId, ());
135+
136+
auto const pushResult = thread.PushDelayed(entry.m_delay, [&]() { entry.m_end = thread.Now(); });
137+
TEST(pushResult.m_isSuccess, ());
138+
TEST_NOT_EQUAL(pushResult.m_id, ThreadPool::kIncorrectId, ());
117139
}
118140

119141
for (int i = 0; i < kNumTasks; ++i)
120142
{
121143
auto & entry = immediateEntries[i];
122-
auto const id = thread.Push([&]() { entry = thread.Now(); });
123-
TEST_NOT_EQUAL(id, ThreadPool::kIncorrectId, ());
144+
auto const pushResult = thread.Push([&]() { entry = thread.Now(); });
145+
146+
TEST(pushResult.m_isSuccess, ());
147+
TEST_NOT_EQUAL(pushResult.m_id, ThreadPool::kIncorrectId, ());
124148
}
125149

126150
thread.Shutdown(ThreadPool::Exit::ExecPending);
@@ -147,23 +171,26 @@ UNIT_TEST(ThreadPoolDelayed_CancelImmediate)
147171
TaskLoop::TaskId cancelTaskId;
148172
ThreadPool thread;
149173
{
150-
auto const id = thread.Push([&value]() {
174+
auto const pushResult = thread.Push([&value]() {
151175
++value;
152176
testing::Wait();
153177
});
154-
TEST_EQUAL(id, ThreadPool::kImmediateMinId, ());
178+
TEST(pushResult.m_isSuccess, ());
179+
TEST_EQUAL(pushResult.m_id, ThreadPool::kImmediateMinId, ());
155180
}
156181

157182
{
158-
cancelTaskId = thread.Push([&]() { value += 1023; });
183+
auto const pushResult = thread.Push([&]() { value += 1023; });
184+
TEST(pushResult.m_isSuccess, ());
185+
TEST_EQUAL(pushResult.m_id, ThreadPool::kImmediateMinId + 1, ());
159186

160-
TEST_EQUAL(cancelTaskId, ThreadPool::kImmediateMinId + 1, ());
187+
cancelTaskId = pushResult.m_id;
161188
}
162189

163190
{
164-
auto const id = thread.Push([&]() { ++value; });
165-
166-
TEST_EQUAL(id, ThreadPool::kImmediateMinId + 2, ());
191+
auto const pushResult = thread.Push([&]() { ++value; });
192+
TEST(pushResult.m_isSuccess, ());
193+
TEST_EQUAL(pushResult.m_id, ThreadPool::kImmediateMinId + 2, ());
167194
}
168195

169196
TEST(thread.Cancel(cancelTaskId), ());
@@ -184,23 +211,29 @@ UNIT_TEST(ThreadPoolDelayed_CancelDelayed)
184211
TaskLoop::TaskId cancelTaskId;
185212
ThreadPool thread;
186213
{
187-
auto const id = thread.Push([]() { testing::Wait(); });
188-
TEST_EQUAL(id, ThreadPool::kImmediateMinId, ());
214+
auto const pushResult = thread.Push([]() { testing::Wait(); });
215+
TEST(pushResult.m_isSuccess, ());
216+
TEST_EQUAL(pushResult.m_id, ThreadPool::kImmediateMinId, ());
189217
}
190218

191219
{
192-
auto const delayedId = thread.PushDelayed(milliseconds(1), [&value]() { ++value; });
193-
TEST_EQUAL(delayedId, ThreadPool::kDelayedMinId, ());
220+
auto const pushResult = thread.PushDelayed(milliseconds(1), [&value]() { ++value; });
221+
TEST(pushResult.m_isSuccess, ());
222+
TEST_EQUAL(pushResult.m_id, ThreadPool::kDelayedMinId, ());
194223
}
195224

196225
{
197-
cancelTaskId = thread.PushDelayed(milliseconds(2), [&]() { value += 1023; });
198-
TEST_EQUAL(cancelTaskId, ThreadPool::kDelayedMinId + 1, ());
226+
auto const pushResult = thread.PushDelayed(milliseconds(2), [&]() { value += 1023; });
227+
TEST(pushResult.m_isSuccess, ());
228+
TEST_EQUAL(pushResult.m_id, ThreadPool::kDelayedMinId + 1, ());
229+
230+
cancelTaskId = pushResult.m_id;
199231
}
200232

201233
{
202-
auto const delayedId = thread.PushDelayed(milliseconds(3), [&value]() { ++value; });
203-
TEST_EQUAL(delayedId, ThreadPool::kDelayedMinId + 2, ());
234+
auto const pushResult = thread.PushDelayed(milliseconds(3), [&value]() { ++value; });
235+
TEST(pushResult.m_isSuccess, ());
236+
TEST_EQUAL(pushResult.m_id, ThreadPool::kDelayedMinId + 2, ());
204237
}
205238

206239
TEST(thread.Cancel(cancelTaskId), ());

base/task_loop.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@ class TaskLoop
1313

1414
static TaskId constexpr kIncorrectId = 0;
1515

16+
struct PushResult
17+
{
18+
// Contains true when task is posted successfully.
19+
bool m_isSuccess = false;
20+
// Contains id of posted task.
21+
TaskId m_id = kIncorrectId;
22+
};
23+
1624
virtual ~TaskLoop() = default;
1725

18-
virtual TaskId Push(Task && task) = 0;
19-
virtual TaskId Push(Task const & task) = 0;
26+
virtual PushResult Push(Task && task) = 0;
27+
virtual PushResult Push(Task const & task) = 0;
2028
};
2129
} // namespace base

base/thread_pool_delayed.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,28 @@ ThreadPool::~ThreadPool()
3535
ShutdownAndJoin();
3636
}
3737

38-
TaskLoop::TaskId ThreadPool::Push(Task && t)
38+
TaskLoop::PushResult ThreadPool::Push(Task && t)
3939
{
4040
return AddImmediate(move(t));
4141
}
4242

43-
TaskLoop::TaskId ThreadPool::Push(Task const & t)
43+
TaskLoop::PushResult ThreadPool::Push(Task const & t)
4444
{
4545
return AddImmediate(t);
4646
}
4747

48-
TaskLoop::TaskId ThreadPool::PushDelayed(Duration const & delay, Task && t)
48+
TaskLoop::PushResult ThreadPool::PushDelayed(Duration const & delay, Task && t)
4949
{
5050
return AddDelayed(delay, move(t));
5151
}
5252

53-
TaskLoop::TaskId ThreadPool::PushDelayed(Duration const & delay, Task const & t)
53+
TaskLoop::PushResult ThreadPool::PushDelayed(Duration const & delay, Task const & t)
5454
{
5555
return AddDelayed(delay, t);
5656
}
5757

5858
template <typename T>
59-
TaskLoop::TaskId ThreadPool::AddImmediate(T && task)
59+
TaskLoop::PushResult ThreadPool::AddImmediate(T && task)
6060
{
6161
return AddTask([&]() {
6262
auto const newId = MakeNextId(m_immediateLastId, kImmediateMinId, kImmediateMaxId);
@@ -67,7 +67,7 @@ TaskLoop::TaskId ThreadPool::AddImmediate(T && task)
6767
}
6868

6969
template <typename T>
70-
TaskLoop::TaskId ThreadPool::AddDelayed(Duration const & delay, T && task)
70+
TaskLoop::PushResult ThreadPool::AddDelayed(Duration const & delay, T && task)
7171
{
7272
auto const when = Now() + delay;
7373
return AddTask([&]() {
@@ -79,15 +79,15 @@ TaskLoop::TaskId ThreadPool::AddDelayed(Duration const & delay, T && task)
7979
}
8080

8181
template <typename Add>
82-
TaskLoop::TaskId ThreadPool::AddTask(Add && add)
82+
TaskLoop::PushResult ThreadPool::AddTask(Add && add)
8383
{
8484
lock_guard<mutex> lk(m_mu);
8585
if (m_shutdown)
86-
return kIncorrectId;
86+
return {};
8787

8888
auto const newId = add();
8989
m_cv.notify_one();
90-
return newId;
90+
return {true, newId};
9191
}
9292

9393
void ThreadPool::ProcessTasks()

0 commit comments

Comments
 (0)