Changeset 754
- Timestamp:
- 06/23/08 20:40:22 (4 months ago)
- Files:
-
- trunk/phobos/std/c/windows/windows.d (modified) (1 diff)
- trunk/phobos/std/synchro.d (modified) (4 diffs)
- trunk/phobos/win32.mak (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/phobos/std/c/windows/windows.d
r748 r754 1210 1210 void InitializeCriticalSection(CRITICAL_SECTION * lpCriticalSection); 1211 1211 void EnterCriticalSection(CRITICAL_SECTION * lpCriticalSection); 1212 BOOL TryEnterCriticalSection(CRITICAL_SECTION * lpCriticalSection); 1212 1213 void LeaveCriticalSection(CRITICAL_SECTION * lpCriticalSection); 1213 1214 void DeleteCriticalSection(CRITICAL_SECTION * lpCriticalSection); trunk/phobos/std/synchro.d
r750 r754 1 1 // Written in the D programming language 2 // Put in the public domain by Bartosz Milewski 2 3 3 /* 4 * Copyright (C) 2002-2008 by Digital Mars, www.digitalmars.com 5 * Written by Bartosz Milewski 6 * 7 * This software is provided 'as-is', without any express or implied 8 * warranty. In no event will the authors be held liable for any damages 9 * arising from the use of this software. 10 * 11 * Permission is granted to anyone to use this software for any purpose, 12 * including commercial applications, and to alter it and redistribute it 13 * freely, subject to the following restrictions: 14 * 15 * o The origin of this software must not be misrepresented; you must not 16 * claim that you wrote the original software. If you use this software 17 * in a product, an acknowledgment in the product documentation would be 18 * appreciated but is not required. 19 * o Altered source versions must be plainly marked as such, and must not 20 * be misrepresented as being the original software. 21 * o This notice may not be removed or altered from any source 22 * distribution. 23 */ 4 /** 5 Defines thread synchronization primitives. 24 6 25 /************************** 26 * The synchro module defines synchronization primitives. 27 * 28 * $(B CriticalSection) is an interprocess mutex 29 * Macros: 30 * WIKI=Phobos/StdSynchro 31 */ 7 Note: 8 9 A lot of synchronization can be done using built-in monitors 10 by defining $(D synchronized) methods. 11 (every object is a potential monitor--behavior inherited from $(D Object).) 12 Use std.synchro for those cases 13 where simple monitors are not sufficient. 14 15 Author: Bartosz Milewski 16 Macros: 17 WIKI=Phobos/StdSynchro 18 */ 32 19 33 20 module std.synchro; … … 36 23 37 24 //debug=thread; 25 26 /** 27 Defines a common interface for lockable objects (e.g., mutexes). 28 */ 29 interface Lockable 30 { 31 void lock(); 32 void unlock(); 33 } 34 35 /** 36 Scoped object that locks a $(D Lockable) for the duration of a scope. 37 38 Note: 39 40 If the mutex is associated with an object, and the lock is taken for the 41 whole scope of a method, it's probably better to declare the method sd $(D synchronized) 42 and use the mutex that's built into every $(D Object). 43 44 Example: 45 ---- 46 import std.synchro; 47 48 Mutex mtx; 49 50 static this() 51 { 52 mtx = new Mutex; 53 } 54 55 void f() 56 { 57 scope lock = new Lock(mtx); 58 // access shared data 59 // end of scope: mutex released 60 } 61 ---- 62 */ 63 scope class Lock 64 { 65 public: 66 this (Lockable mtx) 67 { 68 _mtx = mtx; 69 _mtx.lock (); 70 } 71 ~this () 72 { 73 _mtx.unlock (); 74 } 75 private: 76 Lockable _mtx; 77 } 38 78 39 79 /* ================================ Windows ================================= */ … … 44 84 private import std.c.windows.windows; 45 85 46 class CriticalSection 86 /** 87 Implements mutual exclusion. 88 89 Note: On Windows, it's implemented as $(D CriticalSection); on Linux, using pthreads. 90 91 Example: See the $(D Lock) example 92 */ 93 class Mutex: Lockable 47 94 { 48 95 public: 49 this () 50 { 51 InitializeCriticalSection (&_critSection); 52 } 53 ~this () 54 { 55 DeleteCriticalSection (&_critSection); 56 } 96 this() 97 { 98 InitializeCriticalSection (&_critSection); 99 } 100 ~this() 101 { 102 DeleteCriticalSection (&_critSection); 103 } 104 override void lock() 105 { 106 EnterCriticalSection (&_critSection); 107 } 108 override void unlock() 109 { 110 LeaveCriticalSection (&_critSection); 111 } 112 bool trylock() 113 { 114 return TryEnterCriticalSection(&_critSection) != 0; // lock taken 115 } 57 116 private: 58 void lock () 59 { 60 EnterCriticalSection (&_critSection); 61 } 62 void unlock () 63 { 64 LeaveCriticalSection (&_critSection); 65 } 66 private: 67 CRITICAL_SECTION _critSection; 68 } 69 70 scope class Lock 71 { 72 public: 73 this (CriticalSection critSect) 74 { 75 _critSect = critSect; 76 _critSect.lock (); 77 } 78 ~this () 79 { 80 _critSect.unlock (); 81 } 82 private: 83 CriticalSection _critSect; 117 CRITICAL_SECTION _critSection; 84 118 } 85 119 … … 87 121 88 122 /* ================================ linux ================================= */ 89 90 123 version (linux) 91 124 { 92 125 126 private import std.c.linux.linux; 127 private import std.c.linux.linuxextern; 128 129 class Mutex: Lockable 130 { 131 public: 132 this() 133 { 134 pthread_mutex_init(&_mtx, null); 135 } 136 ~this() 137 { 138 pthread_mutex_destroy(&_mtx); 139 } 140 override void lock() 141 { 142 pthread_mutex_lock(&_mtx); 143 } 144 override void unlock() 145 { 146 pthread_mutex_unlock(&_mtx); 147 } 148 override bool trylock() 149 { 150 return pthread_mutex_trylock(&_mtx) == 0; // lock taken 151 } 152 private: 153 pthread_mutex_t _mtx; 93 154 } 94 155 156 } // linux 157 158 version(unittest) 159 { 160 import std.synchro; 161 import std.thread; 162 163 Mutex mtx; 164 int glob; 165 166 } 167 168 unittest 169 { 170 void inc_glob_twice() 171 { 172 scope lock = new Lock(mtx); 173 174 assert(glob % 2 == 0); 175 glob++; 176 glob++; 177 } 178 179 mtx = new Mutex; 180 181 int f() 182 { 183 for (int i = 0; i < 1000; ++i) 184 inc_glob_twice(); 185 return 0; 186 } 187 188 auto thr1 = new Thread(&f); 189 auto thr2 = new Thread(&f); 190 thr1.start; 191 thr2.start; 192 thr1.wait; 193 thr2.wait; 194 } 195 trunk/phobos/win32.mak
r752 r754 97 97 std\signals.d std\typetuple.d std\traits.d std\bind.d \ 98 98 std\bitmanip.d std\typecons.d std\switcherr.d \ 99 std\thread.d std\ moduleinit.d std\boxer.d \99 std\thread.d std\synchro.d std\moduleinit.d std\boxer.d \ 100 100 std\asserterror.d std\outofmemory.d std\system.d \ 101 101 std\iterator.d std\encoding.d std\variant.d \ … … 195 195 $(DOC)\std_stdint.html \ 196 196 $(DOC)\std_stdio.html \ 197 $(DOC)\std_synchro.html \ 197 198 $(DOC)\std_system.html \ 198 199 $(DOC)\std_thread.html \ … … 217 218 $(DOC)\std_c_string.html \ 218 219 $(DOC)\std_c_time.html \ 219 $(DOC)\std_c_wcharh.html \220 220 $(DOC)\phobos.html 221 221 … … 225 225 std\gc.d std\math.d std\string.d std\path.d std\date.d \ 226 226 std\ctype.d std\file.d std\compiler.d std\system.d std\moduleinit.d \ 227 std\outbuffer.d std\thread.d std\ md5.d std\base64.d \227 std\outbuffer.d std\thread.d std\synchro.d std\md5.d std\base64.d \ 228 228 std\asserterror.d std\dateparse.d std\outofmemory.d std\mmfile.d \ 229 229 std\intrinsic.d std\array.d std\switcherr.d std\syserror.d \ … … 594 594 $(DMD) -c $(DFLAGS) std\thread.d 595 595 596 synchro.obj : std\synchro.d 597 $(DMD) -c $(DFLAGS) std\synchro.d 598 596 599 traits.obj : std\traits.d 597 600 $(DMD) -c $(DFLAGS) std\traits.d -oftraits.obj … … 933 936 $(DMD) -c -o- $(DFLAGS) -Df$(DOC)\std_thread.html std.ddoc std\thread.d 934 937 938 $(DOC)\std_synchro.html : std.ddoc std\synchro.d 939 $(DMD) -c -o- $(DFLAGS) -Df$(DOC)\std_synchro.html std.ddoc std\synchro.d 940 935 941 $(DOC)\std_traits.html : std.ddoc std\traits.d 936 942 $(DMD) -c -o- $(DFLAGS) -Df$(DOC)\std_traits.html std.ddoc std\traits.d … … 1001 1007 $(DOC)\std_c_time.html : std.ddoc std\c\time.d 1002 1008 $(DMD) -c -o- $(DFLAGS) -Df$(DOC)\std_c_time.html std.ddoc std\c\time.d 1003 1004 $(DOC)\std_c_wcharh.html : std.ddoc std\c\wcharh.d1005 $(DMD) -c -o- $(DFLAGS) -Df$(DOC)\std_c_wcharh.html std.ddoc std\c\wcharh.d1006 1007 1009 1008 1010 ######################################################
