/
io.go
57 lines (46 loc) · 844 Bytes
/
io.go
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
package server
import (
"os"
"sync"
)
type Mutex struct {
waitGroup sync.WaitGroup
c chan struct{}
}
func NewMutex() *Mutex {
return &Mutex{
waitGroup: sync.WaitGroup{},
c: make(chan struct{}, 1),
}
}
func (m *Mutex) CreateJobs(nJobs int) {
m.waitGroup.Add(nJobs)
}
func (m *Mutex) Wait() {
m.waitGroup.Wait()
}
func (m *Mutex) Done() {
m.waitGroup.Done()
}
func (m *Mutex) SendSignal() {
m.waitGroup.Add(1)
m.c <- struct{}{}
m.waitGroup.Wait()
}
func (m *Mutex) ListenForSignal() {
<- m.c
}
func (srv *Server) ReadFileConcurrent(filePath string) ([]byte, error) {
fm, ok := srv.Router.fileMutexMap[filePath]
if !ok {
fm = new(sync.Mutex)
srv.Router.fileMutexMap[filePath] = fm
}
fm.Lock()
b, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
fm.Unlock()
return b, nil
}