pthreadpool: add pthreadpool_max_threads() and pthreadpool_queued_jobs() helpers
authorStefan Metzmacher <metze@samba.org>
Thu, 21 Jun 2018 22:49:33 +0000 (00:49 +0200)
committerStefan Metzmacher <metze@samba.org>
Thu, 12 Jul 2018 12:25:18 +0000 (14:25 +0200)
These can be used to implement some kind of flow control in the caller.
E.g. unless pthreadpool_queued_jobs() is lower than
pthreadpool_max_threads() is good to prepare new jobs.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
lib/pthreadpool/pthreadpool.c
lib/pthreadpool/pthreadpool.h
lib/pthreadpool/pthreadpool_sync.c

index 58ea857ded5b0244472e96fac58d0ed2e6d43ff2..6c51bc5272b391559a8cbf6b78c5197c8c2c7d71 100644 (file)
@@ -196,6 +196,29 @@ int pthreadpool_init(unsigned max_threads, struct pthreadpool **presult,
        return 0;
 }
 
+size_t pthreadpool_max_threads(struct pthreadpool *pool)
+{
+       return pool->max_threads;
+}
+
+size_t pthreadpool_queued_jobs(struct pthreadpool *pool)
+{
+       int res;
+       int unlock_res;
+       size_t ret;
+
+       res = pthread_mutex_lock(&pool->mutex);
+       if (res != 0) {
+               return 0;
+       }
+
+       ret = pool->num_jobs;
+
+       unlock_res = pthread_mutex_unlock(&pool->mutex);
+       assert(unlock_res == 0);
+       return ret;
+}
+
 static void pthreadpool_prepare_pool(struct pthreadpool *pool)
 {
        int ret;
index defbe5a9f6238fdfc42ccc36e307701a3ddf1f1a..cb8baffebb1825f78cc226fe3a3f19d5e61b1547 100644 (file)
@@ -50,6 +50,27 @@ int pthreadpool_init(unsigned max_threads, struct pthreadpool **presult,
                                      void *private_data),
                     void *signal_fn_private_data);
 
+/**
+ * @brief Get the max threads value of pthreadpool
+ *
+ * @note This can be 0 for strict sync processing.
+ *
+ * @param[in]  pool            The pool
+ * @return                     number of possible threads
+ */
+size_t pthreadpool_max_threads(struct pthreadpool *pool);
+
+/**
+ * @brief The number of queued jobs of pthreadpool
+ *
+ * This is the number of jobs added by pthreadpool_add_job(),
+ * which are not yet processed by a thread.
+ *
+ * @param[in]  pool            The pool
+ * @return                     The number of jobs
+ */
+size_t pthreadpool_queued_jobs(struct pthreadpool *pool);
+
 /**
  * @brief Destroy a pthreadpool
  *
index d9a95f53c611b763eef00e320a2ea3a7bd1552f6..a7dce580951b8f5a92cf26948a7027d4427144cc 100644 (file)
@@ -52,6 +52,16 @@ int pthreadpool_init(unsigned max_threads, struct pthreadpool **presult,
        return 0;
 }
 
+size_t pthreadpool_max_threads(struct pthreadpool *pool)
+{
+       return 0;
+}
+
+size_t pthreadpool_queued_jobs(struct pthreadpool *pool)
+{
+       return 0;
+}
+
 int pthreadpool_add_job(struct pthreadpool *pool, int job_id,
                        void (*fn)(void *private_data), void *private_data)
 {