3 * Threadpool for all concurrent GC work.
5 * Copyright (C) 2015 Xamarin Inc
7 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
10 #ifndef __MONO_SGEN_THREAD_POOL_H__
11 #define __MONO_SGEN_THREAD_POOL_H__
13 #include "mono/sgen/sgen-pointer-queue.h"
14 #include "mono/utils/mono-threads.h"
16 typedef struct _SgenThreadPoolJob SgenThreadPoolJob;
17 typedef struct _SgenThreadPool SgenThreadPool;
18 typedef struct _SgenThreadPoolData SgenThreadPoolData;
20 typedef void (*SgenThreadPoolJobFunc) (void *thread_data, SgenThreadPoolJob *job);
21 typedef void (*SgenThreadPoolThreadInitFunc) (void*);
22 typedef void (*SgenThreadPoolIdleJobFunc) (void*);
23 typedef gboolean (*SgenThreadPoolContinueIdleJobFunc) (void*);
24 typedef gboolean (*SgenThreadPoolShouldWorkFunc) (void*);
26 struct _SgenThreadPoolJob {
28 SgenThreadPoolJobFunc func;
30 volatile gint32 state;
33 #define MAX_NUM_THREADS 8
35 struct _SgenThreadPool {
37 mono_cond_t work_cond;
38 mono_cond_t done_cond;
41 MonoNativeThreadId threads [MAX_NUM_THREADS];
43 /* Only accessed with the lock held. */
44 SgenPointerQueue job_queue;
46 SgenThreadPoolThreadInitFunc thread_init_func;
47 SgenThreadPoolIdleJobFunc idle_job_func;
48 SgenThreadPoolContinueIdleJobFunc continue_idle_job_func;
49 SgenThreadPoolShouldWorkFunc should_work_func;
51 volatile gboolean threadpool_shutdown;
52 volatile int threads_finished;
55 struct _SgenThreadPoolData {
59 void sgen_thread_pool_init (SgenThreadPool *pool, int num_threads, SgenThreadPoolThreadInitFunc init_func, SgenThreadPoolIdleJobFunc idle_func, SgenThreadPoolContinueIdleJobFunc continue_idle_func, SgenThreadPoolShouldWorkFunc should_work_func, SgenThreadPoolData **thread_datas);
61 void sgen_thread_pool_shutdown (SgenThreadPool *pool);
63 SgenThreadPoolJob* sgen_thread_pool_job_alloc (const char *name, SgenThreadPoolJobFunc func, size_t size);
64 /* This only needs to be called on jobs that are not enqueued. */
65 void sgen_thread_pool_job_free (SgenThreadPoolJob *job);
67 void sgen_thread_pool_job_enqueue (SgenThreadPool *pool, SgenThreadPoolJob *job);
68 /* This must only be called after the job has been enqueued. */
69 void sgen_thread_pool_job_wait (SgenThreadPool *pool, SgenThreadPoolJob *job);
71 void sgen_thread_pool_idle_signal (SgenThreadPool *pool);
72 void sgen_thread_pool_idle_wait (SgenThreadPool *pool);
74 void sgen_thread_pool_wait_for_all_jobs (SgenThreadPool *pool);
76 int sgen_thread_pool_is_thread_pool_thread (SgenThreadPool *pool, MonoNativeThreadId thread);