aboutsummaryrefslogtreecommitdiffstats
path: root/lib/kernel/Lifecycle.ml
blob: 772cb6eafaf752a04bd4b50ab5c24bced4f30615 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class type lifecycle = object
  method reference : bool
  method release : unit
end

class type virtual controlled_lifecycle = object
  inherit lifecycle
  method virtual destroy : unit
end

class null = object
  method reference = true
  method release = ()
end

class virtual counted = object (self)
  val references = Atomic.make 1

  method virtual destroy : unit

  method reference =
    Atomic.incr references;
    true

  method release =
    if Atomic.fetch_and_add references (-1) = 1 then
      self#destroy
end

class retaining inner (owner : lifecycle) = object
  inherit counted
  initializer ignore owner#reference

  method destroy =
    Protocols.Handle.release inner;
    owner#release

  method hash = Protocols.Handle.hash inner
  method protocols = Protocols.Handle.protocols inner
end