From 68299e69a811a4876bd27b807e22482330dcf6b0 Mon Sep 17 00:00:00 2001 From: Maciej Socha Date: Wed, 26 Oct 2011 10:53:18 +0200 Subject: 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 Tested-by: Maciej SOCHA Reviewed-by: QATOOLS Reviewed-by: QATEST Reviewed-by: Per PERSSON Reviewed-by: Jimmy RUBIN --- src/blt_b2r2.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/blt_b2r2.c') 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; } -- cgit v1.2.3