Changeset 763

Show
Ignore:
Timestamp:
06/25/08 13:04:13 (2 months ago)
Author:
Bartosz
Message:

Made pthread monitor reentrant

Files:

Legend:

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

    r760 r763  
    8585 
    8686/** 
    87 Implements mutual exclusion. 
     87Implements mutual exclusion. $(D Mutex) is re-entrant, i.e.,  
     88the same thread may lock it multiple times.  
     89It must unlock it the same number of times. 
    8890 
    8991Note: On Windows, it's implemented as $(D CriticalSection); on Linux, using pthreads. 
     
    110112        LeaveCriticalSection (&_critSection); 
    111113    } 
     114/** 
     115    Returns $(D true) if lock taken.  
     116*/ 
    112117    bool trylock() 
    113118    { 
     
    127132private import std.c.linux.linuxextern; 
    128133 
     134private pthread_mutexattr_t monitors_attr; 
     135 
     136// Revisit: This should be called before static constructors, in case they start a thread 
     137static this() 
     138{ 
     139    // Warning: this is non-portable 
     140    pthread_mutexattr_init(&_monitors_attr); 
     141    pthread_mutexattr_settype(&_monitors_attr, PTHREAD_MUTEX_RECURSIVE_NP); 
     142} 
     143 
     144static ~this() 
     145{ 
     146    pthread_mutexattr_destroy(&_monitors_attr); 
     147} 
     148 
    129149class Mutex: Lockable 
    130150{ 
     
    132152    this() 
    133153    { 
    134         pthread_mutex_init(&_mtx, null); 
     154        pthread_mutex_init(&_mtx, &monitors_attr); 
    135155    } 
    136156    ~this()