Skip to content

Commit ae7de4b

Browse files
committed
Drop the Tx API for simplicity
1 parent 8ff3677 commit ae7de4b

29 files changed

+264
-956
lines changed

README.md

Lines changed: 114 additions & 174 deletions
Large diffs are not rendered by default.

src/kcas.ml

Lines changed: 0 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -300,154 +300,6 @@ module Op = struct
300300
else run (CASN (loc, { before; after; casn }, NIL, NIL)) rest
301301
end
302302

303-
let update_as0 g loc f casn post_commit l r =
304-
let state = Atomic.get loc.state in
305-
let before = eval state in
306-
let after = f before in
307-
let state =
308-
if before == after && is_obstruction_free casn then state
309-
else { before; after; casn }
310-
in
311-
((casn, CASN (loc, state, l, r), post_commit), g before)
312-
[@@inline]
313-
314-
let update_as g loc f casn post_commit state' l r =
315-
let state = Obj.magic state' in
316-
if is_cmp casn state then
317-
let before = eval state in
318-
let after = f before in
319-
let state = if before == after then state else { before; after; casn } in
320-
((casn, CASN (loc, state, l, r), post_commit), g before)
321-
else
322-
let current = state.after in
323-
( (casn, CASN (loc, { state with after = f current }, l, r), post_commit),
324-
g current )
325-
[@@inline]
326-
327-
let update_as g loc f (casn, cass, post_commit) =
328-
let x = loc.id in
329-
match cass with
330-
| NIL -> update_as0 g loc f casn post_commit NIL NIL
331-
| CASN (a, _, NIL, _) as cass when x < a.id ->
332-
update_as0 g loc f casn post_commit NIL cass
333-
| CASN (a, _, _, NIL) as cass when a.id < x ->
334-
update_as0 g loc f casn post_commit cass NIL
335-
| CASN (loc', state', l, r) when Obj.magic loc' == loc ->
336-
update_as g loc f casn post_commit state' l r
337-
| _ -> (
338-
match splay ~hit_parent:false x cass with
339-
| l, Miss, r -> update_as0 g loc f casn post_commit l r
340-
| l, Hit (_loc', state'), r ->
341-
update_as g loc f casn post_commit state' l r)
342-
[@@inline]
343-
344-
let attempt (mode : Mode.t) tx =
345-
let casn = Atomic.make (mode :> status) in
346-
match tx (casn, NIL, Action.noop) with
347-
| (_, NIL, post_commit), result -> Action.run post_commit result
348-
| (_, CASN (loc, state, NIL, NIL), post_commit), result ->
349-
if is_cmp casn state then Action.run post_commit result
350-
else
351-
let before = state.before in
352-
state.before <- state.after;
353-
if cas loc before state then Action.run post_commit result else exit ()
354-
| (_, cass, post_commit), result ->
355-
if determine_for_owner casn cass then Action.run post_commit result
356-
else exit ()
357-
358-
let rec commit backoff mode tx =
359-
match attempt mode tx with
360-
| result -> result
361-
| exception Mode.Interference ->
362-
let backoff = Backoff.once backoff in
363-
commit backoff Mode.lock_free tx
364-
| exception Exit ->
365-
let backoff = Backoff.once backoff in
366-
commit backoff mode tx
367-
368-
module Tx = struct
369-
type log = casn * cass * Action.t
370-
type 'a t = log -> log * 'a
371-
372-
let get loc log = update_as Fun.id loc Fun.id log
373-
let get_as f loc log = update_as f loc Fun.id log
374-
let set loc after log = update_as ignore loc (fun _ -> after) log
375-
let update loc f log = update_as Fun.id loc f log
376-
let modify loc f log = update_as ignore loc f log
377-
378-
let compare_and_swap loc before after log =
379-
update_as Fun.id loc
380-
(fun actual -> if actual == before then after else actual)
381-
log
382-
383-
let exchange_as g loc after log = update_as g loc (fun _ -> after) log
384-
let exchange loc after log = update_as Fun.id loc (fun _ -> after) log
385-
let fetch_and_add loc n log = update_as Fun.id loc (( + ) n) log
386-
let incr loc log = update_as ignore loc inc log
387-
let decr loc log = update_as ignore loc dec log
388-
let update_as g loc f log = update_as g loc f log
389-
390-
let swap l1 l2 log =
391-
let log, x1 = get l1 log in
392-
let log, x2 = exchange l2 x1 log in
393-
set l1 x2 log
394-
395-
let return value log = (log, value)
396-
let delay uxt log = uxt () log
397-
398-
let post_commit action (casn, cass, post_commit) =
399-
((casn, cass, Action.append action post_commit), ())
400-
401-
let is_in_log loc ((casn, cass, post_commit) as log) =
402-
let x = loc.id in
403-
match cass with
404-
| NIL -> (log, false)
405-
| CASN (a, _, NIL, _) when x < a.id -> (log, false)
406-
| CASN (a, _, _, NIL) when a.id < x -> (log, false)
407-
| CASN (a, _, _, _) when Obj.magic a == loc -> (log, true)
408-
| cass -> (
409-
match splay ~hit_parent:true loc.id cass with
410-
| l, Hit (a, s), r ->
411-
((casn, CASN (a, s, l, r), post_commit), Obj.magic a == loc)
412-
| _, Miss, _ -> impossible ())
413-
414-
let ( let* ) xt xyt log =
415-
let log, x = xt log in
416-
xyt x log
417-
418-
let ( and* ) xt yt log =
419-
let log, x = xt log in
420-
let log, y = yt log in
421-
(log, (x, y))
422-
423-
let ( let+ ) xt xy log =
424-
let log, x = xt log in
425-
(log, xy x)
426-
427-
let ( and+ ) = ( and* )
428-
429-
let ( >> ) ut xt log =
430-
let log, _ = ut log in
431-
xt log
432-
433-
let ( >>. ) ut x log =
434-
let log, _ = ut log in
435-
(log, x)
436-
437-
let ( >>= ) = ( let* )
438-
let map xy xt = ( let+ ) xt xy
439-
440-
let try_in eyt xyt xt log =
441-
match xt log with log, x -> xyt x log | exception e -> eyt e log
442-
443-
let ( <|> ) lhs rhs log = try lhs log with Exit -> rhs log
444-
let forget = exit
445-
let attempt ?(mode = Mode.lock_free) tx = attempt mode tx
446-
447-
let commit ?(backoff = Backoff.default) ?(mode = Mode.obstruction_free) tx =
448-
commit backoff mode tx
449-
end
450-
451303
module Xt = struct
452304
type 'x t = {
453305
casn : casn;
@@ -573,15 +425,4 @@ module Xt = struct
573425
[@@inline]
574426

575427
let attempt ?(mode = Mode.lock_free) tx = attempt mode tx.tx [@@inline]
576-
577-
let of_tx tx ~xt =
578-
let (_, cass, post_commit), x = tx (xt.casn, xt.cass, xt.post_commit) in
579-
xt.cass <- cass;
580-
xt.post_commit <- post_commit;
581-
x
582-
583-
let to_tx tx (casn, cass, post_commit) =
584-
let xt = { casn; cass; post_commit } in
585-
let x = tx.tx ~xt in
586-
((xt.casn, xt.cass, xt.post_commit), x)
587428
end

0 commit comments

Comments
 (0)