示例#1
0
func NewLockMonf () *LockMonf {
//
  var m mon.Monitor
  f:= func (a Any, i uint) Any {
        p:= a.(uint)
        if i == lock {
          if stat[left(p)] == dining && stat[right(p)] == satisfied ||
             stat[left(p)] == satisfied && stat[right(p)] == dining {
            changeStatus (p, starving)
          }
          for stat[left(p)] == dining || stat[left(p)] == starving ||
            stat[right(p)] == dining || stat[right(p)] == starving {
            m.Wait (p)
          }
        } else { // unlock
          if stat[left(p)] == hungry || stat[left(p)] == starving {
            m.Signal (left(p))
          }
          if stat[right(p)] == hungry || stat[right(p)] == starving {
            m.Signal (right(p))
          }
        }
        return nil
      }
  m = mon.New (nPhilos, f)
  return &LockMonf { m }
}
示例#2
0
func NewMon() *ImpMon {
	//
	var m mon.Monitor
	var nR, nW int
	f := func(a Any, k uint) Any {
		switch k {
		case rIn:
			for nW > 0 {
				m.Wait(rIn)
			}
			nR++
			m.Signal(rIn)
		case rOut:
			nR--
			if nR == 0 {
				m.Signal(wIn)
			}
		case wIn:
			for nR > 0 || nW > 0 {
				m.Wait(wIn)
			}
			nW = 1
		case wOut:
			nW = 0
			if m.Awaited(rIn) {
				m.Signal(rIn)
			} else {
				m.Signal(wIn)
			}
		}
		return nil
	}
	m = mon.New(4, f)
	return &ImpMon{m}
}
示例#3
0
func NewMon(n uint) *ImpMon {
	//
	if n < 2 {
		return nil
	}
	var m mon.Monitor
	involved := n
	waiting := uint(0)
	f := func(a Any, k uint) Any {
		waiting++
		if waiting < involved {
			m.Wait(0)
		} else {
			/*
			   m.SignalAll (0) // simple solution with broadcast
			   waiting = 0
			*/
			for waiting > 0 { // standard solution
				waiting--
				if waiting > 0 {
					m.Signal(0)
				}
			}
		}
		return nil
	}
	m = mon.New(1, f)
	return &ImpMon{m}
}
示例#4
0
func NewMon() *ImpMon {
	//
	var x mon.Monitor
	var n uint
	//  barberFree:= true
	do := func(a Any, i uint) Any {
		if i == customer {
			//           for ! barberFree {
			//             x.Wait (barber)
			//           }
			//           barberFree = false
			n++
			x.Signal(customer)
		} else { // i == barber
			//           barberFree = true
			//           x.Signal (barber)
			for n == 0 {
				x.Wait(customer)
			}
			n--
		}
		return 0
	}
	x = mon.New(2, do)
	return &ImpMon{x}
}
示例#5
0
func NewMon() *ImpMon {
	//
	var m mon.Monitor
	var avail [3]bool
	f := func(a Any, k uint) Any {
		if k < 3 {
			if a == nil { // AgentOut
				m.Wait(3)
			} else { // AgentIn
				avail[(k+1)%3], avail[(k+2)%3] = true, true
				m.Signal(k)
			}
		} else { // k == 3
			if a == nil { // SmokerOut
				m.Signal(3)
			} else { // SmokerIn
				u := a.(uint)
				u1, u2 := (u+1)%3, (u+2)%3
				if !avail[u1] || !avail[u2] {
					m.Wait(u)
				}
				avail[u1], avail[u2] = false, false
			}
		}
		return a
	}
	m = mon.New(3+1, f)
	return &ImpMon{m}
}
示例#6
0
func NewLockMon() *LockMon {
	//
	var m mon.Monitor
	nForks := make([]uint, nPhilos)
	for p := uint(0); p < nPhilos; p++ {
		nForks[p] = 2
	}
	f := func(a Any, i uint) Any {
		p := a.(uint)
		if i == lock {
			changeStatus(p, starving)
			for nForks[p] < 2 {
				m.Wait(p)
			}
			nForks[left(p)]--
			nForks[right(p)]--
		} else { // k == unlock
			nForks[left(p)]++
			nForks[right(p)]++
			if nForks[left(p)] == 2 {
				m.Signal(left(p))
			}
			if nForks[right(p)] == 2 {
				m.Signal(right(p))
			}
		}
		return nil
	}
	m = mon.New(nPhilos, f)
	return &LockMon{m}
}
示例#7
0
func NewLockMonuf() *LockMonuf {
	//
	var m mon.Monitor
	f := func(a Any, k uint) Any {
		p := a.(uint)
		if k == lock {
			changeStatus(p, starving)
			for stat[left(p)] == dining || stat[right(p)] == dining {
				m.Wait(p)
			}
		} else { // k == unlock
			m.Signal(left(p))
			m.Signal(right(p))
		}
		return nil
	}
	m = mon.New(nPhilos, f)
	return &LockMonuf{m}
}