source: libcfa/src/concurrency/mutex_stmt.hfa @ 4e28d2e9

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 4e28d2e9 was 1b97976c, checked in by caparsons <caparson@…>, 3 years ago

added in file I forgot and added test expect for locks

  • Property mode set to 100644
File size: 1.1 KB
Line 
1#include "bits/algorithm.hfa"
2#include <assert.h>
3#include "invoke.h"
4#include "stdlib.hfa"
5#include <stdio.h>
6
7//-----------------------------------------------------------------------------
8// is_lock
9trait is_lock(L & | sized(L)) {
10        // For acquiring a lock
11        void lock( L & );
12
13        // For releasing a lock
14        void unlock( L & );
15};
16
17forall(L & | is_lock(L)) {
18
19    struct __mutex_stmt_lock_guard {
20        L ** lockarr;
21        __lock_size_t count;
22    };
23   
24    static inline void ?{}( __mutex_stmt_lock_guard(L) & this, L * lockarr [], __lock_size_t count  ) {
25        this.lockarr = lockarr;
26        this.count = count;
27
28        // Sort locks based on address
29        __libcfa_small_sort(this.lockarr, count);
30
31        // acquire locks in order
32        for ( size_t i = 0; i < count; i++ ) {
33            lock(*this.lockarr[i]);
34        }
35    }
36   
37    static inline void ^?{}( __mutex_stmt_lock_guard(L) & this ) with(this) {
38        for ( size_t i = count; i > 0; i-- ) {
39            unlock(*lockarr[i - 1]);
40        }
41    }
42
43    static inline L * __get_pointer( L & lock ) {
44        return &lock;
45    }
46}
Note: See TracBrowser for help on using the repository browser.