ctask.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. {
  2. This unit implements basic task handling for unit and package handling
  3. Copyright (c) 2005 by Florian Klaempfl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ctask;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses;
  22. type
  23. tabstracttask = class;
  24. ttasklistitem = class(TLinkedListItem);
  25. task : tabstracttask;
  26. constructor create(p : ttabstracttask);
  27. end;
  28. tabstracttask = class
  29. lastchecked : aint;
  30. dependson : tlinkedlist;
  31. requiredby : tlinkedlist;
  32. destructor destroy;override;
  33. end;
  34. ttaskqueue = class
  35. run : aint;
  36. tasks : tlinkedlist;
  37. destructor destroy;override;
  38. procedure addtask(p : tabstracttask);
  39. procedure removetask(p : tabstracttask);
  40. procedure adddependency(p : tabstracttask;requires : tabstracttask);
  41. procedure markasdone(p : tabstracttask);
  42. procedure finished(p : tabstracttask);
  43. { searches for the next task to execute }
  44. function searchdoabletask : tabstracttask;
  45. end;
  46. implementation
  47. constructor ttasklistitem.create(p : ttabstracttask);
  48. begin
  49. inherited create;
  50. task:=p;
  51. end;
  52. destructor ttaskqueue.destroy;
  53. begin
  54. dependson.free;
  55. requiredby.free;
  56. inherited destroy;
  57. end;
  58. destructor ttaskqueue.destroy;
  59. begin
  60. tasks.free;
  61. inherited destroy;
  62. end;
  63. procedure ttaskqueue.addtask(p : tabstracttask);
  64. begin
  65. tasks.add(ttasklistitem.create(p));
  66. end;
  67. procedure ttaskqueue.tasktoitem(p : tabstracttask) : ttasklistitem;
  68. var
  69. hp : ttasklistitem;
  70. begin
  71. hp:=ttasklistitem(tasks.getfirst);
  72. while assigned(hp) do
  73. begin
  74. if hp.task=p then
  75. begin
  76. result:=hp.task;
  77. exit;
  78. end;
  79. hp:=ttasklistitem(hp.next);
  80. end;
  81. internalerror(2005052901);
  82. end;
  83. procedure ttaskqueue.removetask(p : tabstracttask);
  84. begin
  85. tasks.remove(tasktoitem(p));
  86. end;
  87. procedure ttaskqueue.markasdone(p : tabstracttask);
  88. begin
  89. { sanity check }
  90. if not(dependson.empty) then
  91. internalerror(2005052902);
  92. { walk through all tasks depending on the current one }
  93. !!!!
  94. removetask(p);
  95. end;
  96. procedure ttaskqueue.finished(p : tabstracttask);
  97. begin
  98. markasdone(p);
  99. p.free;
  100. end;
  101. function ttaskqueue.searchdoabletask : tabstracttask;
  102. var
  103. hp : ttasklistitem;
  104. begin
  105. inc(run);
  106. hp:=ttasklistitem(tasks.getfirst);
  107. while assigned(hp) do
  108. begin
  109. if hp.task.dependson.empty then
  110. begin
  111. result:=hp.task;
  112. exit;
  113. end;
  114. { did we touch this task already? }
  115. if hp.task.run=run then
  116. begin
  117. result:=nil;
  118. exit;
  119. end;
  120. { tag current task }
  121. hp.task.run:=run;
  122. next:=hp.next;
  123. { move task to the end of the queue }
  124. tasks.remove(hp);
  125. tasks.concat(hp);
  126. hp:=next;
  127. end;
  128. end;
  129. end.