sync: fix mutex on win & waitgroup (all os) update. fixes news_fetcher example on win (#1776)

pull/1779/head
joe-conigliaro 2019-08-29 18:48:03 +10:00 committed by Alexander Medvednikov
parent 4a506b0566
commit 32683ad6fd
3 changed files with 60 additions and 87 deletions

View File

@ -25,15 +25,14 @@ mut:
fn (f mut Fetcher) fetch() {
for {
f.mu.lock()
if f.cursor >= f.ids.len {
f.mu.unlock()
return
}
id := f.ids[f.cursor]
f.mu.lock()
f.cursor++
cursor := f.cursor
f.mu.unlock()
cursor := f.cursor
resp := http.get('https://hacker-news.firebaseio.com/v0/item/${id}.json') or {
println('failed to fetch data from /v0/item/${id}.json')
exit(1)
@ -42,8 +41,8 @@ fn (f mut Fetcher) fetch() {
println('failed to decode a story')
exit(1)
}
f.wg.done()
println('#$cursor) $story.title | $story.url')
f.wg.done()
}
}

View File

@ -5,33 +5,29 @@
module sync
import os
// Unsafe pointer
type Pointer voidptr
// Mutex HANDLE
type MHANDLE voidptr
struct Mutex {
mut:
mx MHANDLE // mutex handle
wstate u32 // wait state
state MutexState // mutex state
cycle_wait i64 // waiting cycles (implemented only with atomic)
cycle_woken i64 // woken cycles ^
reader_sem u32 // reader semarphone
writer_sem u32 // writer semaphones
}
enum MutexState {
broken
waiting
released
abandoned
destroyed
}
const (
WAIT = u32(8) // Waiting mutex
WOKEN = u32(16) // Woken mutex
ABOND = u32(32)
BROKEN = u32(64)
DESTROYED = u32(0)
)
const (
INFINITY = 0xffffffff
INFINITE = 0xffffffff
)
// Ref - https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobject#return-value
@ -45,51 +41,35 @@ const (
pub fn (m mut Mutex) lock() {
// if mutex handle not initalized
if m.mx == MHANDLE(0) {
if isnil(m.mx) {
m.mx = C.CreateMutex(0, false, 0)
_pmhx := int(m.mx)
if (((_pmhx & 0xff) - 1) == 0) || (_pmhx == os.INVALID_HANDLE_VALUE) {
m.wstate = BROKEN // handle broken and mutex state are broken
if isnil(m.mx) {
m.state = .broken // handle broken and mutex state are broken
return
}
}
state := C.WaitForSingleObject(m.mx, INFINITY) // infinity wait
// for {
// if (m.cycle_woken - 1) < 0 {
// break
// }
// if state&0x00000080 {
// continue // abondoned
// }
// m.cycle_wait++
// }
match state {
WAIT_FAILED => { m.wstate = BROKEN }
WAIT_ABANDONED => { m.wstate = ABOND }
WAIT_OBJECT_0 => { m.wstate = WAIT & u32(0xff) }
state := C.WaitForSingleObject(m.mx, INFINITE) // infinite wait
m.state = match state {
WAIT_ABANDONED => { MutexState.abandoned }
WAIT_OBJECT_0 => { MutexState.waiting }
else => { MutexState.broken }
}
// todo implement atomic counter
}
pub fn (m mut Mutex) unlock() {
_pmx := &m.mx
if _pmx != os.INVALID_HANDLE_VALUE {
if m.wstate == (WAIT & u32(0xff)) {
if C.ReleaseMutex(_pmx) != 0 {
m.wstate = WOKEN // woken up mutex
return
}
m.wstate = ABOND
if m.state == .waiting {
if C.ReleaseMutex(m.mx) != 0 {
m.state = .broken
return
}
}
m.wstate = BROKEN
m.state = .released
}
pub fn (m mut Mutex) destroy() {
if m.wstate == WAIT {
if m.state == .waiting {
m.unlock() // unlock mutex before destroying
}
m.wstate = DESTROYED // setting up reference to invalid state
C.CloseHandle(m.mx) // destroy mutex
m.state = .destroyed // setting up reference to invalid state
}

View File

@ -7,23 +7,16 @@ module sync
struct WaitGroup {
mut:
mu Mutex
finished Mutex
active int
}
pub fn (wg mut WaitGroup) add(delta int) {
wg.mu.lock()
if wg.active == 0 {
wg.finished.lock()
}
wg.active += delta
wg.mu.unlock()
if wg.active < 0 {
panic('Negative number of jobs in waitgroup')
}
if wg.active == 0 {
wg.finished.unlock()
}
wg.mu.unlock()
}
pub fn (wg mut WaitGroup) done() {
@ -31,7 +24,8 @@ pub fn (wg mut WaitGroup) done() {
}
pub fn (wg mut WaitGroup) wait() {
wg.finished.lock()
wg.finished.unlock()
for wg.active > 0 {
// waiting
}
}