source: src/libcfa/concurrency/monitor.c @ 9c31349

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 9c31349 was cb0e6de, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Threads now use monitor semantics to wait until completion

  • Property mode set to 100644
File size: 2.1 KB
Line 
1//                              -*- Mode: CFA -*-
2//
3// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4//
5// The contents of this file are covered under the licence agreement in the
6// file "LICENCE" distributed with Cforall.
7//
8// monitor_desc.c --
9//
10// Author           : Thierry Delisle
11// Created On       : Thd Feb 23 12:27:26 2017
12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count     : 0
15//
16
17#include "monitor"
18
19#include "kernel_private.h"
20
21extern "C" {
22        void __enter_monitor_desc(monitor_desc * this) {
23                lock( &this->lock );
24                thread_desc * thrd = this_thread();
25
26                if( !this->owner ) {
27                        //No one has the monitor, just take it
28                        this->owner = thrd;
29                        this->recursion = 1;
30                }
31                else if( this->owner == thrd) {
32                        //We already have the monitor, just not how many times we took it
33                        assert( this->recursion > 0 );
34                        this->recursion += 1;
35                }
36                else {
37                        //Some one else has the monitor, wait in line for it
38                        append( &this->entry_queue, thrd );
39                        ScheduleInternal( &this->lock );
40
41                        //ScheduleInternal will unlock spinlock, no need to unlock ourselves
42                        return; 
43                }
44
45                unlock( &this->lock );
46        }
47
48        void __leave_monitor_desc(monitor_desc * this) {
49                lock( &this->lock );
50
51                thread_desc * thrd = this_thread();
52                assert( thrd == this->owner );
53
54                //Leaving a recursion level, decrement the counter
55                this->recursion -= 1;
56
57                //If we left the last level of recursion it means we are changing who owns the monitor
58                thread_desc * new_owner = 0;
59                if( this->recursion == 0) {
60                        //Get the next thread in the list
61                        new_owner = this->owner = pop_head( &this->entry_queue );
62
63                        //We are passing the monitor to someone else, which means recursion level is not 0
64                        this->recursion = new_owner ? 1 : 0;
65                }       
66
67                unlock( &this->lock );
68
69                //If we have a new owner, we need to wake-up the thread
70                if( new_owner ) {
71                        ScheduleThread( new_owner );
72                }
73        }
74}
75
76void enter(monitor_desc ** monitors, int count) {
77        for(int i = 0; i < count; i++) {
78                __enter_monitor_desc( monitors[i] );
79        }
80}
81
82void leave(monitor_desc ** monitors, int count) {
83        for(int i = count - 1; i >= 0; i--) {
84                __leave_monitor_desc( monitors[i] );
85        }
86}
Note: See TracBrowser for help on using the repository browser.