source: src/libcfa/concurrency/monitor.c @ 95448f1e

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 95448f1e 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
RevLine 
[f07e037]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//
[84c52a8]8// monitor_desc.c --
[f07e037]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
[cb0e6de]21extern "C" {
22        void __enter_monitor_desc(monitor_desc * this) {
23                lock( &this->lock );
24                thread_desc * thrd = this_thread();
[f07e037]25
[cb0e6de]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 );
[cc7f4b1]40
[cb0e6de]41                        //ScheduleInternal will unlock spinlock, no need to unlock ourselves
42                        return; 
43                }
[f07e037]44
[cb0e6de]45                unlock( &this->lock );
46        }
[f07e037]47
[cb0e6de]48        void __leave_monitor_desc(monitor_desc * this) {
49                lock( &this->lock );
[f07e037]50
[cb0e6de]51                thread_desc * thrd = this_thread();
52                assert( thrd == this->owner );
[cc7f4b1]53
[cb0e6de]54                //Leaving a recursion level, decrement the counter
55                this->recursion -= 1;
[f07e037]56
[cb0e6de]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 );
[cc7f4b1]62
[cb0e6de]63                        //We are passing the monitor to someone else, which means recursion level is not 0
64                        this->recursion = new_owner ? 1 : 0;
65                }       
[f07e037]66
[cb0e6de]67                unlock( &this->lock );
[51f3798]68
[cb0e6de]69                //If we have a new owner, we need to wake-up the thread
70                if( new_owner ) {
71                        ScheduleThread( new_owner );
72                }
[cc7f4b1]73        }
[2781e65]74}
75
[84c52a8]76void enter(monitor_desc ** monitors, int count) {
[2781e65]77        for(int i = 0; i < count; i++) {
[cb0e6de]78                __enter_monitor_desc( monitors[i] );
[2781e65]79        }
80}
81
[84c52a8]82void leave(monitor_desc ** monitors, int count) {
[2781e65]83        for(int i = count - 1; i >= 0; i--) {
[cb0e6de]84                __leave_monitor_desc( monitors[i] );
[2781e65]85        }
[f07e037]86}
Note: See TracBrowser for help on using the repository browser.