[tools] Add nuget-hash-extractor tool to help produce the runtime ignored assemblies...
[mono-project.git] / mono / utils / mono-lazy-init.h
blob7deca1275f5f26f39602ad5404a5d061ef5cc94a
1 /*
2 * mono-lazy-init.h: Lazy initialization and cleanup utilities
4 * Authors: Ludovic Henry <ludovic@xamarin.com>
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 */
10 #ifndef __MONO_LAZY_INIT_H__
11 #define __MONO_LAZY_INIT_H__
13 #include <glib.h>
15 #include <config.h>
17 #include "atomic.h"
18 #include "mono-threads.h"
19 #include "mono-memory-model.h"
22 * These functions should be used if you want some form of lazy initialization. You can have a look at the
23 * threadpool for a more detailed example.
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)
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
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)
45 typedef gint32 mono_lazy_init_t;
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,
55 static inline void
56 mono_lazy_initialize (mono_lazy_init_t *lazy_init, void (*initialize) (void))
58 gint32 status;
60 g_assert (lazy_init);
62 status = *lazy_init;
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;
76 initialize ();
78 mono_atomic_store_release (lazy_init, MONO_LAZY_INIT_STATUS_INITIALIZED);
81 static inline void
82 mono_lazy_cleanup (mono_lazy_init_t *lazy_init, void (*cleanup) (void))
84 gint32 status;
86 g_assert (lazy_init);
88 status = *lazy_init;
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;
96 if (status == MONO_LAZY_INIT_STATUS_INITIALIZING) {
97 while ((status = *lazy_init) == MONO_LAZY_INIT_STATUS_INITIALIZING)
98 mono_thread_info_yield ();
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
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;
113 cleanup ();
115 mono_atomic_store_release (lazy_init, MONO_LAZY_INIT_STATUS_CLEANED);
118 static inline gboolean
119 mono_lazy_is_initialized (mono_lazy_init_t *lazy_init)
121 g_assert (lazy_init);
122 return InterlockedRead (lazy_init) == MONO_LAZY_INIT_STATUS_INITIALIZED;
125 #endif