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 #define SGEN_THREADPOOL_MAX_NUM_THREADS 8
17 #define SGEN_THREADPOOL_MAX_NUM_CONTEXTS 3
19 typedef struct _SgenThreadPoolJob SgenThreadPoolJob;
20 typedef struct _SgenThreadPoolContext SgenThreadPoolContext;
22 typedef void (*SgenThreadPoolJobFunc) (void *thread_data, SgenThreadPoolJob *job);
23 typedef void (*SgenThreadPoolThreadInitFunc) (void*);
24 typedef void (*SgenThreadPoolIdleJobFunc) (void*);
25 typedef gboolean (*SgenThreadPoolContinueIdleJobFunc) (void*, int);
26 typedef gboolean (*SgenThreadPoolShouldWorkFunc) (void*);
28 struct _SgenThreadPoolJob {
30 SgenThreadPoolJobFunc func;
32 volatile gint32 state;
35 struct _SgenThreadPoolContext {
36 /* Only accessed with the lock held. */
37 SgenPointerQueue job_queue;
39 SgenThreadPoolThreadInitFunc thread_init_func;
40 SgenThreadPoolIdleJobFunc idle_job_func;
41 SgenThreadPoolContinueIdleJobFunc continue_idle_job_func;
42 SgenThreadPoolShouldWorkFunc should_work_func;
49 int sgen_thread_pool_create_context (int num_threads, SgenThreadPoolThreadInitFunc init_func, SgenThreadPoolIdleJobFunc idle_func, SgenThreadPoolContinueIdleJobFunc continue_idle_func, SgenThreadPoolShouldWorkFunc should_work_func, void **thread_datas);
50 void sgen_thread_pool_start (void);
52 void sgen_thread_pool_shutdown (void);
54 SgenThreadPoolJob* sgen_thread_pool_job_alloc (const char *name, SgenThreadPoolJobFunc func, size_t size);
55 /* This only needs to be called on jobs that are not enqueued. */
56 void sgen_thread_pool_job_free (SgenThreadPoolJob *job);
58 void sgen_thread_pool_job_enqueue (int context_id, SgenThreadPoolJob *job);
59 /* This must only be called after the job has been enqueued. */
60 void sgen_thread_pool_job_wait (int context_id, SgenThreadPoolJob *job);
62 void sgen_thread_pool_idle_signal (int context_id);
63 void sgen_thread_pool_idle_wait (int context_id);
65 void sgen_thread_pool_wait_for_all_jobs (int context_id);
67 int sgen_thread_pool_is_thread_pool_thread (MonoNativeThreadId thread);