box-extra:   box utilities
1 Reference
box-update-strategy/  c
box-update-proc/  c
make-box-update-proc
box-update/  busy!
box-update/  semaphore!
1.1 Unsafe API
make-unsafe-box-update-proc
unsafe-box-update/  busy!
unsafe-box-update/  semaphore!
8.2

box-extra: box utilities

Bogdan Popa <bogdan@defn.io>

Extra utilities for working with boxes.

1 Reference

value

box-update-strategy/c : (or/c 'busy 'semaphore)

value

box-update-proc/c : (-> (-> any/c any/c) any/c)

procedure

(make-box-update-proc b [strategy])  box-update-proc/c

  b : box?
  strategy : box-update-strategy/c = 'semaphore
Returns a procedure that may be used to update the value within b using the given retry strategy.

The 'semaphore strategy synchronizes retrying threads using a semaphore. See box-update/semaphore! for details.

The 'busy strategy retries without any sort of backoff. See box-update/busy! for details.

Examples:
> (require box-extra)
> (define b (box 0))
> (define update-b! (make-box-update-proc b))
> (update-b! add1)

1

> (unbox b)

1

procedure

(box-update/busy! b proc)  any/c

  b : box?
  proc : (-> any/c any/c)
Updates b by applying proc to its current value. When box-cas! is fails, it retries immediately. Returns the value that was last put in the box.

Prefer box-update/semaphore! over this function. Only use this function if you’re sure that multiple threads won’t attempt to update b concurrently.

procedure

(box-update/semaphore! b sema proc)  any/c

  b : box?
  sema : semaphore?
  proc : (-> any/c any/c)
Updates b by applying proc to its current value. When box-cas! fails, it waits on sema before retrying. Returns the value that was last put in the box.

The sema argument must have an initial count of 1.

Examples:
> (require box-extra)
> (define b (box 0))
> (define sema (make-semaphore 1))
> (define thds
    (list
     (thread (λ () (box-update/semaphore! b sema add1)))
     (thread (λ () (box-update/semaphore! b sema add1)))))
> (for-each thread-wait thds)
> (unbox b)

2

1.1 Unsafe API

 (require box-extra/unsafe) package: box-extra-lib

procedure

(make-unsafe-box-update-proc b [strategy])  box-update-proc/c

  b : box?
  strategy : box-update-strategy/c = 'semaphore

procedure

(unsafe-box-update/busy! b proc)  any/c

  b : box?
  proc : (-> any/c any/c)

procedure

(unsafe-box-update/semaphore! b sema proc)  any/c

  b : box?
  sema : semaphore?
  proc : (-> any/c any/c)
Unsafe variants of make-box-update-proc, box-update/busy! and box-update/semaphore!, respectively.