source: appstream-generator/build/girepo/glib/Mutex.d @ 4841

Last change on this file since 4841 was 4841, checked in by Juanma, 2 years ago

Initial release

File size: 5.1 KB
Line 
1/*
2 * Licensed under the GNU Lesser General Public License Version 3
3 *
4 * This library is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the license, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18// generated automatically - do not change
19
20
21module glib.Mutex;
22
23private import gi.glib;
24public  import gi.glibtypes;
25
26
27/**
28 * The #GMutex struct is an opaque data structure to represent a mutex
29 * (mutual exclusion). It can be used to protect data against shared
30 * access.
31 *
32 * Take for example the following function:
33 * |[<!-- language="C" -->
34 * int
35 * give_me_next_number (void)
36 * {
37 * static int current_number = 0;
38 *
39 * // now do a very complicated calculation to calculate the new
40 * // number, this might for example be a random number generator
41 * current_number = calc_next_number (current_number);
42 *
43 * return current_number;
44 * }
45 * ]|
46 * It is easy to see that this won't work in a multi-threaded
47 * application. There current_number must be protected against shared
48 * access. A #GMutex can be used as a solution to this problem:
49 * |[<!-- language="C" -->
50 * int
51 * give_me_next_number (void)
52 * {
53 * static GMutex mutex;
54 * static int current_number = 0;
55 * int ret_val;
56 *
57 * g_mutex_lock (&mutex);
58 * ret_val = current_number = calc_next_number (current_number);
59 * g_mutex_unlock (&mutex);
60 *
61 * return ret_val;
62 * }
63 * ]|
64 * Notice that the #GMutex is not initialised to any particular value.
65 * Its placement in static storage ensures that it will be initialised
66 * to all-zeros, which is appropriate.
67 *
68 * If a #GMutex is placed in other contexts (eg: embedded in a struct)
69 * then it must be explicitly initialised using g_mutex_init().
70 *
71 * A #GMutex should only be accessed via g_mutex_ functions.
72 */
73public class Mutex
74{
75        /** the main Gtk struct */
76        protected GMutex* gMutex;
77        protected bool ownedRef;
78
79        /** Get the main Gtk struct */
80        public GMutex* getMutexStruct()
81        {
82                return gMutex;
83        }
84
85        /** the main Gtk struct as a void* */
86        protected void* getStruct()
87        {
88                return cast(void*)gMutex;
89        }
90
91        /**
92         * Sets our main struct and passes it to the parent class.
93         */
94        public this (GMutex* gMutex, bool ownedRef = false)
95        {
96                this.gMutex = gMutex;
97                this.ownedRef = ownedRef;
98        }
99
100
101        /**
102         * Frees the resources allocated to a mutex with g_mutex_init().
103         *
104         * This function should not be used with a #GMutex that has been
105         * statically allocated.
106         *
107         * Calling g_mutex_clear() on a locked mutex leads to undefined
108         * behaviour.
109         *
110         * Sine: 2.32
111         */
112        public void clear()
113        {
114                g_mutex_clear(gMutex);
115        }
116
117        /**
118         * Initializes a #GMutex so that it can be used.
119         *
120         * This function is useful to initialize a mutex that has been
121         * allocated on the stack, or as part of a larger structure.
122         * It is not necessary to initialize a mutex that has been
123         * statically allocated.
124         *
125         * |[<!-- language="C" -->
126         * typedef struct {
127         * GMutex m;
128         * ...
129         * } Blob;
130         *
131         * Blob *b;
132         *
133         * b = g_new (Blob, 1);
134         * g_mutex_init (&b->m);
135         * ]|
136         *
137         * To undo the effect of g_mutex_init() when a mutex is no longer
138         * needed, use g_mutex_clear().
139         *
140         * Calling g_mutex_init() on an already initialized #GMutex leads
141         * to undefined behaviour.
142         *
143         * Since: 2.32
144         */
145        public void init()
146        {
147                g_mutex_init(gMutex);
148        }
149
150        /**
151         * Locks @mutex. If @mutex is already locked by another thread, the
152         * current thread will block until @mutex is unlocked by the other
153         * thread.
154         *
155         * #GMutex is neither guaranteed to be recursive nor to be
156         * non-recursive.  As such, calling g_mutex_lock() on a #GMutex that has
157         * already been locked by the same thread results in undefined behaviour
158         * (including but not limited to deadlocks).
159         */
160        public void lock()
161        {
162                g_mutex_lock(gMutex);
163        }
164
165        /**
166         * Tries to lock @mutex. If @mutex is already locked by another thread,
167         * it immediately returns %FALSE. Otherwise it locks @mutex and returns
168         * %TRUE.
169         *
170         * #GMutex is neither guaranteed to be recursive nor to be
171         * non-recursive.  As such, calling g_mutex_lock() on a #GMutex that has
172         * already been locked by the same thread results in undefined behaviour
173         * (including but not limited to deadlocks or arbitrary return values).
174         *
175         * Returns: %TRUE if @mutex could be locked
176         */
177        public bool trylock()
178        {
179                return g_mutex_trylock(gMutex) != 0;
180        }
181
182        /**
183         * Unlocks @mutex. If another thread is blocked in a g_mutex_lock()
184         * call for @mutex, it will become unblocked and can lock @mutex itself.
185         *
186         * Calling g_mutex_unlock() on a mutex that is not locked by the
187         * current thread leads to undefined behaviour.
188         */
189        public void unlock()
190        {
191                g_mutex_unlock(gMutex);
192        }
193}
Note: See TracBrowser for help on using the repository browser.