99631f08ef68c58f8b0120d0bd95d44b77360473
[mono.git] / mono / utils / mono-lazy-init.h
1 /*
2  * mono-lazy-init.h: Lazy initialization and cleanup utilities
3  *
4  * Authors: Ludovic Henry <ludovic@xamarin.com>
5  *
6  * Copyright 2015 Xamarin, Inc. (www.xamarin.com)
7  */
8
9 #ifndef __MONO_LAZY_INIT_H__
10 #define __MONO_LAZY_INIT_H__
11
12 #include <glib.h>
13
14 #include <config.h>
15
16 #include "atomic.h"
17 #include "mono-threads.h"
18 #include "mono-memory-model.h"
19
20 /*
21  * These functions should be used if you want some form of lazy initialization. You can have a look at the
22  * threadpool-ms for a more detailed example.
23  *
24  * The idea is that a module can be in 5 different states:
25  *  - not initialized: it is the first state it starts in
26  *  - initializing/initialized: whenever we need this module for the first time, we need to initialize it: allocate
27  *     memory, launch background thread, etc. To achieve this, we have a module specific function (let's call it
28  *     initialize)
29  *  - cleaning/cleaned: when we want to clean this module specific data up, then we need to clean it up: deallocate
30  *     memory, wait for background threads to finish, etc. As for the initialization process, we need a module specific
31  *     function (let's call it cleanup)
32  *
33  * The switch from one state to the other can only happen in the following ways:
34  *  - not initialized
35  *  - not initialized -> initializing -> initialized
36  *  - not initialized -> cleaned
37  *  - not initialized -> initializing -> initialized -> cleaning -> cleaned
38  *
39  * The initialize and cleanup functions are guaranteed to:
40  *  - be each called once and only once
41  *  - not be called concurrently (either 2+ initialize or 2+ cleanup, either initialize and cleanup)
42  */
43
44 typedef gint32 mono_lazy_init_t;
45
46 enum {
47         MONO_LAZY_INIT_STATUS_NOT_INITIALIZED,
48         MONO_LAZY_INIT_STATUS_INITIALIZING,
49         MONO_LAZY_INIT_STATUS_INITIALIZED,
50         MONO_LAZY_INIT_STATUS_CLEANING,
51         MONO_LAZY_INIT_STATUS_CLEANED,
52 };
53
54 static inline void
55 mono_lazy_initialize (mono_lazy_init_t *lazy_init, void (*initialize) (void))
56 {
57         gint32 status;
58
59         g_assert (lazy_init);
60
61         status = *lazy_init;
62
63         if (status >= MONO_LAZY_INIT_STATUS_INITIALIZED)
64                 return;
65         if (status == MONO_LAZY_INIT_STATUS_INITIALIZING
66              || InterlockedCompareExchange (lazy_init, MONO_LAZY_INIT_STATUS_INITIALIZING, MONO_LAZY_INIT_STATUS_NOT_INITIALIZED)
67                  != MONO_LAZY_INIT_STATUS_NOT_INITIALIZED
68         ) {
69                 while (*lazy_init == MONO_LAZY_INIT_STATUS_INITIALIZING)
70                         mono_thread_info_yield ();
71                 g_assert (InterlockedRead (lazy_init) >= MONO_LAZY_INIT_STATUS_INITIALIZED);
72                 return;
73         }
74
75         initialize ();
76
77         mono_atomic_store_release (lazy_init, MONO_LAZY_INIT_STATUS_INITIALIZED);
78 }
79
80 static inline void
81 mono_lazy_cleanup (mono_lazy_init_t *lazy_init, void (*cleanup) (void))
82 {
83         gint32 status;
84
85         g_assert (lazy_init);
86
87         status = *lazy_init;
88
89         if (status == MONO_LAZY_INIT_STATUS_NOT_INITIALIZED
90              && InterlockedCompareExchange (lazy_init, MONO_LAZY_INIT_STATUS_CLEANED, MONO_LAZY_INIT_STATUS_NOT_INITIALIZED)
91                  == MONO_LAZY_INIT_STATUS_NOT_INITIALIZED
92         ) {
93                 return;
94         }
95         if (status == MONO_LAZY_INIT_STATUS_INITIALIZING) {
96                 while ((status = *lazy_init) == MONO_LAZY_INIT_STATUS_INITIALIZING)
97                         mono_thread_info_yield ();
98         }
99
100         if (status == MONO_LAZY_INIT_STATUS_CLEANED)
101                 return;
102         if (status == MONO_LAZY_INIT_STATUS_CLEANING
103              || InterlockedCompareExchange (lazy_init, MONO_LAZY_INIT_STATUS_CLEANING, MONO_LAZY_INIT_STATUS_INITIALIZED)
104                  != MONO_LAZY_INIT_STATUS_INITIALIZED
105         ) {
106                 while (*lazy_init == MONO_LAZY_INIT_STATUS_CLEANING)
107                         mono_thread_info_yield ();
108                 g_assert (InterlockedRead (lazy_init) == MONO_LAZY_INIT_STATUS_CLEANED);
109                 return;
110         }
111
112         cleanup ();
113
114         mono_atomic_store_release (lazy_init, MONO_LAZY_INIT_STATUS_CLEANED);
115 }
116
117 static inline gboolean
118 mono_lazy_is_initialized (mono_lazy_init_t *lazy_init)
119 {
120         g_assert (lazy_init);
121         return InterlockedRead (lazy_init) == MONO_LAZY_INIT_STATUS_INITIALIZED;
122 }
123
124 #endif