source: libcfa/src/concurrency/monitor.hfa @ db62eef

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since db62eef was ac2b598, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Changed descriptors for concurrency to use $ prefix instead of trailing _desc

  • Property mode set to 100644
File size: 3.7 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// monitor --
8//
9// Author           : Thierry Delisle
10// Created On       : Thd Feb 23 12:27:26 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Dec  4 07:55:32 2019
13// Update Count     : 11
14//
15
16#pragma once
17
18#include <stddef.h>
19
20#include <assert.h>
21#include "invoke.h"
22#include "stdlib.hfa"
23
24trait is_monitor(dtype T) {
25        $monitor * get_monitor( T & );
26        void ^?{}( T & mutex );
27};
28
29static inline void ?{}($monitor & this) with( this ) {
30        lock{};
31        entry_queue{};
32        signal_stack{};
33        owner         = 0p;
34        recursion     = 0;
35        mask.accepted = 0p;
36        mask.data     = 0p;
37        mask.size     = 0;
38        dtor_node     = 0p;
39}
40
41static inline void ^?{}($monitor & ) {}
42
43struct monitor_guard_t {
44        $monitor **     m;
45        __lock_size_t           count;
46        __monitor_group_t prev;
47};
48
49void ?{}( monitor_guard_t & this, $monitor ** m, __lock_size_t count, void (*func)() );
50void ^?{}( monitor_guard_t & this );
51
52struct monitor_dtor_guard_t {
53        $monitor *    m;
54        __monitor_group_t prev;
55};
56
57void ?{}( monitor_dtor_guard_t & this, $monitor ** m, void (*func)() );
58void ^?{}( monitor_dtor_guard_t & this );
59
60static inline forall( dtype T | sized(T) | { void ^?{}( T & mutex ); } )
61void delete( T * th ) {
62        ^(*th){};
63        free( th );
64}
65
66//-----------------------------------------------------------------------------
67// Internal scheduling
68
69struct __condition_criterion_t {
70        // Whether or not the criterion is met (True if met)
71        bool ready;
72
73        // The monitor this criterion concerns
74        $monitor * target;
75
76        // The parent node to which this criterion belongs
77        struct __condition_node_t * owner;
78
79        // Intrusive linked list Next field
80        __condition_criterion_t * next;
81};
82
83static inline __condition_criterion_t * & get_next( __condition_criterion_t & this ) {
84        return this.next;
85}
86
87struct __condition_node_t {
88        // Thread that needs to be woken when all criteria are met
89        $thread * waiting_thread;
90
91        // Array of criteria (Criterions are contiguous in memory)
92        __condition_criterion_t * criteria;
93
94        // Number of criterions in the criteria
95        __lock_size_t count;
96
97        // Intrusive linked list Next field
98        __condition_node_t * next;
99
100        // Custom user info accessible before signalling
101        uintptr_t user_info;
102};
103
104static inline __condition_node_t * & get_next( __condition_node_t & this ) {
105        return this.next;
106}
107
108void ?{}(__condition_node_t & this, $thread * waiting_thread, __lock_size_t count, uintptr_t user_info );
109void ?{}(__condition_criterion_t & this );
110void ?{}(__condition_criterion_t & this, $monitor * target, __condition_node_t * owner );
111
112struct condition {
113        // Link list which contains the blocked threads as-well as the information needed to unblock them
114        __queue_t(__condition_node_t) blocked;
115
116        // Array of monitor pointers (Monitors are NOT contiguous in memory)
117        $monitor ** monitors;
118
119        // Number of monitors in the array
120        __lock_size_t monitor_count;
121};
122
123static inline void ?{}( condition & this ) {
124        this.monitors = 0p;
125        this.monitor_count = 0;
126}
127
128static inline void ^?{}( condition & this ) {
129        free( this.monitors );
130}
131
132              void wait        ( condition & this, uintptr_t user_info = 0 );
133              bool signal      ( condition & this );
134              bool signal_block( condition & this );
135static inline bool is_empty    ( condition & this ) { return this.blocked.head == 1p; }
136         uintptr_t front       ( condition & this );
137
138//-----------------------------------------------------------------------------
139// External scheduling
140
141struct __acceptable_t {
142        inline struct __monitor_group_t;
143        bool is_dtor;
144};
145
146void __waitfor_internal( const __waitfor_mask_t & mask, int duration );
147
148// Local Variables: //
149// mode: c //
150// tab-width: 4 //
151// End: //
Note: See TracBrowser for help on using the repository browser.