Clojure

Remove birth-thread check from transients

Details

  • Type: Enhancement Enhancement
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Completed
  • Affects Version/s: Release 1.6
  • Fix Version/s: Release 1.7
  • Component/s: None
  • Patch:
    Code
  • Approval:
    Ok

Description

Transients protect themselves from use by any thread other than the one that creates them. This is good for safety, however it eliminates certain valid usages of transients. For example, usage in a go-block might occur in subsequent invocations across multiple OS threads (but only one logical thread of control).

Current simple test:

user> (def v (transient []))
#'user/v
user> (persistent! @(future (conj! v 1)))
IllegalAccessError Transient used by non-owner thread  clojure.lang.PersistentVector$TransientVector.ensureEditable (PersistentVector.java:464)

Proposal: Remove the owner check from transient collections. (Leave the edit after persistent check as is.) The test above should succeed.

After:

user=> (def v (transient []))
#'user/v
user=> (persistent! @(future (conj! v 1)))
[1]

The clj-1498-3.diff version of the patch also replaces the AtomicReference<Thread> with AtomicBoolean as we can now track just ownership, not who owns it.

Doc update: Various pieces of documentation will need to be updated with this change, namely http://clojure.org/transients

Patch: clj-1498-3.diff

Alternative: Another idea would be to make this check optional with some kind of option on the transient call (transient coll :check-owner true). Not sure whether what the default would be for that.

  1. clj-1498.diff
    08/Aug/14 11:55 AM
    2 kB
    Alex Miller
  2. clj-1498-2.diff
    13/Aug/14 1:42 PM
    16 kB
    Alex Miller
  3. clj-1498-3.diff
    20/Aug/14 8:59 AM
    20 kB
    Alex Miller

Activity

Hide
Jozef Wagner added a comment -

I suggest to add a functionality to pass ownership of a transient to the different thread, or to release the ownership by passing nil.

user=> (def v (pass! (transient []) nil))
#'user/v
user=> (persistent! @(future (conj! v 1)))
[1]

pass! has to be called by current owner thread, or by any thread if the transient is currently released.

Show
Jozef Wagner added a comment - I suggest to add a functionality to pass ownership of a transient to the different thread, or to release the ownership by passing nil.
user=> (def v (pass! (transient []) nil))
#'user/v
user=> (persistent! @(future (conj! v 1)))
[1]
pass! has to be called by current owner thread, or by any thread if the transient is currently released.
Hide
Alex Miller added a comment -

New patch that replaces AtomicReference<Thread> with AtomicBoolean.

Show
Alex Miller added a comment - New patch that replaces AtomicReference<Thread> with AtomicBoolean.
Hide
Stuart Halloway added a comment -

Alex, can you please expand the example test you provided to a generative test that covers the following combinations:

  1. different collection sizes (above and below the ArrayMap size boundary)
  2. different shapes (vector vs. map)
  3. successful use across threads (positive use case this ticket enables)

data_structures.clj has helpers for generating transient interactions that you can build on.

Show
Stuart Halloway added a comment - Alex, can you please expand the example test you provided to a generative test that covers the following combinations:
  1. different collection sizes (above and below the ArrayMap size boundary)
  2. different shapes (vector vs. map)
  3. successful use across threads (positive use case this ticket enables)
data_structures.clj has helpers for generating transient interactions that you can build on.
Hide
Alex Miller added a comment -

Enhanced existing generative tests to test random actions against sets, vectors, and both PHM and PAM. Added additional actions to do transient modification actions in other threads as well as originating thread.

Show
Alex Miller added a comment - Enhanced existing generative tests to test random actions against sets, vectors, and both PHM and PAM. Added additional actions to do transient modification actions in other threads as well as originating thread.

People

Vote (0)
Watch (3)

Dates

  • Created:
    Updated:
    Resolved: