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);
72 deBool overIntervalLimit = secondsSinceLastTouch > dog->intervalTimeLimit;
73 deBool overTotalLimit = totalSecondsPassed > dog->totalTimeLimit;
75 if (overIntervalLimit || overTotalLimit)
77 qpTimeoutReason reason = overTotalLimit ? QP_TIMEOUT_REASON_TOTAL_LIMIT : QP_TIMEOUT_REASON_INTERVAL_LIMIT;
78 DBGPRINT(("watchDogThreadFunc(): call timeout func\n"));
79 dog->timeOutFunc(dog, dog->timeOutUserPtr, reason);
86 DBGPRINT(("watchDogThreadFunc(): stop\n"));
89 qpWatchDog* qpWatchDog_create (qpWatchDogFunc timeOutFunc, void* userPtr, int totalTimeLimitSecs, int intervalTimeLimitSecs)
91 /* Allocate & initialize. */
92 qpWatchDog* dog = (qpWatchDog*)deCalloc(sizeof(qpWatchDog));
96 DE_ASSERT(timeOutFunc);
97 DE_ASSERT((totalTimeLimitSecs > 0) && (intervalTimeLimitSecs > 0));
99 DBGPRINT(("qpWatchDog::create(%ds, %ds)\n", totalTimeLimitSecs, intervalTimeLimitSecs));
101 dog->timeOutFunc = timeOutFunc;
102 dog->timeOutUserPtr = userPtr;
103 dog->totalTimeLimit = totalTimeLimitSecs;
104 dog->intervalTimeLimit = intervalTimeLimitSecs;
106 /* Reset (sets time values). */
107 qpWatchDog_reset(dog);
109 /* Initialize watchdog thread. */
110 dog->status = STATUS_THREAD_RUNNING;
111 dog->watchDogThread = deThread_create(watchDogThreadFunc, dog, DE_NULL);
112 if (!dog->watchDogThread)
121 void qpWatchDog_reset (qpWatchDog* dog)
123 deUint64 curTime = deGetMicroseconds();
126 DBGPRINT(("qpWatchDog::reset()\n"));
128 dog->resetTime = curTime;
129 dog->lastTouchTime = curTime;
132 void qpWatchDog_destroy (qpWatchDog* dog)
135 DBGPRINT(("qpWatchDog::destroy()\n"));
137 /* Finish the watchdog thread. */
138 dog->status = STATUS_STOP_THREAD;
139 deThread_join(dog->watchDogThread);
140 deThread_destroy(dog->watchDogThread);
142 DBGPRINT(("qpWatchDog::destroy() finished\n"));
146 void qpWatchDog_touch (qpWatchDog* dog)
149 DBGPRINT(("qpWatchDog::touch()\n"));
150 dog->lastTouchTime = deGetMicroseconds();