summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaciej Socha <maciej.socha@stericsson.com>2011-10-26 10:53:18 +0200
committerKalle Vahlman <kalle.vahlman@movial.com>2011-12-14 08:38:15 +0200
commit68299e69a811a4876bd27b807e22482330dcf6b0 (patch)
tree30bc3b6b4c30c6180385b815f832c2b6d3f9876b
parent604a2f1be71151d320d280898ddc138e73a2905a (diff)
Add timeout to the callback thread
The callback thread was polling the b2r2 device without an explicit timeout. The poll() function could thus block the thread indefinitely, even though the b2r2 device may have been closed. With an explicit timeout, control is guaranteed to return to the callback thread, allowing it to exit. Further, a pthread_detach() call was added to have the thread's context cleaned up, once the thread terminates. ST-Ericsson ID: 367329, 356025 ST-Ericsson FOSS-OUT ID: Trivial Change-Id: I789cda9c54ce4a1cd827ca8a0137ab2b5a01562e Reviewed-on: http://gerrit.lud.stericsson.com/gerrit/35313 Reviewed-by: Maciej SOCHA <maciej.socha@stericsson.com> Tested-by: Maciej SOCHA <maciej.socha@stericsson.com> Reviewed-by: QATOOLS Reviewed-by: QATEST Reviewed-by: Per PERSSON <per.xb.persson@stericsson.com> Reviewed-by: Jimmy RUBIN <jimmy.rubin@stericsson.com>
-rw-r--r--src/blt_b2r2.c21
1 files changed, 16 insertions, 5 deletions
diff --git a/src/blt_b2r2.c b/src/blt_b2r2.c
index 02f3bc1..cb01738 100644
--- a/src/blt_b2r2.c
+++ b/src/blt_b2r2.c
@@ -32,6 +32,7 @@
#else
# define LOGE(format) fprintf(stderr, LOG_TAG format "\n")
# define LOGE2(format, ...) fprintf(stderr, LOG_TAG format "\n", __VA_ARGS__)
+# define LOGI(format) printf(LOG_TAG format "\n")
# define LOGI2(format, ...) printf(LOG_TAG format "\n", __VA_ARGS__)
#endif
@@ -122,6 +123,12 @@ static void free_handle(int handle) {
static void *callback_thread_run(void *arg)
{
+ /*
+ * The resources consumed by this thread will be freed immediately when
+ * this thread is terminated
+ */
+ pthread_detach(pthread_self());
+
while (1) {
int result;
struct pollfd fds;
@@ -130,7 +137,7 @@ static void *callback_thread_run(void *arg)
fds.fd = (int)arg;
fds.events = POLLIN;
- result = poll(&fds, 1, -1);
+ result = poll(&fds, 1, 3000);
switch (result) {
case 0:
/* timeout occurred */
@@ -154,11 +161,15 @@ static void *callback_thread_run(void *arg)
void (*callback)(int, uint32_t) = (void*)report.report1;
callback(report.request_id, (uint32_t)report.report2);
}
+ } else if (fds.revents & POLLNVAL) {
+ /* fd not open, device must have been closed */
+ LOGI("Device closed. Callback thread terminated.\n");
+ pthread_exit(NULL);
} else {
- /* We assume that this is because the device was closed */
- LOGE2("Other event than POLLIN (%s)",
- strerror(errno));
- pthread_exit(NULL);
+ LOGE2("Unexpected event. Callback thread will exit. "
+ "errno=(%s) result=%d revents=0x%x",
+ strerror(errno), result, fds.revents);
+ pthread_exit(NULL);
}
break;
}