source: src/libcfa/concurrency/monitor @ d6655bd

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since d6655bd was 751d963, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

add delete for mutex types

  • Property mode set to 100644
File size: 4.0 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 : Sat Oct  7 18:06:45 2017
13// Update Count     : 10
14//
15
16#pragma once
17
18#include <stddef.h>
19
20#include <assert.h>
21#include "invoke.h"
22#include "stdlib"
23
24trait is_monitor(dtype T) {
25        monitor_desc * get_monitor( T & );
26        void ^?{}( T & mutex );
27};
28
29static inline void ?{}(monitor_desc & this) {
30        (this.lock){};
31        (this.entry_queue){};
32        (this.signal_stack){};
33        this.owner         = NULL;
34        this.recursion     = 0;
35        this.mask.accepted = NULL;
36        this.mask.clauses  = NULL;
37        this.mask.size     = 0;
38        this.dtor_node     = NULL;
39}
40
41// static inline int ?<?(monitor_desc* lhs, monitor_desc* rhs) {
42//      return ((intptr_t)lhs) < ((intptr_t)rhs);
43// }
44
45struct monitor_guard_t {
46        monitor_desc ** m;
47        int count;
48        monitor_desc ** prev_mntrs;
49        unsigned short  prev_count;
50        fptr_t          prev_func;
51};
52
53void ?{}( monitor_guard_t & this, monitor_desc ** m, int count, void (*func)() );
54void ^?{}( monitor_guard_t & this );
55
56struct monitor_dtor_guard_t {
57        monitor_desc * m;
58        monitor_desc ** prev_mntrs;
59        unsigned short  prev_count;
60        fptr_t          prev_func;
61};
62
63void ?{}( monitor_dtor_guard_t & this, monitor_desc ** m, void (*func)() );
64void ^?{}( monitor_dtor_guard_t & this );
65
66static inline forall( dtype T | sized(T) | { void ^?{}( T & mutex ); } )
67void delete( T * th ) {
68        ^(*th){};
69        free( th );
70}
71
72//-----------------------------------------------------------------------------
73// Internal scheduling
74
75struct __condition_criterion_t {
76        bool ready;                                             //Whether or not the criterion is met (True if met)
77        monitor_desc * target;                          //The monitor this criterion concerns
78        struct __condition_node_t * owner;              //The parent node to which this criterion belongs
79        __condition_criterion_t * next;         //Intrusive linked list Next field
80};
81
82struct __condition_node_t {
83        thread_desc * waiting_thread;                   //Thread that needs to be woken when all criteria are met
84        __condition_criterion_t * criteria;     //Array of criteria (Criterions are contiguous in memory)
85        unsigned short count;                           //Number of criterions in the criteria
86        __condition_node_t * next;                      //Intrusive linked list Next field
87        uintptr_t user_info;                            //Custom user info accessible before signalling
88};
89
90struct __condition_blocked_queue_t {
91        __condition_node_t * head;
92        __condition_node_t ** tail;
93};
94
95void ?{}(__condition_node_t & this, thread_desc * waiting_thread, unsigned short count, uintptr_t user_info );
96void ?{}(__condition_criterion_t & this );
97void ?{}(__condition_criterion_t & this, monitor_desc * target, __condition_node_t * owner );
98
99void ?{}( __condition_blocked_queue_t & );
100void append( __condition_blocked_queue_t *, __condition_node_t * );
101__condition_node_t * pop_head( __condition_blocked_queue_t * );
102
103struct condition {
104        __condition_blocked_queue_t blocked;    //Link list which contains the blocked threads as-well as the information needed to unblock them
105        monitor_desc ** monitors;                       //Array of monitor pointers (Monitors are NOT contiguous in memory)
106        unsigned short monitor_count;                   //Number of monitors in the array
107};
108
109static inline void ?{}( condition & this ) {
110        this.monitors = NULL;
111        this.monitor_count = 0;
112}
113
114static inline void ^?{}( condition & this ) {
115        free( this.monitors );
116}
117
118void wait( condition * this, uintptr_t user_info = 0 );
119bool signal( condition * this );
120bool signal_block( condition * this );
121static inline bool is_empty( condition * this ) { return !this->blocked.head; }
122uintptr_t front( condition * this );
123
124//-----------------------------------------------------------------------------
125// External scheduling
126
127struct __acceptable_t {
128        __monitor_group_t;
129        bool is_dtor;
130};
131
132void __waitfor_internal( const __waitfor_mask_t & mask, int duration );
133
134// Local Variables: //
135// mode: c //
136// tab-width: 4 //
137// End: //
Note: See TracBrowser for help on using the repository browser.