Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log: implement set_always_flush/1 for log.Log, log.ThreadSafeLog and log.Logger #20698

Merged
merged 3 commits into from
Feb 1, 2024

Conversation

spytheman
Copy link
Member

No description provided.

pub fn (mut x ThreadSafeLog) set_always_flush(should_flush bool) {
x.mu.@lock()
x.Log.set_always_flush(should_flush)
x.mu.unlock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be a defer x.mu.unlock()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be a defer x.mu.unlock()?

why? there no 2 return's anyway

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The execution of the function doesn't happen linearly when concurrency is used. defer fixes that here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The execution of the function doesn't happen linearly when concurrency is used. defer fixes that here.

  • Current version:
import sync

struct Abc {
mut:
	mu sync.Mutex
}

fn (mut x Abc) f() {
	x.mu.@lock()
	println('foo')
	x.mu.unlock()
}

fn main() {
	mut y := Abc{}
	y.f()
}

C output:

void main__Abc_f(main__Abc* x) {
	sync__Mutex_lock(&x->mu);
	println(_SLIT("foo"));
	sync__Mutex_unlock(&x->mu);
}
  • Your version:
fn (mut x Abc) f() {
	x.mu.@lock()
	defer { x.mu.unlock() }
	println('foo')
}

Output:

void main__Abc_f(main__Abc* x) {
	bool main__Abc_f_defer_0 = false;
	sync__Mutex_lock(&x->mu);
	main__Abc_f_defer_0 = true;
	println(_SLIT("foo"));
	// Defer begin
	if (main__Abc_f_defer_0) {
		sync__Mutex_unlock(&x->mu);
	}
	// Defer end
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The execution of the function doesn't happen linearly when concurrency is used. defer fixes that here.

No, it happens in order. The mutex lock and unlock are here, to protect multiple threads from executing the code in between them. They ensure that only one thread (the one that succeed in locking the mutex), will continue. All the other will block.
Then one of the other threads will continue, while others continue to block etc.

I am not using defer here, since that will use 3 lines for the defer block, not just 1, and since the body is a single line -> I think that the version with defer will be less clear to read.

@spytheman spytheman merged commit 68bd9a9 into vlang:master Feb 1, 2024
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants