-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPlayerScheduler.cpp
More file actions
249 lines (229 loc) · 6.27 KB
/
PlayerScheduler.cpp
File metadata and controls
249 lines (229 loc) · 6.27 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2024 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file PlayerScheduler.cpp
* @brief Class to schedule commands for async execution
*/
#include "PlayerScheduler.h"
#include "PlayerLogManager.h"
/**
* @brief PlayerScheduler Constructor
*/
PlayerScheduler::PlayerScheduler() : mTaskQueue(), mQMutex(), mQCond(),
mSchedulerRunning(false), mSchedulerThread(), mExMutex(),
mExLock(mExMutex, std::defer_lock), mNextTaskId(PLAYER_SCHEDULER_ID_DEFAULT),
mCurrentTaskId(PLAYER_TASK_ID_INVALID), mLockOut(false)
{
}
/**
* @brief PlayerScheduler Destructor
*/
PlayerScheduler::~PlayerScheduler()
{
if (mSchedulerRunning)
{
StopScheduler();
}
}
static std::hash<std::thread::id> std_thread_hasher;
std::size_t GetPlayerThreadID( const std::thread &t )
{
return std_thread_hasher( t.get_id() );
}
/**
* @brief To start scheduler thread
*/
void PlayerScheduler::StartScheduler()
{
//Turn on thread for processing async operations
std::lock_guard<std::mutex>lock(mQMutex);
mSchedulerThread = std::thread(std::bind(&PlayerScheduler::ExecuteAsyncTask, this));
mSchedulerRunning = true;
MW_LOG_INFO("Thread created Async Worker [%zx]", GetPlayerThreadID(mSchedulerThread));
}
/**
* @brief To schedule a task to be executed later
*/
int PlayerScheduler::ScheduleTask(PlayerAsyncTaskObj obj)
{
int id = PLAYER_TASK_ID_INVALID;
if (mSchedulerRunning)
{
std::lock_guard<std::mutex>lock(mQMutex);
if (!mLockOut)
{
id = mNextTaskId++;
// Upper limit check
if (mNextTaskId >= PLAYER_SCHEDULER_ID_MAX_VALUE)
{
mNextTaskId = PLAYER_SCHEDULER_ID_DEFAULT;
}
obj.mId = id;
mTaskQueue.push_back(obj);
mQCond.notify_one();
}
else
{
// Operation is skipped here, this might happen due to race conditions during normal operation, hence setting as info log
MW_LOG_INFO("Warning: Attempting to schedule a task when scheduler is locked out, skipping operation %s!!", obj.mTaskName.c_str());
}
}
else
{
MW_LOG_ERR("Attempting to schedule a task when scheduler is not running, undefined behavior, task ignored:%s",obj.mTaskName.c_str());
}
return id;
}
/**
* @brief Executes scheduled tasks - invoked by thread
*/
void PlayerScheduler::ExecuteAsyncTask()
{
std::unique_lock<std::mutex>queueLock(mQMutex);
while (mSchedulerRunning)
{
if (mTaskQueue.empty())
{
mQCond.wait(queueLock);
}
else
{
/*
Take the execution lock before taking a task from the queue
otherwise this function could hold a task, out of the queue,
that cannot be deleted by RemoveAllTasks()!
Allow the queue to be modified while waiting.*/
queueLock.unlock();
std::lock_guard<std::mutex>executionLock(mExMutex);
queueLock.lock();
//note: mTaskQueue could have been modified while waiting for execute permission
if (!mTaskQueue.empty())
{
PlayerAsyncTaskObj obj = mTaskQueue.front();
mTaskQueue.pop_front();
if (obj.mId != PLAYER_TASK_ID_INVALID)
{
mCurrentTaskId = obj.mId;
MW_LOG_INFO("Found entry in function queue!!, task:%s. CurrentTaskId:%d ",obj.mTaskName.c_str(),mCurrentTaskId);
//Unlock so that new entries can be added to queue while function executes
queueLock.unlock();
MW_LOG_WARN("SchedulerTask Execution:%s taskId:%d",obj.mTaskName.c_str(),obj.mId);
//Execute function
obj.mTask(obj.mData);
//May be used in a wait() in future loops, it needs to be locked
queueLock.lock();
}
else
{
MW_LOG_INFO("Scheduler found a task with invalid ID, skip task!");
}
}
}
}
MW_LOG_INFO("Exited Async Worker Thread");
}
/**
* @brief To remove all scheduled tasks and prevent further tasks from scheduling
*/
void PlayerScheduler::RemoveAllTasks()
{
std::lock_guard<std::mutex>lock(mQMutex);
if(!mLockOut)
{
MW_LOG_WARN("The scheduler is active. An active task may continue to execute after this function exits. Call SuspendScheduler() prior to this function to prevent this.");
}
if (!mTaskQueue.empty())
{
MW_LOG_WARN("Clearing up %d entries from mFuncQueue", (int)mTaskQueue.size());
mTaskQueue.clear();
}
}
/**
* @brief To stop scheduler and associated resources
*/
void PlayerScheduler::StopScheduler()
{
MW_LOG_WARN("Stopping Async Worker Thread");
// Clean up things in queue
mSchedulerRunning = false;
//allow StopScheduler() to be called without warning from a nonsuspended state and
//not cause an error in ResumeScheduler() below due to trying to unlock an unlocked lock
if(!mLockOut)
{
SuspendScheduler();
}
RemoveAllTasks();
//prevent possible deadlock where mSchedulerThread is waiting for mExLock/mExMutex
ResumeScheduler();
mQCond.notify_one();
if (mSchedulerThread.joinable())
mSchedulerThread.join();
}
/**
* @brief To acquire execution lock for synchronisation purposes
*/
void PlayerScheduler::SuspendScheduler()
{
mExLock.lock();
std::lock_guard<std::mutex>lock(mQMutex);
mLockOut = true;
}
/**
* @brief To release execution lock
*/
void PlayerScheduler::ResumeScheduler()
{
mExLock.unlock();
std::lock_guard<std::mutex>lock(mQMutex);
mLockOut = false;
}
/**
* @brief To remove a scheduled tasks with ID
*/
bool PlayerScheduler::RemoveTask(int id)
{
bool ret = false;
std::lock_guard<std::mutex>lock(mQMutex);
// Make sure its not currently executing/executed task
if (id != PLAYER_TASK_ID_INVALID && mCurrentTaskId != id)
{
for (auto it = mTaskQueue.begin(); it != mTaskQueue.end(); )
{
if (it->mId == id)
{
mTaskQueue.erase(it);
ret = true;
break;
}
else
{
it++;
}
}
}
return ret;
}
/**
* @brief To enable scheduler to queue new tasks
*/
void PlayerScheduler::EnableScheduleTask()
{
std::lock_guard<std::mutex>lock(mQMutex);
mLockOut = false;
}