source: libcfa/src/concurrency/mutex_stmt.hfa @ c1d8cde

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since c1d8cde was c1d8cde, checked in by caparsons <caparson@…>, 2 years ago

added scoped locking

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[1b97976c]1#include "bits/algorithm.hfa"
[eb9c2dc]2#include "bits/defs.hfa"
[1b97976c]3
4//-----------------------------------------------------------------------------
5// is_lock
6trait is_lock(L & | sized(L)) {
7        // For acquiring a lock
8        void lock( L & );
9
10        // For releasing a lock
11        void unlock( L & );
12};
13
14forall(L & | is_lock(L)) {
15
16    struct __mutex_stmt_lock_guard {
17        L ** lockarr;
18        __lock_size_t count;
19    };
20   
21    static inline void ?{}( __mutex_stmt_lock_guard(L) & this, L * lockarr [], __lock_size_t count  ) {
22        this.lockarr = lockarr;
23        this.count = count;
24
25        // Sort locks based on address
26        __libcfa_small_sort(this.lockarr, count);
27
28        // acquire locks in order
29        for ( size_t i = 0; i < count; i++ ) {
30            lock(*this.lockarr[i]);
31        }
32    }
33   
34    static inline void ^?{}( __mutex_stmt_lock_guard(L) & this ) with(this) {
35        for ( size_t i = count; i > 0; i-- ) {
36            unlock(*lockarr[i - 1]);
37        }
38    }
[a8367eb]39
[c1d8cde]40    struct scoped_lock {
41        L * internal_lock;
42    };
43
44    static inline void ?{}( scoped_lock(L) & this, L & internal_lock ) {
45        this.internal_lock = &internal_lock;
46        lock(internal_lock);
47    }
48   
49    static inline void ^?{}( scoped_lock(L) & this ) with(this) {
50        unlock(*internal_lock);
51    }
52
[a8367eb]53    static inline L * __get_ptr( L & this ) {
54        return &this;
55    }
56
57    static inline L __get_type( L & this );
58
59    static inline L __get_type( L * this );
[1b97976c]60}
Note: See TracBrowser for help on using the repository browser.