All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Leonardo Brás" <leobras.c@gmail.com>
To: paulmck@kernel.org, perfbook@vger.kernel.org
Subject: Re: Atomic Limit Counter Add and Subtract, and QQ about goto
Date: Sat, 18 Jan 2025 17:52:00 -0300	[thread overview]
Message-ID: <14e20db14385717ef5bd02468900f5e7afc194bf.camel@gmail.com> (raw)
In-Reply-To: <75e169509708600bc90db4cef897778929c4d4e2.camel@gmail.com>

On Sat, 2025-01-18 at 17:49 -0300, Leonardo Brás wrote:
> Hi Paul,
> 
> I was reading the Counting Chapter in detail, and the QQ about avoiding a Goto
> got my attention:
> 
> Listing 5.13: Atomic Limit Counter Add and Subtract
> Quick Quiz 5.43: Yecch! Why the ugly goto on line 11 of
> Listing 5.13? Haven’t you heard of the break statement???
> 
> And I was thinking on an alternate solution to those, by calling slowpath as a
> function in this simple change:
> 
> ```
> inline int add_count_slowpath(unsigned long delta)
> {
> 	spin_lock(&gblcnt_mutex);
> 	globalize_count();
> 
> 	if (globalcountmax - globalcount - globalreserve < delta) {
> 		flush_local_count();
> 		if (globalcountmax - globalcount - globalreserve < delta) {
> 			spin_unlock(&gblcnt_mutex);
> 			return 0;
> 		}
> 	}
> 
> 	globalcount += delta;
> 	balance_count();
> 	spin_unlock(&gblcnt_mutex);
> 	return 1;
> }
> 
> 
> int add_count(unsigned long delta)
> {
> 	int c;
> 	int cm;
> 	int old;
> 	int new;
> 
> 	do {
> 		split_counterandmax(&counterandmax, &old, &c, &cm);
> 		if (delta > MAX_COUNTERMAX || c + delta > cm)
> 			return add_count_slowpath(delta);
> 		new = merge_counterandmax(c + delta, cm);	
> 	 while (atomic_cmpxchg(&counterandmax, old, new) != old);
> 
> 	return 1;
> 
> }
> ```
> 
> Does it make sense?
> Does it introduce any overhead I couldn't see?
> 
> Thanks!
> Leo
> 
> 

Oh, just to be clear, I like goto, and IMO the above is a perfectly acceptable
way of using goto, and I would not change it. Just thinking about the QQ and
added complexity.

  reply	other threads:[~2025-01-18 20:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-18 20:49 Atomic Limit Counter Add and Subtract, and QQ about goto Leonardo Brás
2025-01-18 20:52 ` Leonardo Brás [this message]
2025-01-19 17:22   ` Paul E. McKenney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=14e20db14385717ef5bd02468900f5e7afc194bf.camel@gmail.com \
    --to=leobras.c@gmail.com \
    --cc=paulmck@kernel.org \
    --cc=perfbook@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.