public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
From: Alexander Terekhov <TEREKHOV@de.ibm.com>
To: Blanco Alejandro-EAB005 <Alex.Blanco@motorola.com>
Cc: "'pthreads-win32@sources.redhat.com'"
	<pthreads-win32@sources.redhat.com>
Subject: Re: FW: sem_getvalue()
Date: Wed, 08 Sep 2004 19:59:00 -0000	[thread overview]
Message-ID: <OF8868FE33.7F2DC138-ONC1256F09.006B925D-C1256F09.006DC69C@de.ibm.com> (raw)
In-Reply-To: <31173C0B4EF5D611A021009027CB2CBD04EE2CC6@fl08exm04>

> if there are threads waiting on the semaphore

That's pretty useless info. Consider "fast" semaphore using "lock 
queue" (Linux folks reinvented "half-or-less" of this thing and 
proudly called it "a futex") address based parking interface with 
"waiters bit" maintained by its implementation:

sema_lock:

  WHILE // CAS/LL-SC
    !atomic_decrement_if_binand_7FFFFFFF_is_not_zero_ccacq(&lock) 
      lock_queue_wait(&lock, 0) // wait if sem.value is zero

sema_unlock:

  uintptr_t lock_queue;
  IF atomic_increment_rel(lock_queue = &lock) > 0x80000000
    THEN lock_queue_wake(lock_queue, 1)

(try/timed operations omitted for brevity)

BTW, fast mutex:

mutex_lock:

  WHILE 
    atomic_bit_test_set_ccacq(&lock, 1) 
      lock_queue_wait(&lock, 1) // wait if locked bit is set

mutex_unlock:

  uintptr_t lock_queue;
  IF atomic_decrement_rel(lock_queue = &lock)
    THEN lock_queue_wake(lock_queue, 1) 

"ccacq" stands for acquire-but-with-"control condition"-hint
(to avoid redundant memory barrier(s) -- arch specific stuff).

"rel" is a classic release.

regards,
alexander.

  reply	other threads:[~2004-09-08 19:59 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-09-08 18:59 Blanco Alejandro-EAB005
2004-09-08 19:59 ` Alexander Terekhov [this message]
2004-09-09  9:11 ` Ross Johnson
2004-09-08 20:17 Blanco Alejandro-EAB005
2004-09-08 20:34 ` Alexander Terekhov
2004-09-09 12:59 Blanco Alejandro-EAB005
2004-09-09 13:55 ` Alexander Terekhov
2004-09-09 14:12 Blanco Alejandro-EAB005
2004-09-09 14:26 ` Alexander Terekhov

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=OF8868FE33.7F2DC138-ONC1256F09.006B925D-C1256F09.006DC69C@de.ibm.com \
    --to=terekhov@de.ibm.com \
    --cc=Alex.Blanco@motorola.com \
    --cc=pthreads-win32@sources.redhat.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).