[sgen] Implement work context for thread pool threads
[mono.git] / mono / sgen / sgen-thread-pool.h
1 /**
2  * \file
3  * Threadpool for all concurrent GC work.
4  *
5  * Copyright (C) 2015 Xamarin Inc
6  *
7  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
8  */
9
10 #ifndef __MONO_SGEN_THREAD_POOL_H__
11 #define __MONO_SGEN_THREAD_POOL_H__
12
13 #include "mono/sgen/sgen-pointer-queue.h"
14 #include "mono/utils/mono-threads.h"
15
16 #define SGEN_THREADPOOL_MAX_NUM_THREADS 8
17 #define SGEN_THREADPOOL_MAX_NUM_CONTEXTS 3
18
19 typedef struct _SgenThreadPoolJob SgenThreadPoolJob;
20 typedef struct _SgenThreadPoolContext SgenThreadPoolContext;
21
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*);
27
28 struct _SgenThreadPoolJob {
29         const char *name;
30         SgenThreadPoolJobFunc func;
31         size_t size;
32         volatile gint32 state;
33 };
34
35 struct _SgenThreadPoolContext {
36         /* Only accessed with the lock held. */
37         SgenPointerQueue job_queue;
38
39         SgenThreadPoolThreadInitFunc thread_init_func;
40         SgenThreadPoolIdleJobFunc idle_job_func;
41         SgenThreadPoolContinueIdleJobFunc continue_idle_job_func;
42         SgenThreadPoolShouldWorkFunc should_work_func;
43
44         void **thread_datas;
45         int num_threads;
46 };
47
48
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);
51
52 void sgen_thread_pool_shutdown (void);
53
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);
57
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);
61
62 void sgen_thread_pool_idle_signal (int context_id);
63 void sgen_thread_pool_idle_wait (int context_id);
64
65 void sgen_thread_pool_wait_for_all_jobs (int context_id);
66
67 int sgen_thread_pool_is_thread_pool_thread (MonoNativeThreadId thread);
68
69 #endif