Skip to content

Commit 376be18

Browse files
committed
Trying to use waitgroup but getting deadlocks
1 parent 3c5b4e2 commit 376be18

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

Block.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ package cssminify
22

33
import (
44
"bufio"
5-
"fmt"
65
"os"
6+
"sync"
77
)
88

99
type Block struct {
1010
selector []byte
1111
pairs []Pair
1212
}
1313

14-
func Blocks(cb chan Block, file string) {
14+
func Blocks(cb chan Block, file string, wg sync.WaitGroup) {
1515
cf := make(chan byte)
1616

17-
go readFile(cf, file)
18-
go parse(cf, cb)
17+
wg.Add(1)
18+
go readFile(cf, file, wg)
19+
wg.Add(1)
20+
go parse(cf, cb, wg)
1921
}
2022

2123
func stripLetter(content []byte) (byte, []byte) {
@@ -29,7 +31,7 @@ func stripLetter(content []byte) (byte, []byte) {
2931
return letter, content
3032
}
3133

32-
func readFile(cf chan byte, root string) {
34+
func readFile(cf chan byte, root string, wg sync.WaitGroup) {
3335
file, err := os.Open(root)
3436
if err != nil {
3537
panic(err)
@@ -38,15 +40,17 @@ func readFile(cf chan byte, root string) {
3840

3941
reader := bufio.NewReader(file)
4042
for b, err := reader.ReadByte(); err != nil; b, err = reader.ReadByte() {
41-
fmt.Printf("%c\n", b)
4243
cf <- b
4344
}
45+
defer wg.Done()
4446
}
4547

46-
func parse(cf chan byte, cb chan Block) {
48+
func parse(cf chan byte, cb chan Block, wg sync.WaitGroup) {
4749
var letter byte
4850
state := new(State)
4951
for letter = <-cf; letter != 0; letter = <-cf {
50-
state.parse(cf, cb)
52+
wg.Add(1)
53+
state.parse(cf, cb, wg)
5154
}
55+
defer wg.Done()
5256
}

Minify.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package cssminify
22

33
import (
44
"fmt"
5+
"sync"
56
)
67

7-
func Minify(cb chan Block) {
8+
func Minify(cb chan Block, wg sync.WaitGroup) {
89
for block := <-cb; block.selector != nil; block = <-cb {
910
fmt.Printf("%s\n", block.selector)
1011
}
12+
defer wg.Done()
1113
}

State.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cssminify
33
import (
44
"bytes"
55
"strings"
6+
"sync"
67
)
78

89
type State struct {
@@ -34,7 +35,7 @@ const (
3435
IN_VALUE = iota
3536
)
3637

37-
func (s *State) parse(cf chan byte, cb chan Block) {
38+
func (s *State) parse(cf chan byte, cb chan Block, wg sync.WaitGroup) {
3839
letter := <-cf
3940
switch letter {
4041
case '/':
@@ -52,6 +53,7 @@ func (s *State) parse(cf chan byte, cb chan Block) {
5253
default:
5354
s.rest(letter)
5455
}
56+
defer wg.Done()
5557
}
5658

5759
func (s *State) slash(letter byte) {

0 commit comments

Comments
 (0)