1 /* 2 DSFML - The Simple and Fast Multimedia Library for D 3 4 Copyright (c) 2013 - 2015 Jeremy DeHaan (dehaan.jeremiah@gmail.com) 5 6 This software is provided 'as-is', without any express or implied warranty. 7 In no event will the authors be held liable for any damages arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, including commercial applications, 10 and to alter it and redistribute it freely, subject to the following restrictions: 11 12 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. 13 If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 14 15 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 16 17 3. This notice may not be removed or altered from any source distribution 18 */ 19 20 ///A module containing the Mutex class used by DSFML. 21 module dsfml.system.mutex; 22 23 import core = core.sync.mutex; 24 25 /** 26 *Blocks concurrent access to shared resources from multiple threads. 27 * 28 *Mutex stands for "MUTual EXclusion". 29 * 30 *A mutex is a synchronization object, used when multiple threads are involved. 31 * 32 *When you want to protect a part of the code from being accessed simultaneously by multiple threads, you typically use a mutex. 33 *When a thread is locked by a mutex, any other thread trying to lock it will be blocked until the mutex is released by the thread that locked it. 34 *This way, you can allow only one thread at a time to access a critical region of your code. 35 */ 36 class Mutex 37 { 38 private core.Mutex m_mutex; 39 40 ///Default Constructor 41 this() 42 { 43 m_mutex = new core.Mutex(); 44 } 45 46 //Destructor 47 ~this() 48 { 49 import dsfml.system.config; 50 mixin(destructorOutput); 51 } 52 53 ///Lock the mutex 54 /// 55 ///If the mutex is already locked in another thread, this call will block the execution until the mutex is released. 56 void lock() 57 { 58 m_mutex.lock(); 59 } 60 61 //Unlock the mutex 62 void unlock() 63 { 64 m_mutex.unlock(); 65 } 66 } 67 68 69 70 71 unittest 72 { 73 version(DSFML_Unittest_System) 74 { 75 import dsfml.system.thread; 76 import dsfml.system.sleep; 77 import core.time; 78 import std.stdio; 79 80 auto mutex = new Mutex(); 81 82 void secondThreadHello() 83 { 84 mutex.lock(); 85 for(int i = 0; i < 10; ++i) 86 { 87 writeln("Hello from the second thread!"); 88 } 89 mutex.unlock(); 90 } 91 92 93 writeln("Unit test for Mutex class"); 94 writeln(); 95 96 writeln("Locking a mutex and then unlocking it later."); 97 98 auto secondThread = new Thread(&secondThreadHello); 99 100 secondThread.launch(); 101 102 mutex.lock(); 103 104 for(int i = 0; i < 10; ++i) 105 { 106 writeln("Hello from the main thread!"); 107 } 108 109 mutex.unlock(); 110 sleep(seconds(1));//let's this unit test finish before moving on to the next one. 111 writeln(); 112 } 113 }