Provide a helper function that can reset cgsems to zero.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
diff --git a/util.c b/util.c
index 0e9670c..4e292d4 100644
--- a/util.c
+++ b/util.c
@@ -2601,6 +2601,25 @@ int _cgsem_mswait(cgsem_t *cgsem, int ms, const char *file, const char *func, co
/* We don't reach here */
return 0;
}
+
+/* Reset semaphore count back to zero */
+void cgsem_reset(cgsem_t *cgsem)
+{
+ int ret, fd;
+ fd_set rd;
+ char buf;
+
+ fd = cgsem->pipefd[0];
+ FD_ZERO(&rd);
+ FD_SET(fd, &rd);
+ do {
+ struct timeval timeout = {0, 0};
+
+ ret = select(fd + 1, &rd, NULL, NULL, &timeout);
+ if (ret > 0)
+ ret = read(fd, &buf, 1);
+ } while (ret > 0);
+}
#else
void _cgsem_init(cgsem_t *cgsem, const char *file, const char *func, const int line)
{
@@ -2641,6 +2660,15 @@ int _cgsem_mswait(cgsem_t *cgsem, int ms, const char *file, const char *func, co
return 0;
}
+void cgsem_reset(cgsem_t *cgsem)
+{
+ int ret;
+
+ do {
+ ret = sem_trywait(cgsem);
+ } while (!ret);
+}
+
void cgsem_destroy(cgsem_t *cgsem)
{
sem_destroy(cgsem);
diff --git a/util.h b/util.h
index d359656..90b1ca9 100644
--- a/util.h
+++ b/util.h
@@ -138,6 +138,7 @@ 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_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);
+void cgsem_reset(cgsem_t *cgsem);
void cgsem_destroy(cgsem_t *cgsem);
bool cg_completion_timeout(void *fn, void *fnarg, int timeout);