source: libcfa/src/concurrency/monitor.hfa @ 8f2f185

ADTast-experimentalenumpthread-emulationqualifiedEnum
Last change on this file since 8f2f185 was a1574e2, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Forgot this was needed for the non-VLA commit.

  • Property mode set to 100644
File size: 4.4 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(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, monitor$ ** m, __lock_size_t count );
51void ^?{}( monitor_guard_t & this );
52
53struct monitor_dtor_guard_t {
54        monitor$ *    m;
55        __monitor_group_t prev;
56        bool join;
57};
58
59void ?{}( monitor_dtor_guard_t & this, monitor$ ** m, void (*func)(), bool join );
60void ^?{}( monitor_dtor_guard_t & this );
61
62static inline forall( T & | sized(T) | { void ^?{}( T & mutex ); } )
63void delete( T * th ) {
64        if(th) ^(*th){};
65        free( th );
66}
67
68static inline forall( T & | sized(T) | { void ^?{}( T & mutex ); } )
69void adelete( T arr[] ) {
70        if ( arr ) {                                                                            // ignore null
71                size_t dim = malloc_size( arr ) / sizeof( T );
72                for ( int i = dim - 1; i >= 0; i -= 1 ) {               // reverse allocation order, must be unsigned
73                        ^(arr[i]){};                                                            // run destructor
74                } // for
75                free( arr );
76        } // if
77} // adelete
78
79//-----------------------------------------------------------------------------
80// Internal scheduling
81
82struct __condition_criterion_t {
83        // Whether or not the criterion is met (True if met)
84        bool ready;
85
86        // The monitor this criterion concerns
87        monitor$ * target;
88
89        // The parent node to which this criterion belongs
90        struct __condition_node_t * owner;
91
92        // Intrusive linked list Next field
93        __condition_criterion_t * next;
94};
95
96static inline __condition_criterion_t * & get_next( __condition_criterion_t & this ) {
97        return this.next;
98}
99
100struct __condition_node_t {
101        // Thread that needs to be woken when all criteria are met
102        thread$ * waiting_thread;
103
104        // Array of criteria (Criterions are contiguous in memory)
105        __condition_criterion_t * criteria;
106
107        // Number of criterions in the criteria
108        __lock_size_t count;
109
110        // Intrusive linked list Next field
111        __condition_node_t * next;
112
113        // Custom user info accessible before signalling
114        uintptr_t user_info;
115};
116
117static inline __condition_node_t * & get_next( __condition_node_t & this ) {
118        return this.next;
119}
120
121void ?{}(__condition_node_t & this, thread$ * waiting_thread, __lock_size_t count, uintptr_t user_info );
122void ?{}(__condition_criterion_t & this );
123void ?{}(__condition_criterion_t & this, monitor$ * target, __condition_node_t * owner );
124
125struct condition {
126        // Link list which contains the blocked threads as-well as the information needed to unblock them
127        __queue_t(__condition_node_t) blocked;
128
129        // Array of monitor pointers (Monitors are NOT contiguous in memory)
130        monitor$ ** monitors;
131
132        // Number of monitors in the array
133        __lock_size_t monitor_count;
134};
135
136static inline void ?{}( condition & this ) {
137        this.monitors = 0p;
138        this.monitor_count = 0;
139}
140
141static inline void ^?{}( condition & this ) {
142        free( this.monitors );
143}
144
145              void wait        ( condition & this, uintptr_t user_info = 0 );
146static inline bool is_empty    ( condition & this ) { return this.blocked.head == 1p; }
147              bool signal      ( condition & this );
148              bool signal_block( condition & this );
149static inline bool signal_all  ( condition & this ) { bool ret = false; while(!is_empty(this)) { ret = signal(this) || ret; } return ret; }
150         uintptr_t front       ( condition & this );
151
152//-----------------------------------------------------------------------------
153// External scheduling
154
155struct __acceptable_t {
156        inline struct __monitor_group_t;
157        bool is_dtor;
158};
159
160void __waitfor_internal( const __waitfor_mask_t & mask, int duration );
161
162// lock and unlock routines for mutex statements to use
163void lock( monitor$ * this );
164void unlock( monitor$ * this );
165
166// Local Variables: //
167// mode: c //
168// tab-width: 4 //
169// End: //
Note: See TracBrowser for help on using the repository browser.