Changeset 754

Show
Ignore:
Timestamp:
06/23/08 20:40:22 (4 months ago)
Author:
Bartosz
Message:

added synchro.d to Windows makefile,
fixed an old build break,
added Mutex and trylock

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/phobos/std/c/windows/windows.d

    r748 r754  
    12101210void InitializeCriticalSection(CRITICAL_SECTION * lpCriticalSection); 
    12111211void EnterCriticalSection(CRITICAL_SECTION * lpCriticalSection); 
     1212BOOL TryEnterCriticalSection(CRITICAL_SECTION * lpCriticalSection); 
    12121213void LeaveCriticalSection(CRITICAL_SECTION * lpCriticalSection); 
    12131214void DeleteCriticalSection(CRITICAL_SECTION * lpCriticalSection); 
  • trunk/phobos/std/synchro.d

    r750 r754  
    11// Written in the D programming language 
     2// Put in the public domain by Bartosz Milewski 
    23 
    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/** 
     5Defines thread synchronization primitives. 
    246 
    25 /************************** 
    26  * The synchro module defines synchronization primitives. 
    27  * 
    28  * $(B CriticalSection) is an interprocess mutex 
    29  * Macros: 
    30  *      WIKI=Phobos/StdSynchro 
    31  */ 
     7Note: 
     8 
     9A lot of synchronization can be done using built-in monitors 
     10by defining $(D synchronized) methods.  
     11(every object is a potential monitor--behavior inherited from $(D Object).) 
     12Use std.synchro for those cases 
     13where simple monitors are not sufficient. 
     14 
     15Author: Bartosz Milewski 
     16Macros: 
     17      WIKI=Phobos/StdSynchro 
     18*/ 
    3219 
    3320module std.synchro; 
     
    3623 
    3724//debug=thread; 
     25 
     26/** 
     27Defines a common interface for lockable objects (e.g., mutexes). 
     28*/ 
     29interface Lockable 
     30{ 
     31    void lock(); 
     32    void unlock(); 
     33} 
     34 
     35/** 
     36Scoped object that locks a $(D Lockable) for the duration of a scope. 
     37 
     38Note: 
     39 
     40If the mutex is associated with an object, and the lock is taken for the  
     41whole scope of a method, it's probably better to declare the method sd $(D synchronized)  
     42and use the mutex that's built into every $(D Object). 
     43 
     44Example: 
     45---- 
     46import std.synchro; 
     47 
     48Mutex mtx; 
     49 
     50static this() 
     51{ 
     52    mtx = new Mutex; 
     53} 
     54 
     55void f() 
     56{ 
     57    scope lock = new Lock(mtx); 
     58    // access shared data 
     59    // end of scope: mutex released 
     60} 
     61---- 
     62*/ 
     63scope class Lock 
     64{ 
     65public: 
     66    this (Lockable mtx) 
     67    { 
     68        _mtx = mtx; 
     69        _mtx.lock (); 
     70    } 
     71    ~this () 
     72    { 
     73        _mtx.unlock (); 
     74    } 
     75private: 
     76    Lockable _mtx; 
     77} 
    3878 
    3979/* ================================ Windows ================================= */ 
     
    4484private import std.c.windows.windows; 
    4585 
    46 class CriticalSection 
     86/** 
     87Implements mutual exclusion. 
     88 
     89Note: On Windows, it's implemented as $(D CriticalSection); on Linux, using pthreads. 
     90 
     91Example: See the $(D Lock) example 
     92*/ 
     93class Mutex: Lockable 
    4794{ 
    4895public: 
    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    } 
    57116private: 
    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; 
    84118} 
    85119 
     
    87121 
    88122/* ================================ linux ================================= */ 
    89  
    90123version (linux) 
    91124{ 
    92125 
     126private import std.c.linux.linux; 
     127private import std.c.linux.linuxextern; 
     128 
     129class Mutex: Lockable 
     130{ 
     131public: 
     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    } 
     152private: 
     153    pthread_mutex_t _mtx; 
    93154} 
    94155 
     156} // linux 
     157 
     158version(unittest) 
     159{ 
     160    import std.synchro; 
     161    import std.thread; 
     162     
     163    Mutex mtx; 
     164    int glob; 
     165     
     166} 
     167 
     168unittest 
     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  
    9797    std\signals.d std\typetuple.d std\traits.d std\bind.d \ 
    9898    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 \ 
    100100    std\asserterror.d std\outofmemory.d std\system.d \ 
    101101    std\iterator.d std\encoding.d std\variant.d \ 
     
    195195    $(DOC)\std_stdint.html \ 
    196196    $(DOC)\std_stdio.html \ 
     197    $(DOC)\std_synchro.html \ 
    197198    $(DOC)\std_system.html \ 
    198199    $(DOC)\std_thread.html \ 
     
    217218    $(DOC)\std_c_string.html \ 
    218219    $(DOC)\std_c_time.html \ 
    219     $(DOC)\std_c_wcharh.html \ 
    220220    $(DOC)\phobos.html 
    221221 
     
    225225    std\gc.d std\math.d std\string.d std\path.d std\date.d \ 
    226226    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 \ 
    228228    std\asserterror.d std\dateparse.d std\outofmemory.d std\mmfile.d \ 
    229229    std\intrinsic.d std\array.d std\switcherr.d std\syserror.d \ 
     
    594594    $(DMD) -c $(DFLAGS) std\thread.d 
    595595 
     596synchro.obj : std\synchro.d 
     597    $(DMD) -c $(DFLAGS) std\synchro.d 
     598 
    596599traits.obj : std\traits.d 
    597600    $(DMD) -c $(DFLAGS) std\traits.d -oftraits.obj 
     
    933936    $(DMD) -c -o- $(DFLAGS) -Df$(DOC)\std_thread.html std.ddoc std\thread.d 
    934937 
     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 
    935941$(DOC)\std_traits.html : std.ddoc std\traits.d 
    936942    $(DMD) -c -o- $(DFLAGS) -Df$(DOC)\std_traits.html std.ddoc std\traits.d 
     
    10011007$(DOC)\std_c_time.html : std.ddoc std\c\time.d 
    10021008    $(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.d 
    1005     $(DMD) -c -o- $(DFLAGS) -Df$(DOC)\std_c_wcharh.html std.ddoc std\c\wcharh.d 
    1006  
    10071009 
    10081010######################################################