Ejemplo n.º 1
0
func NewCS() *ImpCS {
	//
	var avail [3]bool
	smokerOut := true
	c := func(k uint) bool {
		if k == 3 { // Agent
			return smokerOut
		}
		return avail[(k+1)%3] && avail[(k+2)%3] // Smoker
	}
	e := func(a Any, k uint) {
		if k == 3 { // Agent
			u := a.(uint)
			avail[(u+1)%3], avail[(u+2)%3] = true, true
		} else { // Smoker
			smokerOut = false
			avail[(k+1)%3], avail[(k+2)%3] = false, false
		}
	}
	l := func(a Any, k uint) {
		if k == 3 { // AgentOut
		} else { // SmokerOut
			smokerOut = true
		}
	}
	return &ImpCS{cs.New(3+1, c, e, l)}
}
Ejemplo n.º 2
0
func NewB(m uint) *ImpB {
	//
	var s cs.CriticalSection
	if m < 1 {
		m = 1
	}
	var nR, nW, rR uint
	c := func(k uint) bool {
		if k == reader {
			return nW == 0 && (s.Blocked(writer) || rR < m)
		}
		return nR == 0 && nW == 0 /* && ! x.s.Blocked (readeer) */ // writer
	}
	e := func(a Any, k uint) {
		if k == reader {
			nR++
			rR++
		} else { // writer
			nW++
			rR = 0
		}
	}
	l := func(a Any, k uint) {
		if k == reader {
			nR--
		} else { // writer
			nW--
		}
	}
	s = cs.New(2, c, e, l)
	return &ImpB{s}
}
Ejemplo n.º 3
0
func NewFair() *ImpFair {
	//
	var s *cs.Imp
	var nR, nW uint
	var lastR bool
	c := func(k uint) bool {
		if k == reader {
			return nW == 0 && (!s.Blocked(writer) || !lastR)
		}
		return nR == 0 && nW == 0 && (!s.Blocked(reader) || lastR) // writer
	}
	e := func(X Any, k uint) {
		if k == reader {
			nR++
			lastR = true
		} else { // writer
			nW++
			lastR = false
		}
	}
	l := func(X Any, k uint) {
		if k == reader {
			nR--
		} else { // writer
			nW--
		}
	}
	s = cs.New(2, c, e, l)
	return &ImpFair{s}
}
Ejemplo n.º 4
0
func NewCS2() *ImpCS2 {
	//
	var s cs.CriticalSection
	var nR, nW uint
	c := func(k uint) bool {
		if k == reader {
			return nW == 0 && !s.Blocked(writer)
		}
		return nR == 0 && nW == 0 // writer
	}
	e := func(a Any, k uint) {
		if k == reader {
			nR++
		} else { // writer
			nW = 1
		}
	}
	l := func(a Any, k uint) {
		if k == reader {
			nR--
		} else { // writer
			nW = 0
		}
	}
	s = cs.New(2, c, e, l)
	return &ImpCS2{s}
}
Ejemplo n.º 5
0
func NewCS() *ImpCS {
	//
	var nL, nR uint
	c := func(k uint) bool {
		if k == left {
			return nR == 0
		}
		return nL == 0 // right
	}
	e := func(a Any, k uint) {
		if k == left {
			nL++
		} else { // right
			nR++
		}
	}
	l := func(X Any, k uint) {
		if k == left {
			nL--
		} else { // right
			nR--
		}
	}
	return &ImpCS{cs.New(2, c, e, l)}
}
Ejemplo n.º 6
0
func NewCS() *ImpCS {
	//
	var nR, nW uint
	c := func(k uint) bool {
		if k == reader {
			return nW == 0
		}
		return nR == 0 && nW == 0 // writer
	}
	e := func(a Any, k uint) {
		if k == reader {
			nR++
		} else { // writer
			nW = 1
		}
	}
	a := func(a Any, k uint) {
		if k == reader {
			nR--
		} else { // writer
			nW = 0
		}
	}
	return &ImpCS{cs.New(2, c, e, a)}
}
Ejemplo n.º 7
0
func New(nc, nr uint) *Imp {
	//
	x := new(Imp)
	x.nC, x.nR = nc, nr
	x.stat = make([]status, x.nC)
	for r := uint(0); r < x.nR; r++ {
		x.stat[r].max = make([]uint, x.nC)
		for c := uint(0); c < x.nC; c++ {
			x.stat[r].max[c] = ker.MaxNat
		}
	}
	c := func(k uint) bool {
		var b bool
		for r := uint(0); r < x.nR; r++ {
			b = b ||
				x.stat[r].number == 0 ||
				x.stat[r].class == k && x.stat[r].number < x.stat[r].max[k]
		}
		return b
	}
	e := func(a Any, k uint) {
		for r := uint(0); r < x.nR; r++ {
			if x.stat[r].number == 0 || x.stat[r].class == k {
				x.stat[r].class = k
				x.stat[r].number++
				n := a.(*uint)
				*n = r
				return
			}
		}
		ker.Panic(pack + ".New() error")
	}
	l := func(a Any, k uint) {
		for r := uint(0); r < x.nR; r++ {
			if x.stat[r].class == k && x.stat[r].number > 0 {
				x.stat[r].number--
			}
		}
	}
	x.Imp = cs.New(x.nC, c, e, l)
	return x
}
Ejemplo n.º 8
0
func NewB(l, r uint) *ImpB {
	//
	if l == 0 {
		l = ker.MaxNat
	}
	if r == 0 {
		r = ker.MaxNat
	}
	var nL, nR, rL, rR, zL, zR uint
	var s cs.CriticalSection
	c := func(k uint) bool {
		if k == left {
			return nR == 0 && (!s.Blocked(right) || rL < zL)
		}
		return nL == 0 && (!s.Blocked(left) || rR < zR) // right
	}
	e := func(A Any, k uint) {
		if k == left {
			nL++
			rL++
			rR = 0
		} else { // right
			nR++
			rR++
			rL = 0
		}
	}
	a := func(A Any, k uint) {
		if k == left {
			nL--
		} else { // right
			nR--
		}
	}
	zL, zR = l, r
	s = cs.New(2, c, e, a)
	return &ImpB{s}
}