sync: make new_mutex() and new_waitgroup() return &Mutex and &Waitgroup

pull/3507/head
Delyan Angelov 2020-01-19 21:32:22 +02:00 committed by Alexander Medvednikov
parent bc64263dd8
commit 0d52cc97e4
5 changed files with 12 additions and 20 deletions

View File

@ -63,15 +63,11 @@ fn main() {
} }
ids = tmp ids = tmp
} }
wg := sync.new_waitgroup()
mtx := sync.new_mutex()
mut fetcher := &Fetcher{ mut fetcher := &Fetcher{
ids: ids ids: ids
mu: 0 mu: sync.new_mutex()
wg: 0 wg: sync.new_waitgroup()
} }
fetcher.mu = &mtx
fetcher.wg = &wg
fetcher.wg.add(ids.len) fetcher.wg.add(ids.len)
for i := 0; i < nr_threads; i++ { for i := 0; i < nr_threads; i++ {
go fetcher.fetch() go fetcher.fetch()

View File

@ -103,7 +103,7 @@ pub struct Clipboard {
selection Atom //the selection atom selection Atom //the selection atom
window Window window Window
atoms []Atom atoms []Atom
mutex sync.Mutex mutex &sync.Mutex
text string // text data sent or received text string // text data sent or received
got_text bool // used to confirm that we have got the text got_text bool // used to confirm that we have got the text
is_owner bool // to save selection owner state is_owner bool // to save selection owner state
@ -137,7 +137,7 @@ fn new_x11_clipboard(selection atom_type) &Clipboard {
if display == C.NULL { if display == C.NULL {
println("ERROR: No X Server running. Clipboard cannot be used.") println("ERROR: No X Server running. Clipboard cannot be used.")
return &Clipboard{ display: 0 } return &Clipboard{ display: 0 mutex: sync.new_mutex() }
} }
mut cb := &Clipboard{ mut cb := &Clipboard{

View File

@ -16,9 +16,8 @@ pub struct Mutex {
mutex C.pthread_mutex_t mutex C.pthread_mutex_t
} }
pub fn new_mutex() Mutex { pub fn new_mutex() &Mutex {
m := Mutex{ m := &Mutex{}
}
C.pthread_mutex_init(&m.mutex, C.NULL) C.pthread_mutex_init(&m.mutex, C.NULL)
return m return m
} }

View File

@ -39,8 +39,8 @@ const (
WAIT_FAILED = 0xFFFFFFFF WAIT_FAILED = 0xFFFFFFFF
) )
pub fn new_mutex() Mutex { pub fn new_mutex() &Mutex {
sm := Mutex{} sm := &Mutex{}
unsafe { unsafe {
mut m := sm mut m := sm
m.mx = C.CreateMutex(0, false, 0) m.mx = C.CreateMutex(0, false, 0)
@ -48,8 +48,8 @@ pub fn new_mutex() Mutex {
m.state = .broken // handle broken and mutex state are broken m.state = .broken // handle broken and mutex state are broken
return sm return sm
} }
return sm
} }
return sm
} }
pub fn (m mut Mutex) lock() { pub fn (m mut Mutex) lock() {

View File

@ -5,15 +5,12 @@ module sync
// [init_with=new_waitgroup] // TODO: implement support for init_with struct attribute, and disallow WaitGroup{} from outside the sync.new_waitgroup() function. // [init_with=new_waitgroup] // TODO: implement support for init_with struct attribute, and disallow WaitGroup{} from outside the sync.new_waitgroup() function.
pub struct WaitGroup { pub struct WaitGroup {
mut: mut:
mu Mutex mu &Mutex = &Mutex(0)
active int active int
} }
pub fn new_waitgroup() WaitGroup { pub fn new_waitgroup() &WaitGroup {
mut w := WaitGroup{ return &WaitGroup{mu: sync.new_mutex() }
}
w.mu = sync.new_mutex()
return w
} }
pub fn (wg mut WaitGroup) add(delta int) { pub fn (wg mut WaitGroup) add(delta int) {