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