1 /*
2 Copyright (c) 2021 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 /**
30  * Cross-platform thread synchronization primitive
31  *
32  * Copyright: Timur Gafarov 2021.
33  * License: $(LINK2 https://boost.org/LICENSE_1_0.txt, Boost License 1.0).
34  * Authors: Timur Gafarov
35  */
36 module dlib.core.mutex;
37 
38 version(Windows)
39 {
40     import core.sys.windows.winbase;
41 }
42 else version(Posix)
43 {
44     import core.sys.posix.pthread;
45 }
46 
47 /**
48  * Mutex structure. 
49  * Uses CRITICAL_SECTION under Windows, pthread_mutex_t under Posix
50  */
51 struct Mutex
52 {
53     version(Windows)
54     {
55         CRITICAL_SECTION _mutex;
56     }
57     else version(Posix)
58     {
59         pthread_mutex_t _mutex;
60     }
61     
62     /// Initialize the mutex
63     int init()
64     {
65         version(Windows)
66         {
67             InitializeCriticalSection(&_mutex);
68             return 0;
69         }
70         else version(Posix)
71         {
72             return pthread_mutex_init(&_mutex, null);
73         }
74         else return 0;
75     }
76     
77     /// Enter critical section. If the mutex is already locked, block the thread until it becomes available
78     int lock()
79     {
80         version(Windows)
81         {
82             EnterCriticalSection(&_mutex);
83             return 0;
84         }
85         else version(Posix)
86         {
87             return pthread_mutex_lock(&_mutex);
88         }
89         else return 0;
90     }
91     
92     /// Try to enter critical section. Return immediately if the mutex is already locked
93     int tryLock()
94     {
95         version(Windows)
96         {
97             return !TryEnterCriticalSection(&_mutex);
98         }
99         else version(Posix)
100         {
101             return pthread_mutex_trylock(&_mutex);
102         }
103         else return 0;
104     }
105     
106     /// Leave critical section
107     int unlock()
108     {
109         version(Windows)
110         {
111             LeaveCriticalSection(&_mutex);
112             return 0;
113         }
114         else version(Posix)
115         {
116             return pthread_mutex_unlock(&_mutex);
117         }
118         else return 0;
119     }
120     
121     /// Destroy the mutex
122     int destroy()
123     {
124         version(Windows)
125         {
126             DeleteCriticalSection(&_mutex);
127             return 0;
128         }
129         else version(Posix)
130         {
131             return pthread_mutex_destroy(&_mutex);
132         }
133         else return 0;
134     }
135 }
136 
137 ///
138 unittest
139 {
140     import dlib.core.memory;
141     import dlib.core.thread;
142     
143     int x = 0;
144     Mutex mutex;
145     mutex.init();
146     
147     void add()
148     {
149         mutex.lock();
150         int local = x;
151         local = local + 1;
152         x = local;
153         mutex.unlock();
154     }
155     
156     void sub()
157     {
158         mutex.lock();
159         int local = x;
160         local = local - 1;
161         x = local;
162         mutex.unlock();
163     }
164     
165     Thread[100] threads;
166     
167     foreach(i; 0..threads.length/2)
168     {
169         threads[i] = New!Thread(&add);
170         threads[i].start();
171     }
172     
173     foreach(i; threads.length/2..threads.length)
174     {
175         threads[i] = New!Thread(&sub);
176         threads[i].start();
177     }
178     
179     foreach(t; threads)
180     {
181         t.join();
182     }
183     
184     assert(x == 0);
185 }