Ejemplo n.º 1
0
func NewCMon() *ImpCMon {
	//
	var nR, nL int
	c := func(a Any, k uint) bool {
		switch k {
		case lIn:
			return nR == 0
		case rIn:
			return nL == 0
		}
		return true
	}
	f := func(a Any, k uint) Any {
		switch k {
		case lIn:
			nL++
		case lOut:
			nL--
		case rIn:
			nR++
		case rOut:
			nR--
		}
		return nil
	}
	return &ImpCMon{mon.NewC(4, f, c)}
}
Ejemplo n.º 2
0
func NewCMon() *ImpCMon {
	//
	var nR, nW int
	f := func(a Any, i uint) Any {
		switch i {
		case rIn:
			nR++
		case rOut:
			nR--
		case wIn:
			nW++
		case wOut:
			nW--
		}
		return nil
	}
	p := func(a Any, i uint) bool {
		switch i {
		case rIn:
			return nW == 0
		case wIn:
			return nR == 0 && nW == 0
		}
		return true
	}
	return &ImpCMon{mon.NewC(4, f, p)}
}
Ejemplo n.º 3
0
func NewLockCMon() *LockCMon {
	//
	nForks := make([]uint, nPhilos)
	for p := uint(0); p < nPhilos; p++ {
		nForks[p] = 2
	}
	c := func(a Any, i uint) bool {
		if i == lock {
			p := a.(uint) // p-th philosopher
			return nForks[p] == 2
		}
		return true // unlock
	}
	f := func(a Any, i uint) Any {
		p := a.(uint) // p-th philosopher
		if i == lock {
			nForks[left(p)]--
			nForks[right(p)]--
		} else { // unlock
			nForks[left(p)]++
			nForks[right(p)]++
		}
		return p
	}
	return &LockCMon{mon.NewC(nPhilos, f, c)}
}
Ejemplo n.º 4
0
func NewCMon() *ImpCMon {
	//
	var n uint
	do := func(a Any, i uint) Any {
		if i == customer {
			n++
		} else { // i == barber
			n--
		}
		return 0
	}
	c := func(a Any, i uint) bool {
		if i == customer {
			return true
		}
		return n > 0 // i == barber
	}
	return &ImpCMon{mon.NewC(2, do, c)}
}
Ejemplo n.º 5
0
func NewMon() *ImpMon {
	//
	x := new(ImpMon)
	x.balance = euro.New()
	x.balance.Set2(0, 0)
	p := func(a Any, i uint) bool {
		if i == 1 { // draw
			return a.(*euro.Imp).Eq(x.balance) || a.(*euro.Imp).Less(x.balance)
		}
		return true // deposit
	}
	f := func(a Any, i uint) Any {
		if i == 0 { // deposit
			x.balance.Plus(a.(*euro.Imp))
		} else { // draw
			x.balance.Minus(a.(*euro.Imp))
		}
		return x.balance
	}
	x.Imp = mon.NewC(2, f, p)
	return x
}
Ejemplo n.º 6
0
func NewMon(a Any, n uint) *ImpMon {
	//
	if a == nil || n == 0 {
		return nil
	}
	x := new(ImpMon)
	x.Buffer = newBuffer(a, n)
	p := func(a Any, i uint) bool {
		if i == 0 { // Ins
			return !x.Buffer.Full()
		}
		return x.Buffer.Num() > 0 // Get
	}
	f := func(a Any, i uint) Any {
		if i == 0 { // Ins
			x.Buffer.Ins(a)
			return a
		} // Get
		return x.Buffer.Get()
	}
	x.Monitor /* x.m */ = mon.NewC(3, f, p)
	return x
}
Ejemplo n.º 7
0
func NewCMon() *ImpCMon {
	//
	var m mon.Monitor
	var avail [3]bool
	smokerOut := true
	c := func(a Any, k uint) bool {
		if a == nil { // AgentOut, SmokerOut
			return true
		}
		if k == agent { // AgentIn
			return smokerOut
		}
		u := a.(uint) // SmokerIn
		return avail[(u+1)%3] && avail[(u+2)%3]
	}
	f := func(a Any, k uint) Any {
		if k == agent {
			if a == nil { // AgentOut
				// nixtun
			} else { // AgentIn
				u := a.(uint)
				avail[(u+1)%3], avail[(u+2)%3] = true, true
			}
		} else {
			if a == nil { // SmokerOut
				smokerOut = true
			} else { // SmokerIn
				u := a.(uint)
				avail[(u+1)%3], avail[(u+2)%3] = false, false
				smokerOut = false
			}
		}
		return a
	}
	m = mon.NewC(2, f, c)
	return &ImpCMon{m}
}