diff --git a/vlib/log/safe_log.v b/vlib/log/safe_log.v index f8fdb131d8..7b171d8540 100644 --- a/vlib/log/safe_log.v +++ b/vlib/log/safe_log.v @@ -7,7 +7,7 @@ import sync pub struct ThreadSafeLog { Log pub mut: - mu sync.Mutex + mu &sync.Mutex = sync.new_mutex() } // new_thread_safe_log returns a new log structure, whose methods are safe @@ -16,7 +16,6 @@ pub fn new_thread_safe_log() &ThreadSafeLog { mut x := &ThreadSafeLog{ level: .info } - x.mu.init() return x } @@ -26,6 +25,7 @@ pub fn (mut x ThreadSafeLog) free() { unsafe { x.Log.free() x.mu.destroy() + free(x.mu) // C.printf(c'ThreadSafeLog free(x), x: %p\n', x) } } diff --git a/vlib/sync/sync.c.v b/vlib/sync/sync.c.v index 31902db5c4..62bc037859 100644 --- a/vlib/sync/sync.c.v +++ b/vlib/sync/sync.c.v @@ -9,3 +9,9 @@ fn cpanic(res int) { fn cpanic_errno() { cpanic(C.errno) } + +fn should_be_zero(res int) { + if res != 0 { + cpanic(res) + } +} diff --git a/vlib/sync/sync_darwin.c.v b/vlib/sync/sync_darwin.c.v index 9dcbcc7409..7ddf3f58c9 100644 --- a/vlib/sync/sync_darwin.c.v +++ b/vlib/sync/sync_darwin.c.v @@ -92,9 +92,8 @@ pub fn new_mutex() &Mutex { // init initialises the mutex. It should be called once before the mutex is used, // since it creates the associated resources needed for the mutex to work properly. -@[inline] pub fn (mut m Mutex) init() { - C.pthread_mutex_init(&m.mutex, C.NULL) + should_be_zero(C.pthread_mutex_init(&m.mutex, C.NULL)) } // new_rwmutex creates a new read/write mutex instance on the heap, and returns a pointer to it. @@ -108,11 +107,11 @@ pub fn new_rwmutex() &RwMutex { // since it creates the associated resources needed for the mutex to work properly. pub fn (mut m RwMutex) init() { a := RwMutexAttr{} - C.pthread_rwlockattr_init(&a.attr) + should_be_zero(C.pthread_rwlockattr_init(&a.attr)) // Give writer priority over readers C.pthread_rwlockattr_setkind_np(&a.attr, C.PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) C.pthread_rwlockattr_setpshared(&a.attr, C.PTHREAD_PROCESS_PRIVATE) - C.pthread_rwlock_init(&m.mutex, &a.attr) + should_be_zero(C.pthread_rwlock_init(&m.mutex, &a.attr)) } // lock locks the mutex instance (`lock` is a keyword). @@ -138,12 +137,8 @@ pub fn (mut m Mutex) unlock() { // destroy frees the resources associated with the mutex instance. // Note: the mutex itself is not freed. -@[inline] pub fn (mut m Mutex) destroy() { - res := C.pthread_mutex_destroy(&m.mutex) - if res != 0 { - cpanic(res) - } + should_be_zero(C.pthread_mutex_destroy(&m.mutex)) } // rlock locks the given RwMutex instance for reading. @@ -184,12 +179,8 @@ pub fn (mut m RwMutex) try_wlock() bool { // destroy frees the resources associated with the rwmutex instance. // Note: the mutex itself is not freed. -@[inline] pub fn (mut m RwMutex) destroy() { - res := C.pthread_rwlock_destroy(&m.mutex) - if res != 0 { - cpanic(res) - } + should_be_zero(C.pthread_rwlock_destroy(&m.mutex)) } // runlock unlocks the RwMutex instance, locked for reading. @@ -214,7 +205,6 @@ pub fn (mut m RwMutex) unlock() { // new_semaphore creates a new initialised Semaphore instance on the heap, and returns a pointer to it. // The initial counter value of the semaphore is 0. -@[inline] pub fn new_semaphore() &Semaphore { return new_semaphore_init(0) } @@ -232,9 +222,9 @@ pub fn new_semaphore_init(n u32) &Semaphore { // resources needed for the semaphore to work properly. pub fn (mut sem Semaphore) init(n u32) { C.atomic_store_u32(&sem.count, n) - C.pthread_mutex_init(&sem.mtx, C.NULL) + should_be_zero(C.pthread_mutex_init(&sem.mtx, C.NULL)) attr := CondAttr{} - C.pthread_condattr_init(&attr.attr) + should_be_zero(C.pthread_condattr_init(&attr.attr)) C.pthread_condattr_setpshared(&attr.attr, C.PTHREAD_PROCESS_PRIVATE) C.pthread_cond_init(&sem.cond, &attr.attr) C.pthread_condattr_destroy(&attr.attr) @@ -344,12 +334,6 @@ pub fn (mut sem Semaphore) timed_wait(timeout time.Duration) bool { // destroy frees the resources associated with the Semaphore instance. // Note: the semaphore instance itself is not freed. pub fn (mut sem Semaphore) destroy() { - mut res := C.pthread_cond_destroy(&sem.cond) - if res != 0 { - cpanic(res) - } - res = C.pthread_mutex_destroy(&sem.mtx) - if res != 0 { - cpanic(res) - } + should_be_zero(C.pthread_cond_destroy(&sem.cond)) + should_be_zero(C.pthread_mutex_destroy(&sem.mtx)) } diff --git a/vlib/sync/sync_default.c.v b/vlib/sync/sync_default.c.v index e07b6ad646..2cd73191a3 100644 --- a/vlib/sync/sync_default.c.v +++ b/vlib/sync/sync_default.c.v @@ -126,10 +126,7 @@ pub fn (mut m Mutex) unlock() { // destroy frees the resources associated with the mutex instance. // Note: the mutex itself is not freed. pub fn (mut m Mutex) destroy() { - res := C.pthread_mutex_destroy(&m.mutex) - if res != 0 { - cpanic(res) - } + should_be_zero(C.pthread_mutex_destroy(&m.mutex)) } // rlock locks the given RwMutex instance for reading. @@ -171,10 +168,7 @@ pub fn (mut m RwMutex) try_wlock() bool { // destroy frees the resources associated with the rwmutex instance. // Note: the mutex itself is not freed. pub fn (mut m RwMutex) destroy() { - res := C.pthread_rwlock_destroy(&m.mutex) - if res != 0 { - cpanic(res) - } + should_be_zero(C.pthread_rwlock_destroy(&m.mutex)) } // runlock unlocks the RwMutex instance, locked for reading. @@ -307,8 +301,5 @@ pub fn (mut sem Semaphore) timed_wait(timeout time.Duration) bool { // destroy frees the resources associated with the Semaphore instance. // Note: the semaphore instance itself is not freed. pub fn (mut sem Semaphore) destroy() { - res := C.sem_destroy(&sem.sem) - if res != 0 { - cpanic(res) - } + should_be_zero(C.sem_destroy(&sem.sem)) } diff --git a/vlib/sync/sync_freebsd.c.v b/vlib/sync/sync_freebsd.c.v index 5711e22dc0..530849c244 100644 --- a/vlib/sync/sync_freebsd.c.v +++ b/vlib/sync/sync_freebsd.c.v @@ -124,10 +124,7 @@ pub fn (mut m Mutex) unlock() { // destroy frees the resources associated with the mutex instance. // Note: the mutex itself is not freed. pub fn (mut m Mutex) destroy() { - res := C.pthread_mutex_destroy(&m.mutex) - if res != 0 { - cpanic(res) - } + should_be_zero(C.pthread_mutex_destroy(&m.mutex)) } // rlock locks the given RwMutex instance for reading. @@ -169,10 +166,7 @@ pub fn (mut m RwMutex) try_wlock() bool { // destroy frees the resources associated with the rwmutex instance. // Note: the mutex itself is not freed. pub fn (mut m RwMutex) destroy() { - res := C.pthread_rwlock_destroy(&m.mutex) - if res != 0 { - cpanic(res) - } + should_be_zero(C.pthread_rwlock_destroy(&m.mutex)) } // runlock unlocks the RwMutex instance, locked for reading. @@ -305,8 +299,5 @@ pub fn (mut sem Semaphore) timed_wait(timeout time.Duration) bool { // destroy frees the resources associated with the Semaphore instance. // Note: the semaphore instance itself is not freed. pub fn (mut sem Semaphore) destroy() { - res := C.sem_destroy(&sem.sem) - if res != 0 { - cpanic(res) - } + should_be_zero(C.sem_destroy(&sem.sem)) }