Browse Source

Send pthread_cancel to failed completion_timeout that has timed out.

Con Kolivas 12 years ago
parent
commit
3956382450
2 changed files with 8 additions and 6 deletions
  1. 7 4
      util.c
  2. 1 2
      util.h

+ 7 - 4
util.c

@@ -2410,7 +2410,7 @@ void _cgsem_wait(cgsem_t *cgsem, const char *file, const char *func, const int l
 		applog(LOG_WARNING, "Failed to read errno=%d" IN_FMT_FFL, errno, file, func, line);
 		applog(LOG_WARNING, "Failed to read errno=%d" IN_FMT_FFL, errno, file, func, line);
 }
 }
 
 
-void _cgsem_destroy(cgsem_t *cgsem)
+void cgsem_destroy(cgsem_t *cgsem)
 {
 {
 	close(cgsem->pipefd[1]);
 	close(cgsem->pipefd[1]);
 	close(cgsem->pipefd[0]);
 	close(cgsem->pipefd[0]);
@@ -2480,7 +2480,7 @@ int _cgsem_mswait(cgsem_t *cgsem, int ms, const char *file, const char *func, co
 	return 0;
 	return 0;
 }
 }
 
 
-void _cgsem_destroy(cgsem_t *cgsem)
+void cgsem_destroy(cgsem_t *cgsem)
 {
 {
 	sem_destroy(cgsem);
 	sem_destroy(cgsem);
 }
 }
@@ -2499,7 +2499,7 @@ void *completion_thread(void *arg)
 {
 {
 	struct cg_completion *cgc = (struct cg_completion *)arg;
 	struct cg_completion *cgc = (struct cg_completion *)arg;
 
 
-	pthread_detach(pthread_self());
+	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 	cgc->fn(cgc->fnarg);
 	cgc->fn(cgc->fnarg);
 	cgsem_post(&cgc->cgsem);
 	cgsem_post(&cgc->cgsem);
 
 
@@ -2522,7 +2522,10 @@ bool cg_completion_timeout(void *fn, void *fnarg, int timeout)
 	pthread_create(&pthread, NULL, completion_thread, (void *)cgc);
 	pthread_create(&pthread, NULL, completion_thread, (void *)cgc);
 
 
 	ret = cgsem_mswait(&cgc->cgsem, timeout);
 	ret = cgsem_mswait(&cgc->cgsem, timeout);
-	if (!ret)
+	if (!ret) {
+		pthread_join(pthread, NULL);
 		free(cgc);
 		free(cgc);
+	} else
+		pthread_cancel(pthread);
 	return !ret;
 	return !ret;
 }
 }

+ 1 - 2
util.h

@@ -133,14 +133,13 @@ void _cgsem_init(cgsem_t *cgsem, const char *file, const char *func, const int l
 void _cgsem_post(cgsem_t *cgsem, const char *file, const char *func, const int line);
 void _cgsem_post(cgsem_t *cgsem, const char *file, const char *func, const int line);
 void _cgsem_wait(cgsem_t *cgsem, const char *file, const char *func, const int line);
 void _cgsem_wait(cgsem_t *cgsem, const char *file, const char *func, const int line);
 int _cgsem_mswait(cgsem_t *cgsem, int ms, const char *file, const char *func, const int line);
 int _cgsem_mswait(cgsem_t *cgsem, int ms, const char *file, const char *func, const int line);
-void _cgsem_destroy(cgsem_t *cgsem);
+void cgsem_destroy(cgsem_t *cgsem);
 bool cg_completion_timeout(void *fn, void *fnarg, int timeout);
 bool cg_completion_timeout(void *fn, void *fnarg, int timeout);
 
 
 #define cgsem_init(_sem) _cgsem_init(_sem, __FILE__, __func__, __LINE__)
 #define cgsem_init(_sem) _cgsem_init(_sem, __FILE__, __func__, __LINE__)
 #define cgsem_post(_sem) _cgsem_post(_sem, __FILE__, __func__, __LINE__)
 #define cgsem_post(_sem) _cgsem_post(_sem, __FILE__, __func__, __LINE__)
 #define cgsem_wait(_sem) _cgsem_wait(_sem, __FILE__, __func__, __LINE__)
 #define cgsem_wait(_sem) _cgsem_wait(_sem, __FILE__, __func__, __LINE__)
 #define cgsem_mswait(_sem, _timeout) _cgsem_mswait(_sem, _timeout, __FILE__, __func__, __LINE__)
 #define cgsem_mswait(_sem, _timeout) _cgsem_mswait(_sem, _timeout, __FILE__, __func__, __LINE__)
-#define cgsem_destroy(_sem) _cgsem_destroy(_sem)
 
 
 /* Align a size_t to 4 byte boundaries for fussy arches */
 /* Align a size_t to 4 byte boundaries for fussy arches */
 static inline void align_len(size_t *len)
 static inline void align_len(size_t *len)