Merge pull request #5714 from alexischr/update_bockbuild
[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 typedef gboolean (*SgenThreadPoolContinueIdleWaitFunc) (int, int*);
28
29 struct _SgenThreadPoolJob {
30         const char *name;
31         SgenThreadPoolJobFunc func;
32         size_t size;
33         volatile gint32 state;
34 };
35
36 struct _SgenThreadPoolContext {
37         /* Only accessed with the lock held. */
38         SgenPointerQueue job_queue;
39
40         SgenThreadPoolThreadInitFunc thread_init_func;
41         SgenThreadPoolIdleJobFunc idle_job_func;
42         SgenThreadPoolContinueIdleJobFunc continue_idle_job_func;
43         SgenThreadPoolShouldWorkFunc should_work_func;
44
45         void **thread_datas;
46         int num_threads;
47 };
48
49
50 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);
51 void sgen_thread_pool_start (void);
52
53 void sgen_thread_pool_shutdown (void);
54
55 SgenThreadPoolJob* sgen_thread_pool_job_alloc (const char *name, SgenThreadPoolJobFunc func, size_t size);
56 /* This only needs to be called on jobs that are not enqueued. */
57 void sgen_thread_pool_job_free (SgenThreadPoolJob *job);
58
59 void sgen_thread_pool_job_enqueue (int context_id, SgenThreadPoolJob *job);
60 /* This must only be called after the job has been enqueued. */
61 void sgen_thread_pool_job_wait (int context_id, SgenThreadPoolJob *job);
62
63 void sgen_thread_pool_idle_signal (int context_id);
64 void sgen_thread_pool_idle_wait (int context_id, SgenThreadPoolContinueIdleWaitFunc continue_wait);
65
66 void sgen_thread_pool_wait_for_all_jobs (int context_id);
67
68 int sgen_thread_pool_is_thread_pool_thread (MonoNativeThreadId thread);
69
70 #endif