destructible.jl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # StarPU --- Runtime system for heterogeneous multicore architectures.
  2. #
  3. # Copyright (C) 2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. #
  5. # StarPU is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation; either version 2.1 of the License, or (at
  8. # your option) any later version.
  9. #
  10. # StarPU is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. #
  14. # See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. #
  16. """
  17. Object used to store a lot of function which must
  18. be applied to and object
  19. """
  20. mutable struct StarpuDestructible{T}
  21. object :: T
  22. destructors :: LinkedList{Function}
  23. end
  24. starpu_block_list = Vector{LinkedList{StarpuDestructible}}()
  25. """
  26. Declares a block of code. Every declared StarpuDestructible in this code
  27. will execute its destructors on its object, once the block is exited
  28. """
  29. macro starpu_block(expr)
  30. quote
  31. starpu_enter_new_block()
  32. local z=$(esc(expr))
  33. starpu_exit_block()
  34. z
  35. end
  36. end
  37. function StarpuDestructible(obj :: T, destructors :: Function...) where T
  38. if (isempty(starpu_block_list))
  39. error("Creation of a StarpuDestructible object while not beeing in a @starpu_block")
  40. end
  41. l = LinkedList{Function}()
  42. for destr in destructors
  43. add_to_tail!(l, destr)
  44. end
  45. output = StarpuDestructible{T}(obj, l)
  46. add_to_head!(starpu_block_list[end], output)
  47. return output
  48. end
  49. function starpu_enter_new_block()
  50. push!(starpu_block_list, LinkedList{StarpuDestructible}())
  51. end
  52. function starpu_destruct!(x :: StarpuDestructible)
  53. @foreach_asc x.destructors destr begin
  54. destr.data(x.object)
  55. end
  56. empty!(x.destructors)
  57. return nothing
  58. end
  59. function starpu_exit_block()
  60. destr_list = pop!(starpu_block_list)
  61. @foreach_asc destr_list x begin
  62. starpu_destruct!(x.data)
  63. end
  64. end
  65. """
  66. Adds new destructors to the list of function. They will be executed before
  67. already stored ones when calling starpu_destruct!
  68. """
  69. function starpu_add_destructor!(x :: StarpuDestructible, destrs :: Function...)
  70. for d in destrs
  71. add_to_head!(x.destructors, d)
  72. end
  73. return nothing
  74. end
  75. """
  76. Removes detsructor without executing it
  77. """
  78. function starpu_remove_destructor!(x :: StarpuDestructible, destr :: Function)
  79. @foreach_asc x.destructors lnk begin
  80. if (lnk.data == destr)
  81. remove_link!(lnk)
  82. break
  83. end
  84. end
  85. return nothing
  86. end
  87. """
  88. Executes "destr" function. If it was one of the stored destructors, it
  89. is removed.
  90. This function can be used to allow user to execute a specific action manually
  91. (ex : explicit call to starpu_data_unpartition() without unregistering)
  92. """
  93. function starpu_execute_destructor!(x :: StarpuDestructible, destr :: Function)
  94. starpu_remove_destructor!(x, destr)
  95. return destr(x.object)
  96. end