public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
* RE: FW: sem_getvalue()
@ 2004-09-09 14:12 Blanco Alejandro-EAB005
  2004-09-09 14:26 ` Alexander Terekhov
  0 siblings, 1 reply; 9+ messages in thread
From: Blanco Alejandro-EAB005 @ 2004-09-09 14:12 UTC (permalink / raw)
  To: 'Alexander Terekhov'; +Cc: 'Ross Johnson', pthreads-win32

What's xbd?  I was looking at an appendix for a POSIX spec and it defined it non-optionally.

-----Original Message-----
From: Alexander Terekhov [mailto:TEREKHOV@de.ibm.com] 
Sent: Thursday, September 09, 2004 9:55 AM
To: Blanco Alejandro-EAB005
Cc: 'Ross Johnson'; pthreads-win32@sources.redhat.com
Subject: RE: FW: sem_getvalue()


> "The updated value represents an actual semaphore value..."

Semaphore value is either zero or positive. See XBD. Reporting 
number of waiters as negative value is "optional" and you shall not rely on this feature. As I said, stay away from semas (if you have a choice).

regards,
alexander.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: FW: sem_getvalue()
  2004-09-09 14:12 FW: sem_getvalue() Blanco Alejandro-EAB005
@ 2004-09-09 14:26 ` Alexander Terekhov
  0 siblings, 0 replies; 9+ messages in thread
From: Alexander Terekhov @ 2004-09-09 14:26 UTC (permalink / raw)
  To: Blanco Alejandro-EAB005; +Cc: 'Ross Johnson', pthreads-win32

http://www.opengroup.org/onlinepubs/009695399/basedefs/contents.html

I mean the definition of semaphore lock (and unlock) operation.

See also ("unofficial" aside for a moment):

http://www.pasc.org/interps/unofficial/db/p1003.1/pasc-1003.1-90.html

regards,
alexander.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: FW: sem_getvalue()
  2004-09-09 12:59 Blanco Alejandro-EAB005
@ 2004-09-09 13:55 ` Alexander Terekhov
  0 siblings, 0 replies; 9+ messages in thread
From: Alexander Terekhov @ 2004-09-09 13:55 UTC (permalink / raw)
  To: Blanco Alejandro-EAB005; +Cc: 'Ross Johnson', pthreads-win32

> "The updated value represents an actual semaphore value..."

Semaphore value is either zero or positive. See XBD. Reporting 
number of waiters as negative value is "optional" and you shall
not rely on this feature. As I said, stay away from semas (if
you have a choice).

regards,
alexander.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: FW: sem_getvalue()
@ 2004-09-09 12:59 Blanco Alejandro-EAB005
  2004-09-09 13:55 ` Alexander Terekhov
  0 siblings, 1 reply; 9+ messages in thread
From: Blanco Alejandro-EAB005 @ 2004-09-09 12:59 UTC (permalink / raw)
  To: 'Ross Johnson'; +Cc: pthreads-win32

I got an answer on a different email thread from one of the people who worked on the spec.  The answer I got was that the "or" in the statement was just to deal with the temporal concerns - i.e. that at some time during the call, the number of threads waiting may be changing, so 0 or a negative number are valid, but that the important part of the statement is:

"The updated value represents an actual semaphore value..."

and that the intent was definitely that the number be negative when it is known threads are waiting.  I'm not familiar enough with the code to offer a patch, but is it an easy update to support this?  I looked at where the value is determined, but I could not really figure out whether it was possible.

Alex

-----Original Message-----
From: pthreads-win32-owner@sources.redhat.com [mailto:pthreads-win32-owner@sources.redhat.com] On Behalf Of Ross Johnson
Sent: Thursday, September 09, 2004 5:11 AM
To: pthreads-win32@sources.redhat.com
Subject: Re: FW: sem_getvalue()


The pthreads-win32 implementation never returns a negative 
number, only zero or a positive number (i.e. the actual 
semaphore value as close as can be determined). I read it as 
optional because it could easily be written unambiguously. 
However, it would be worth asking the austin group list for 
a clarification.

Ross

Blanco Alejandro-EAB005 wrote:

>I experienced an unusual return value from sem_getValue(), and traced 
>it back to the actual POSIX spec that is troubling.  I had thought this 
>would return a value < 0 if there are threads waiting on the semaphore, 
>where the absolute value is the number of threads waiting.  I saw that 
>it was returning 0 when I had a thread waiting.  I looked up the spec 
>(http://www.opengroup.org/onlinepubs/009695399/functions/sem_getvalue.h
>tml), and it says:
>
>  
>
>>The sem_getvalue() function shall update the location referenced by 
>>the sval argument to have the value of the semaphore referenced by sem without affecting the state of the semaphore. The updated value represents an actual semaphore value that occurred at some unspecified time during the call, but it need not be the actual value of the semaphore when it is returned to the calling process.
>>    
>>
>
>If sem is locked, then the object to which sval points shall either be 
>set to zero **or** to a negative number whose absolute value represents 
>the number of processes waiting for the semaphore at some unspecified 
>time during the call.
>
>Is it really optional to return the negative number?  Does the 
>implementation, at least sometimes, return 0 only?
>
>Alex Blanco
>
>  
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: FW: sem_getvalue()
  2004-09-08 18:59 Blanco Alejandro-EAB005
  2004-09-08 19:59 ` Alexander Terekhov
@ 2004-09-09  9:11 ` Ross Johnson
  1 sibling, 0 replies; 9+ messages in thread
From: Ross Johnson @ 2004-09-09  9:11 UTC (permalink / raw)
  To: pthreads-win32

The pthreads-win32 implementation never returns a negative 
number, only zero or a positive number (i.e. the actual 
semaphore value as close as can be determined). I read it as 
optional because it could easily be written unambiguously. 
However, it would be worth asking the austin group list for 
a clarification.

Ross

Blanco Alejandro-EAB005 wrote:

>I experienced an unusual return value from sem_getValue(), and traced it back to the actual POSIX spec that is troubling.  I had thought this would return a value < 0 if there are threads waiting on the semaphore, where the absolute value is the number of threads waiting.  I saw that it was returning 0 when I had a thread waiting.  I looked up the spec (http://www.opengroup.org/onlinepubs/009695399/functions/sem_getvalue.html), and it says:
>
>  
>
>>The sem_getvalue() function shall update the location referenced by the sval argument to have the value of the semaphore referenced by sem without affecting the state of the semaphore. The updated value represents an actual semaphore value that occurred at some unspecified time during the call, but it need not be the actual value of the semaphore when it is returned to the calling process.
>>    
>>
>
>If sem is locked, then the object to which sval points shall either be set to zero **or** to a negative number whose absolute value represents the number of processes waiting for the semaphore at some unspecified time during the call.
>
>Is it really optional to return the negative number?  Does the implementation, at least sometimes, return 0 only?
>
>Alex Blanco
>
>  
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: FW: sem_getvalue()
  2004-09-08 20:17 Blanco Alejandro-EAB005
@ 2004-09-08 20:34 ` Alexander Terekhov
  0 siblings, 0 replies; 9+ messages in thread
From: Alexander Terekhov @ 2004-09-08 20:34 UTC (permalink / raw)
  To: Blanco Alejandro-EAB005; +Cc: pthreads-win32

Use condvars and mutexes. Avoid semaphores.

regards,
alexander.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: FW: sem_getvalue()
@ 2004-09-08 20:17 Blanco Alejandro-EAB005
  2004-09-08 20:34 ` Alexander Terekhov
  0 siblings, 1 reply; 9+ messages in thread
From: Blanco Alejandro-EAB005 @ 2004-09-08 20:17 UTC (permalink / raw)
  To: 'Alexander Terekhov', Blanco Alejandro-EAB005; +Cc: pthreads-win32

the case is 1 thread sleeping, 2 threads may wake up the thread.  the wakers check to see if someone is waiting so the sem value is not positive after both wakers' conditions are met.  there is an obvious race condition, but for the application, this is ok.  

regardless, the question is whether there is a defect in that 0 is returned when it should be -1.

alex

-----Original Message-----
From: Alexander Terekhov [mailto:TEREKHOV@de.ibm.com] 
Sent: Wednesday, September 08, 2004 3:59 PM
To: Blanco Alejandro-EAB005
Cc: pthreads-win32@sources.redhat.com
Subject: Re: FW: sem_getvalue()


> 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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: FW: sem_getvalue()
  2004-09-08 18:59 Blanco Alejandro-EAB005
@ 2004-09-08 19:59 ` Alexander Terekhov
  2004-09-09  9:11 ` Ross Johnson
  1 sibling, 0 replies; 9+ messages in thread
From: Alexander Terekhov @ 2004-09-08 19:59 UTC (permalink / raw)
  To: Blanco Alejandro-EAB005; +Cc: 'pthreads-win32@sources.redhat.com'

> 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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* FW: sem_getvalue()
@ 2004-09-08 18:59 Blanco Alejandro-EAB005
  2004-09-08 19:59 ` Alexander Terekhov
  2004-09-09  9:11 ` Ross Johnson
  0 siblings, 2 replies; 9+ messages in thread
From: Blanco Alejandro-EAB005 @ 2004-09-08 18:59 UTC (permalink / raw)
  To: 'pthreads-win32@sources.redhat.com'

I experienced an unusual return value from sem_getValue(), and traced it back to the actual POSIX spec that is troubling.  I had thought this would return a value < 0 if there are threads waiting on the semaphore, where the absolute value is the number of threads waiting.  I saw that it was returning 0 when I had a thread waiting.  I looked up the spec (http://www.opengroup.org/onlinepubs/009695399/functions/sem_getvalue.html), and it says:

> The sem_getvalue() function shall update the location referenced by the sval argument to have the value of the semaphore referenced by sem without affecting the state of the semaphore. The updated value represents an actual semaphore value that occurred at some unspecified time during the call, but it need not be the actual value of the semaphore when it is returned to the calling process.

If sem is locked, then the object to which sval points shall either be set to zero **or** to a negative number whose absolute value represents the number of processes waiting for the semaphore at some unspecified time during the call.

Is it really optional to return the negative number?  Does the implementation, at least sometimes, return 0 only?

Alex Blanco

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2004-09-09 14:26 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-09 14:12 FW: sem_getvalue() Blanco Alejandro-EAB005
2004-09-09 14:26 ` Alexander Terekhov
  -- strict thread matches above, loose matches on Subject: below --
2004-09-09 12:59 Blanco Alejandro-EAB005
2004-09-09 13:55 ` Alexander Terekhov
2004-09-08 20:17 Blanco Alejandro-EAB005
2004-09-08 20:34 ` Alexander Terekhov
2004-09-08 18:59 Blanco Alejandro-EAB005
2004-09-08 19:59 ` Alexander Terekhov
2004-09-09  9:11 ` Ross Johnson

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).