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)
9 #ifndef __MONO_LAZY_INIT_H__
10 #define __MONO_LAZY_INIT_H__
17 #include "mono-threads.h"
18 #include "mono-memory-model.h"
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.
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
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)
33 * The switch from one state to the other can only happen in the following ways:
35 * - not initialized -> initializing -> initialized
36 * - not initialized -> cleaned
37 * - not initialized -> initializing -> initialized -> cleaning -> cleaned
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)
44 typedef gint32 mono_lazy_init_t;
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,
55 mono_lazy_initialize (mono_lazy_init_t *lazy_init, void (*initialize) (void))
63 if (status >= MONO_LAZY_INIT_STATUS_INITIALIZED)
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
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);
77 mono_atomic_store_release (lazy_init, MONO_LAZY_INIT_STATUS_INITIALIZED);
81 mono_lazy_cleanup (mono_lazy_init_t *lazy_init, void (*cleanup) (void))
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
95 if (status == MONO_LAZY_INIT_STATUS_INITIALIZING) {
96 while ((status = *lazy_init) == MONO_LAZY_INIT_STATUS_INITIALIZING)
97 mono_thread_info_yield ();
100 if (status == MONO_LAZY_INIT_STATUS_CLEANED)
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
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);
114 mono_atomic_store_release (lazy_init, MONO_LAZY_INIT_STATUS_CLEANED);
117 static inline gboolean
118 mono_lazy_is_initialized (mono_lazy_init_t *lazy_init)
120 g_assert (lazy_init);
121 return InterlockedRead (lazy_init) == MONO_LAZY_INIT_STATUS_INITIALIZED;