Use mutex pointer in network state

This commit is contained in:
Ken-Håvard Lieng 2020-06-16 01:29:35 +02:00
parent 2ea4584c97
commit 7040f1c8d0
2 changed files with 6 additions and 2 deletions

View File

@ -27,7 +27,7 @@ type Network struct {
user *User user *User
client *irc.Client client *irc.Client
channels map[string]*Channel channels map[string]*Channel
lock sync.Mutex lock *sync.Mutex
} }
func (n *Network) Save() error { func (n *Network) Save() error {
@ -53,6 +53,7 @@ func (n *Network) Copy() *Network {
user: n.user, user: n.user,
client: n.client, client: n.client,
channels: n.channels, channels: n.channels,
lock: &sync.Mutex{},
} }
n.lock.Unlock() n.lock.Unlock()
@ -137,6 +138,7 @@ func (n *Network) NewChannel(name string) *Channel {
Network: n.Host, Network: n.Host,
Name: name, Name: name,
user: n.user, user: n.user,
lock: &sync.Mutex{},
} }
} }
@ -162,7 +164,7 @@ type Channel struct {
Joined bool Joined bool
user *User user *User
lock sync.Mutex lock *sync.Mutex
} }
func (c *Channel) Save() error { func (c *Channel) Save() error {
@ -177,6 +179,7 @@ func (c *Channel) Copy() *Channel {
Topic: c.Topic, Topic: c.Topic,
Joined: c.Joined, Joined: c.Joined,
user: c.user, user: c.user,
lock: &sync.Mutex{},
} }
c.lock.Unlock() c.lock.Unlock()

View File

@ -167,6 +167,7 @@ func (u *User) NewNetwork(template *Network, client *irc.Client) *Network {
template.user = u template.user = u
template.client = client template.client = client
template.channels = map[string]*Channel{} template.channels = map[string]*Channel{}
template.lock = &sync.Mutex{}
return template return template
} }