1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Helper Library
3 * -------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Watch dog for detecting timeouts
22 *//*--------------------------------------------------------------------*/
24 #include "qpWatchDog.h"
33 # define DBGPRINT(X) qpPrintf X
40 STATUS_THREAD_RUNNING = 0,
48 qpWatchDogFunc timeOutFunc;
50 int totalTimeLimit; /* Total test case time limit in seconds */
51 int intervalTimeLimit; /* Iteration length limit in seconds */
53 volatile deUint64 resetTime;
54 volatile deUint64 lastTouchTime;
56 deThread watchDogThread;
57 volatile Status status;
60 static void watchDogThreadFunc (void* arg)
62 qpWatchDog* dog = (qpWatchDog*)arg;
65 DBGPRINT(("watchDogThreadFunc(): start\n"));
67 while (dog->status == STATUS_THREAD_RUNNING)
69 deUint64 curTime = deGetMicroseconds();
70 int totalSecondsPassed = (int)((curTime - dog->resetTime) / 1000000ull);
71 int secondsSinceLastTouch = (int)((curTime - dog->lastTouchTime) / 1000000ull);
73 if ((secondsSinceLastTouch > dog->intervalTimeLimit) || (totalSecondsPassed > dog->totalTimeLimit))
75 DBGPRINT(("watchDogThreadFunc(): call timeout func\n"));
76 dog->timeOutFunc(dog, dog->timeOutUserPtr);
83 DBGPRINT(("watchDogThreadFunc(): stop\n"));
86 qpWatchDog* qpWatchDog_create (qpWatchDogFunc timeOutFunc, void* userPtr, int totalTimeLimitSecs, int intervalTimeLimitSecs)
88 /* Allocate & initialize. */
89 qpWatchDog* dog = (qpWatchDog*)deCalloc(sizeof(qpWatchDog));
93 DE_ASSERT(timeOutFunc);
94 DE_ASSERT((totalTimeLimitSecs > 0) && (intervalTimeLimitSecs > 0));
96 DBGPRINT(("qpWatchDog::create(%ds, %ds)\n", totalTimeLimitSecs, intervalTimeLimitSecs));
98 dog->timeOutFunc = timeOutFunc;
99 dog->timeOutUserPtr = userPtr;
100 dog->totalTimeLimit = totalTimeLimitSecs;
101 dog->intervalTimeLimit = intervalTimeLimitSecs;
103 /* Reset (sets time values). */
104 qpWatchDog_reset(dog);
106 /* Initialize watchdog thread. */
107 dog->status = STATUS_THREAD_RUNNING;
108 dog->watchDogThread = deThread_create(watchDogThreadFunc, dog, DE_NULL);
109 if (!dog->watchDogThread)
118 void qpWatchDog_reset (qpWatchDog* dog)
120 deUint64 curTime = deGetMicroseconds();
123 DBGPRINT(("qpWatchDog::reset()\n"));
125 dog->resetTime = curTime;
126 dog->lastTouchTime = curTime;
129 void qpWatchDog_destroy (qpWatchDog* dog)
132 DBGPRINT(("qpWatchDog::destroy()\n"));
134 /* Finish the watchdog thread. */
135 dog->status = STATUS_STOP_THREAD;
136 deThread_join(dog->watchDogThread);
137 deThread_destroy(dog->watchDogThread);
139 DBGPRINT(("qpWatchDog::destroy() finished\n"));
143 void qpWatchDog_touch (qpWatchDog* dog)
146 DBGPRINT(("qpWatchDog::touch()\n"));
147 dog->lastTouchTime = deGetMicroseconds();