summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Metzmacher <metze@samba.org>2018-06-22 00:49:33 +0200
committerStefan Metzmacher <metze@samba.org>2018-07-12 14:25:18 +0200
commit505d298e81570bb118a9b82e5166a8f11139750b (patch)
treed5c39eb756e430939e9c7cd728fd4aea354f52b3
parent76474a6fad43c791293f4fb30dc7c155619c5dec (diff)
downloadsamba-505d298e81570bb118a9b82e5166a8f11139750b.tar.gz
pthreadpool: add pthreadpool_max_threads() and pthreadpool_queued_jobs() helpers
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>
-rw-r--r--lib/pthreadpool/pthreadpool.c23
-rw-r--r--lib/pthreadpool/pthreadpool.h21
-rw-r--r--lib/pthreadpool/pthreadpool_sync.c10
3 files changed, 54 insertions, 0 deletions
diff --git a/lib/pthreadpool/pthreadpool.c b/lib/pthreadpool/pthreadpool.c
index 58ea857ded5..6c51bc5272b 100644
--- a/lib/pthreadpool/pthreadpool.c
+++ b/lib/pthreadpool/pthreadpool.c
@@ -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;
diff --git a/lib/pthreadpool/pthreadpool.h b/lib/pthreadpool/pthreadpool.h
index defbe5a9f62..cb8baffebb1 100644
--- a/lib/pthreadpool/pthreadpool.h
+++ b/lib/pthreadpool/pthreadpool.h
@@ -51,6 +51,27 @@ int pthreadpool_init(unsigned max_threads, struct pthreadpool **presult,
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
*
* Destroy a pthreadpool. If jobs are submitted, but not yet active in
diff --git a/lib/pthreadpool/pthreadpool_sync.c b/lib/pthreadpool/pthreadpool_sync.c
index d9a95f53c61..a7dce580951 100644
--- a/lib/pthreadpool/pthreadpool_sync.c
+++ b/lib/pthreadpool/pthreadpool_sync.c
@@ -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)
{