Skip to content

Commit bad85d3

Browse files
gonnetxnnpack-bot
authored andcommitted
Add a pthreadpool_update_executor function that replaces the executor used by the pthreadpool if needed.
PiperOrigin-RevId: 811258607
1 parent a505b3b commit bad85d3

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

include/pthreadpool.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifndef __PTHREADPOOL_INCLUDE_PTHREADPOOL_H_
1111
#define __PTHREADPOOL_INCLUDE_PTHREADPOOL_H_
1212

13+
#include <stdbool.h>
1314
#include <stddef.h>
1415
#include <stdint.h>
1516

@@ -248,6 +249,26 @@ size_t pthreadpool_set_threads_count(pthreadpool_t threadpool,
248249
*/
249250
void pthreadpool_release_executor_threads(struct pthreadpool* threadpool);
250251

252+
/**
253+
* Updates a thread pool with a given @a pthreadpool_executor.
254+
*
255+
* @param threadpool The thread pool in which to replace the executor.
256+
* @param executor A pointer to a @a pthreadpool_executor object that
257+
* will be used to determine the number of extra
258+
* threads (plus the calling thread), and provide the
259+
* threads itself, for each call to a
260+
* `pthreadpool_parallelize_*` function.
261+
* @param executor_context A pointer to the context that will be passed to the
262+
* functions in the @a executor object.
263+
*
264+
* @return @c true if the @a executor was successfully swapped, and @c false if
265+
* it was not, e.g. because the current and nex @a executor and @a
266+
* executor_context are identical.
267+
*/
268+
bool pthreadpool_update_executor(pthreadpool_t threadpool,
269+
struct pthreadpool_executor* executor,
270+
void* executor_context);
271+
251272
/**
252273
* Process items on a 1D grid.
253274
*

src/pthreads.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,27 @@ void pthreadpool_release_executor_threads(struct pthreadpool* threadpool) {
778778
}
779779
}
780780

781+
bool pthreadpool_update_executor(pthreadpool_t threadpool,
782+
struct pthreadpool_executor* executor,
783+
void* executor_context) {
784+
/* Protect the global threadpool structures */
785+
pthreadpool_mutex_lock(&threadpool->execution_mutex);
786+
787+
const bool res = threadpool->executor.num_threads != executor->num_threads ||
788+
threadpool->executor.schedule != executor->schedule ||
789+
threadpool->executor_context != executor_context;
790+
if (res) {
791+
pthreadpool_release_executor_threads(threadpool);
792+
threadpool->executor = *executor;
793+
threadpool->executor_context = executor_context;
794+
}
795+
796+
/* Unprotect the global threadpool structures now that we're done. */
797+
pthreadpool_mutex_unlock(&threadpool->execution_mutex);
798+
799+
return res;
800+
}
801+
781802
void pthreadpool_destroy(struct pthreadpool* threadpool) {
782803
if (threadpool != NULL) {
783804
/* Tell all threads to stop. */

0 commit comments

Comments
 (0)