This repository has been archived by the owner on Jan 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
AampScheduler.cpp
353 lines (320 loc) · 8.71 KB
/
AampScheduler.cpp
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* 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"
/**
* @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),
mLogObj(NULL),mState(eSTATE_IDLE)
{
}
/**
* @brief AampScheduler Destructor
*/
AampScheduler::~AampScheduler()
{
if (mSchedulerRunning)
{
StopScheduler();
}
}
/**
* @brief To start scheduler thread
*/
void AampScheduler::StartScheduler()
{
//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_WARN("Started Async Worker Thread");
}
/**
* @brief To schedule a task to be executed later
*/
int AampScheduler::ScheduleTask(AsyncTaskObj obj)
{
int id = AAMP_TASK_ID_INVALID;
if (mSchedulerRunning)
{
if( mState == eSTATE_ERROR || mState == 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;
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()
{
std::unique_lock<std::mutex>queueLock(mQMutex);
while (mSchedulerRunning)
{
if (mTaskQueue.empty())
{
mQCond.wait(queueLock);
}
else
{
/* DELIA-57121
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();
//DELIA-57121 - note: 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;
AAMPLOG_INFO("Found entry in function queue!!, task:%s. State:%d",obj.mTaskName.c_str(),mState);
if( mState != eSTATE_ERROR && mState != eSTATE_RELEASED)
{
//Unlock so that new entries can be added to queue while function executes
queueLock.unlock();
AAMPLOG_WARN("SchedulerTask Execution:%s",obj.mTaskName.c_str());
//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 %zu entries from mFuncQueue", 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;
//DELIA-57121 allow StopScheduler() to be called without warning from a nonsuspended state and
//DELIA-57122 not cause an error in ResumeScheduler() below due to trying to unlock an unlocked lock
if(!mLockOut)
{
SuspendScheduler();
}
RemoveAllTasks();
//DELIA-57122 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 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(PrivAAMPState sstate)
{
mState = sstate;
}
#ifdef __UNIT_TESTING__
void printTask(const char *str)
{
AAMPLOG_WARN("Inside task function :%s\n",str);
int seconds = rand() % 10 + 1;
sleep(seconds);
}
void AddTask(AampScheduler &mScheduler, std::string str )
{
mScheduler.ScheduleTask(AsyncTaskObj([str](void *data)
{
printTask(str.c_str());
}, (void *) nullptr, __FUNCTION__));
}
void task1(void *inst) {
std::string input="Task1";
AampScheduler *mScheduler = (AampScheduler *)inst;
for(int i=0;i<10;i++)
{
mScheduler->ScheduleTask(AsyncTaskObj([input](void *data)
{
printTask(input.c_str());
}, (void *) nullptr, __FUNCTION__));
int state = rand() % 13 + 1;
mScheduler->SetState((PrivAAMPState)state);
AAMPLOG_WARN("setting state:%d",state);
}
}
void task2(void *inst) {
std::string input="Task2";
AampScheduler *mScheduler = (AampScheduler *)inst;
mScheduler->ScheduleTask(AsyncTaskObj([input](void *data)
{
printTask(input.c_str());
}, (void *) nullptr, __FUNCTION__));
}
int main()
{
AAMPLOG_WARN("Testing the Player Scheduler Class\n");
AampScheduler mScheduler;
srand(time(0));
//Test 1: Simple start / stop scheduler
//mScheduler.StartScheduler();
//mScheduler.StopScheduler();
// Test 2: start but no stop
//mScheduler.StartScheduler();
// Test 3 :
//mScheduler.StartScheduler();
//AddTask(mScheduler, "Test1");
//sleep(10);
//mScheduler.StopScheduler();
// Test 4 :
//mScheduler.StartScheduler();
//mScheduler.SetState(eSTATE_RELEASED);
//AddTask(mScheduler, "Test2");
//sleep(10);
//mScheduler.StopScheduler();
// Test 5 :
//mScheduler.StartScheduler();
//AddTask(mScheduler, "Test2");
//mScheduler.SetState(eSTATE_RELEASED);
//sleep(10);
//mScheduler.StopScheduler();
// Test 6: Stop without start
//mScheduler.StopScheduler();
// Test 7: Other API calls with start Scheduler
//for(int i=0;i<10;i++)
//{
// AddTask(mScheduler, "LoopTest");
// mScheduler.RemoveAllTasks();
// mScheduler.SuspendScheduler();
// mScheduler.ResumeScheduler();
//}
// Test 8 :
mScheduler.StartScheduler();
std::thread t1(task1,(void *)&mScheduler);
std::thread t2(task2,(void *)&mScheduler);
sleep(10);
t1.join();
t2.join();
mScheduler.StopScheduler();
return 0;
}
#endif