[sgen] Split up concurrent sweep from worker logic
[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 typedef struct _SgenThreadPoolJob SgenThreadPoolJob;
17 typedef struct _SgenThreadPool SgenThreadPool;
18 typedef struct _SgenThreadPoolData SgenThreadPoolData;
19
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*);
25
26 struct _SgenThreadPoolJob {
27         const char *name;
28         SgenThreadPoolJobFunc func;
29         size_t size;
30         volatile gint32 state;
31 };
32
33 #define MAX_NUM_THREADS 8
34
35 struct _SgenThreadPool {
36         mono_mutex_t lock;
37         mono_cond_t work_cond;
38         mono_cond_t done_cond;
39
40         int threads_num;
41         MonoNativeThreadId threads [MAX_NUM_THREADS];
42
43         /* Only accessed with the lock held. */
44         SgenPointerQueue job_queue;
45
46         SgenThreadPoolThreadInitFunc thread_init_func;
47         SgenThreadPoolIdleJobFunc idle_job_func;
48         SgenThreadPoolContinueIdleJobFunc continue_idle_job_func;
49         SgenThreadPoolShouldWorkFunc should_work_func;
50
51         volatile gboolean threadpool_shutdown;
52         volatile int threads_finished;
53 };
54
55 struct _SgenThreadPoolData {
56         SgenThreadPool *pool;
57 };
58
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);
60
61 void sgen_thread_pool_shutdown (SgenThreadPool *pool);
62
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);
66
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);
70
71 void sgen_thread_pool_idle_signal (SgenThreadPool *pool);
72 void sgen_thread_pool_idle_wait (SgenThreadPool *pool);
73
74 void sgen_thread_pool_wait_for_all_jobs (SgenThreadPool *pool);
75
76 int sgen_thread_pool_is_thread_pool_thread (SgenThreadPool *pool, MonoNativeThreadId thread);
77
78 #endif