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.
10 #ifndef __MONO_LAZY_INIT_H__
11 #define __MONO_LAZY_INIT_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-ms 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
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:
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
;
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
,
56 mono_lazy_initialize (mono_lazy_init_t
*lazy_init
, void (*initialize
) (void))
64 if (status
>= MONO_LAZY_INIT_STATUS_INITIALIZED
)
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
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
);
78 mono_atomic_store_release (lazy_init
, MONO_LAZY_INIT_STATUS_INITIALIZED
);
82 mono_lazy_cleanup (mono_lazy_init_t
*lazy_init
, void (*cleanup
) (void))
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
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
)
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
);
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
;