Semaphore-count

59 views
Skip to first unread message

Craig Allen

unread,
Sep 4, 2018, 7:47:46 AM9/4/18
to Racket Users
So I have this snippet pinched from Rosetta Code:

(define-syntax-rule (define/atomic (name arg ...) body ...)
  (define name
    (let ([sema (make-semaphore 1)])
      (lambda (arg ...)
        (dynamic-wind (lambda () (semaphore-wait sema))
                      (lambda () body ...)
                      (lambda () (semaphore-post sema)))))))

But I would like to be able to ask whether I will have to wait, before I am in fact waiting (so I can print a message). I notice the fsemaphore package exports semaphore-count, but semaphore does not. Is this an oversight, or is there a better way to do this?

Thanks,

Craig

Matthew Flatt

unread,
Sep 4, 2018, 8:17:09 AM9/4/18
to Craig Allen, Racket Users
There's no `semaphore-count`, but you can use `semaphore-try-wait?` to
poll a semaphore instead of blocking.

Craig Allen

unread,
Sep 4, 2018, 8:31:39 AM9/4/18
to Racket Users
I saw that function, but was scared off by its documentation:

Like semaphore-wait, but semaphore-try-wait? never blocks execution. If sema’s internal counter is zero, semaphore-try-wait? returns #f immediately without decrementing the counter. If sema’s counter is positive, it is decremented and #t is returned.

I'm not sure why I'd want it to decrement the count, I really just want to see if my critical section is running. I will try it though, thanks.

George Neuner

unread,
Sep 4, 2018, 11:47:02 AM9/4/18
to Craig Allen, racket users

On 9/4/2018 8:31 AM, Craig Allen wrote:
I saw that function, but was scared off by its documentation:

Like semaphore-wait, but semaphore-try-wait? never blocks execution. If sema’s internal counter is zero, semaphore-try-wait? returns #f immediately without decrementing the counter. If sema’s counter is positive, it is decremented and #t is returned.

I'm not sure why I'd want it to decrement the count, I really just want to see if my critical section is running. I will try it though, thanks.


'wait' takes the semaphore when it is available.  'try-wait' takes the semaphore if possible, or fails immediately if it can't. 

AFAIK there is no way to simply 'check' if the semaphore is available.  In a multithread environment, such checking doesn't do much good anyway ... a positive counter value doesn't mean you actually can take a semaphore - some other thread(s) may beat you to it.

The canon [language independent] methods of "optimistic" locking are either to spin until 'try' returns true, or to combine one or a few initial probes using 'try' with a fall back to a normal wait if the probes fail.

George

Jos Koot

unread,
Sep 5, 2018, 9:38:47 AM9/5/18
to George Neuner, Craig Allen, racket users
Hi Craig Allen
Would something like the following work?
Jos
 
(define (make-sema n)
 (define-syntax-rule (with-counter-sema expr ...)
  (begin
   (semaphore-wait counter-sema)
   (define result (begin expr ...))
   (semaphore-post counter-sema)
   result))
 (define counter-sema (make-semaphore 1))
 (define counter n)
 (define sema (make-semaphore n))
 (define (wait) (with-counter-sema (semaphore-wait sema) (set! n (sub1 n))))
 (define (post) (with-counter-sema (semaphore-post sema) (set! n (add1 n))))
 (define (count) (with-counter-sema n))
 (values wait post count))
 
(define-values (wait post count) (make-sema 3))
(count)
(wait)
(count)
(post)
(count)


From: racket...@googlegroups.com [mailto:racket...@googlegroups.com] On Behalf Of George Neuner
Sent: 04 September 2018 17:47
To: Craig Allen
Cc: racket users
Subject: Re: [racket-users] Semaphore-count

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
racket-users...@googlegroups.com.
For more options, visit
https://groups.google.com/d/optout.

George Neuner

unread,
Sep 5, 2018, 10:25:43 PM9/5/18
to Craig Allen, Racket Users

On 9/4/2018 11:46 AM, George Neuner wrote:
>
> AFAIK there is no way to simply 'check' if the semaphore is available.
>

I was wrong - there is a way using events: semaphore-peek-evt  can check
whether a semaphore is ready without affecting its counter.

https://docs.racket-lang.org/reference/semaphore.html?q=semaphore-peek#%28def._%28%28quote._~23~25kernel%29._semaphore-peek-evt%29%29


George

Jos Koot

unread,
Sep 6, 2018, 8:57:38 AM9/6/18
to George Neuner, Craig Allen, racket users
Ignore my email on this subject, please.
It is wrong.
Jos

Craig Allen

unread,
Sep 7, 2018, 5:59:40 PM9/7/18
to Racket Users
Thanks, I will look into this.
Reply all
Reply to author
Forward
0 new messages