summaryrefslogtreecommitdiff
path: root/philo.go
blob: 030195ca97a8afc209bb4b35008dd127416aeb8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main

import (
	"errors"
	"flag"
	"fmt"
	"math/rand"
	"strings"
	"sync"
	"time"
)

type Fork struct{}

type Philo struct {
	Name    string
	LHS     chan Fork
	RHS     chan Fork
	Bites   int
	Delay   time.Duration
	TimeOut time.Duration
}

func (p Philo) rndDelay() {
	n := rand.Intn(int(p.Delay))
	time.Sleep(time.Duration(n))
}

func (p Philo) printState(s string) {
	fmt.Printf("%10s %s\n", p.Name, s)
}

type stateFn func() stateFn

func (p *Philo) arrive() stateFn {
	p.printState("arrives")

	return p.hungry
}

func (p *Philo) hungry() stateFn {
	p.printState("is hungry")

	<-p.LHS // grab left fork

	select {
	case <-p.RHS: // try to grab right fork
		return p.eat
	case <-time.After(p.TimeOut):
		p.LHS <- Fork{} // put left fork back
		return p.starve
	}

	return p.eat
}

func (p *Philo) starve() stateFn {
	p.printState("is starving")
	p.rndDelay()

	return p.hungry
}

func (p *Philo) eat() stateFn {
	p.printState("is eating")
	p.rndDelay()

	p.LHS <- Fork{} // release left fork
	p.RHS <- Fork{} // release right fork

	if p.Bites--; p.Bites <= 0 {
		return p.leave
	}

	return p.think
}

func (p *Philo) think() stateFn {
	p.printState("is thinking")
	p.rndDelay()

	return p.hungry
}

func (p *Philo) leave() stateFn {
	p.printState("leaves")

	return nil
}

func prepare(n int) []chan Fork {
	forks := make([]chan Fork, n)

	for i := range forks {
		forks[i] = make(chan Fork, 1)
		forks[i] <- Fork{} // put a fork
	}

	return forks
}

type Names []string

func (n Names) String() string {
	return fmt.Sprint(strings.Join(n, ","))
}

func (n *Names) Set(s string) error {
	*n = Names(strings.Split(s, ","))
	if len(*n) < 2 {
		return errors.New("at least 2 names are required")
	}
	return nil
}

func main() {
	names := Names{"Aristotle", "Kant", "Spinoza", "Marx", "Russell"}
	bites := flag.Int("bites", 3, "number of rounds")
	delay := flag.Duration("max delay", 3*time.Second, "delay")
	timeOut := flag.Duration("timeout", time.Second, "delay")
	flag.Var(&names, "names", "philospher names")
	flag.Parse()

	rand.Seed(time.Now().UnixNano())

	fmt.Println(len(names), "philosophers dining")
	defer fmt.Println("Table is empty")

	forks := prepare(len(names))

	wg := sync.WaitGroup{}
	defer wg.Wait()

	for i, name := range names {
		wg.Add(1)

		p := &Philo{
			Name:    name,
			LHS:     forks[i],
			RHS:     forks[(i+1)%len(names)],
			Bites:   *bites,
			Delay:   *delay,
			TimeOut: *timeOut,
		}

		go func(p *Philo) {
			defer wg.Done()

			for state := p.arrive; state != nil; {
				state = state()
			}
		}(p)
	}
}