3 * Lazy initialization and cleanup utilities
5 * Authors: Ludovic Henry <ludovic@xamarin.com>
7 * Copyright 2015 Xamarin, Inc. (www.xamarin.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11 #ifndef __MONO_LAZY_INIT_H__
12 #define __MONO_LAZY_INIT_H__
19 #include "mono-threads.h"
20 #include "mono-memory-model.h"
23 * These functions should be used if you want some form of lazy initialization. You can have a look at the
24 * threadpool for a more detailed example.
26 * The idea is that a module can be in 5 different states:
27 * - not initialized: it is the first state it starts in
28 * - initializing/initialized: whenever we need this module for the first time, we need to initialize it: allocate
29 * memory, launch background thread, etc. To achieve this, we have a module specific function (let's call it
31 * - cleaning/cleaned: when we want to clean this module specific data up, then we need to clean it up: deallocate
32 * memory, wait for background threads to finish, etc. As for the initialization process, we need a module specific
33 * function (let's call it cleanup)
35 * The switch from one state to the other can only happen in the following ways:
37 * - not initialized -> initializing -> initialized
38 * - not initialized -> cleaned
39 * - not initialized -> initializing -> initialized -> cleaning -> cleaned
41 * The initialize and cleanup functions are guaranteed to:
42 * - be each called once and only once
43 * - not be called concurrently (either 2+ initialize or 2+ cleanup, either initialize and cleanup)
46 typedef gint32 mono_lazy_init_t;
49 MONO_LAZY_INIT_STATUS_NOT_INITIALIZED,
50 MONO_LAZY_INIT_STATUS_INITIALIZING,
51 MONO_LAZY_INIT_STATUS_INITIALIZED,
52 MONO_LAZY_INIT_STATUS_CLEANING,
53 MONO_LAZY_INIT_STATUS_CLEANED,
56 static inline gboolean
57 mono_lazy_initialize (mono_lazy_init_t *lazy_init, void (*initialize) (void))
65 if (status >= MONO_LAZY_INIT_STATUS_INITIALIZED)
66 return status == MONO_LAZY_INIT_STATUS_INITIALIZED;
67 if (status == MONO_LAZY_INIT_STATUS_INITIALIZING
68 || InterlockedCompareExchange (lazy_init, MONO_LAZY_INIT_STATUS_INITIALIZING, MONO_LAZY_INIT_STATUS_NOT_INITIALIZED)
69 != MONO_LAZY_INIT_STATUS_NOT_INITIALIZED
71 while (*lazy_init == MONO_LAZY_INIT_STATUS_INITIALIZING)
72 mono_thread_info_yield ();
73 g_assert (InterlockedRead (lazy_init) >= MONO_LAZY_INIT_STATUS_INITIALIZED);
74 return status == MONO_LAZY_INIT_STATUS_INITIALIZED;
79 mono_atomic_store_release (lazy_init, MONO_LAZY_INIT_STATUS_INITIALIZED);
84 mono_lazy_cleanup (mono_lazy_init_t *lazy_init, void (*cleanup) (void))
92 if (status == MONO_LAZY_INIT_STATUS_NOT_INITIALIZED
93 && InterlockedCompareExchange (lazy_init, MONO_LAZY_INIT_STATUS_CLEANED, MONO_LAZY_INIT_STATUS_NOT_INITIALIZED)
94 == MONO_LAZY_INIT_STATUS_NOT_INITIALIZED
98 if (status == MONO_LAZY_INIT_STATUS_INITIALIZING) {
99 while ((status = *lazy_init) == MONO_LAZY_INIT_STATUS_INITIALIZING)
100 mono_thread_info_yield ();
103 if (status == MONO_LAZY_INIT_STATUS_CLEANED)
105 if (status == MONO_LAZY_INIT_STATUS_CLEANING
106 || InterlockedCompareExchange (lazy_init, MONO_LAZY_INIT_STATUS_CLEANING, MONO_LAZY_INIT_STATUS_INITIALIZED)
107 != MONO_LAZY_INIT_STATUS_INITIALIZED
109 while (*lazy_init == MONO_LAZY_INIT_STATUS_CLEANING)
110 mono_thread_info_yield ();
111 g_assert (InterlockedRead (lazy_init) == MONO_LAZY_INIT_STATUS_CLEANED);
117 mono_atomic_store_release (lazy_init, MONO_LAZY_INIT_STATUS_CLEANED);
120 static inline gboolean
121 mono_lazy_is_initialized (mono_lazy_init_t *lazy_init)
123 g_assert (lazy_init);
124 return InterlockedRead (lazy_init) == MONO_LAZY_INIT_STATUS_INITIALIZED;