-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAampScheduler.cpp
More file actions
271 lines (250 loc) · 6.98 KB
/
AampScheduler.cpp
File metadata and controls
271 lines (250 loc) · 6.98 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2020 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 AampScheduler.cpp
* @brief Class to schedule commands for async execution
*/
#include "AampScheduler.h"
#include "AampUtils.h"
/**
* @brief AampScheduler Constructor
*/
AampScheduler::AampScheduler() : mTaskQueue(), mQMutex(), mQCond(),
mSchedulerRunning(false), mSchedulerThread(), mExMutex(),
mExLock(mExMutex, std::defer_lock), mNextTaskId(AAMP_SCHEDULER_ID_DEFAULT),
mCurrentTaskId(AAMP_TASK_ID_INVALID), mLockOut(false), mState(eSTATE_IDLE),mPlayerId(-1)
{
}
/**
* @brief AampScheduler Destructor
*/
AampScheduler::~AampScheduler()
{
if (mSchedulerRunning)
{
StopScheduler();
}
}
/**
* @brief To start scheduler thread
*/
void AampScheduler::StartScheduler( int playerId )
{
mPlayerId = playerId;
//Turn on thread for processing async operations
std::lock_guard<std::mutex>lock(mQMutex);
mSchedulerThread = std::thread(std::bind(&AampScheduler::ExecuteAsyncTask, this));
mSchedulerRunning = true;
AAMPLOG_INFO("Thread created Async Worker [%zx]", GetPrintableThreadID(mSchedulerThread));
}
/**
* @brief To schedule a task to be executed later
*/
int AampScheduler::ScheduleTask(AsyncTaskObj obj)
{
int id = AAMP_TASK_ID_INVALID;
if (mSchedulerRunning)
{
auto currentState = mState.load();
if( currentState == eSTATE_ERROR || currentState == eSTATE_RELEASED)
return id;
std::lock_guard<std::mutex>lock(mQMutex);
if (!mLockOut)
{
id = mNextTaskId++;
// Upper limit check
if (mNextTaskId >= AAMP_SCHEDULER_ID_MAX_VALUE)
{
mNextTaskId = AAMP_SCHEDULER_ID_DEFAULT;
}
obj.mId = id;
if (obj.mTaskName == "SetRate")
{
// Remove any existing SetRate task from the queue
auto it = std::find_if(mTaskQueue.begin(), mTaskQueue.end(),
[](const AsyncTaskObj& obj) { return obj.mTaskName == "SetRate"; });
if (it != mTaskQueue.end()) {
AAMPLOG_INFO("Found queued SetRate task, removing old one. task: %s taskId:%d", it->mTaskName.c_str(), it->mId);
mTaskQueue.erase(it);
}
}
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
AAMPLOG_INFO("Warning: Attempting to schedule a task when scheduler is locked out, skipping operation %s!!", obj.mTaskName.c_str());
}
}
else
{
AAMPLOG_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 AampScheduler::ExecuteAsyncTask()
{
UsingPlayerId playerId( mPlayerId );
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();
//mTaskQueue could have been modified while waiting for execute permission
if (!mTaskQueue.empty())
{
AsyncTaskObj obj = mTaskQueue.front();
mTaskQueue.pop_front();
if (obj.mId != AAMP_TASK_ID_INVALID)
{
mCurrentTaskId = obj.mId;
auto currentState = mState.load();
AAMPLOG_INFO("Found entry in function queue!!, task:%s. State:%d: CurrentTaskId:%d ",obj.mTaskName.c_str(),currentState,mCurrentTaskId);
if( currentState != eSTATE_ERROR && currentState != eSTATE_RELEASED)
{
//Unlock so that new entries can be added to queue while function executes
queueLock.unlock();
AAMPLOG_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
{
AAMPLOG_ERR("Scheduler found a task with invalid ID, skip task!");
}
}
}
}
AAMPLOG_INFO("Exited Async Worker Thread");
}
/**
* @brief To remove all scheduled tasks and prevent further tasks from scheduling
*/
void AampScheduler::RemoveAllTasks()
{
std::lock_guard<std::mutex>lock(mQMutex);
if(!mLockOut)
{
AAMPLOG_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())
{
AAMPLOG_WARN("Clearing up %d entries from mFuncQueue", (int)mTaskQueue.size());
mTaskQueue.clear();
}
}
/**
* @brief To stop scheduler and associated resources
*/
void AampScheduler::StopScheduler()
{
AAMPLOG_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 synchronization purposes
*/
void AampScheduler::SuspendScheduler()
{
mExLock.lock();
std::lock_guard<std::mutex>lock(mQMutex);
mLockOut = true;
}
/**
* @brief To release execution lock
*/
void AampScheduler::ResumeScheduler()
{
mExLock.unlock();
std::lock_guard<std::mutex>lock(mQMutex);
mLockOut = false;
}
/**
* @brief To remove a scheduled tasks with ID
*/
bool AampScheduler::RemoveTask(int id)
{
bool ret = false;
std::lock_guard<std::mutex>lock(mQMutex);
// Make sure its not currently executing/executed task
if (id != AAMP_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 AampScheduler::EnableScheduleTask()
{
std::lock_guard<std::mutex>lock(mQMutex);
mLockOut = false;
}
/**
* @brief To player state to Scheduler
*/
void AampScheduler::SetState(AAMPPlayerState sstate)
{
mState.store(sstate);
}