par.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* $Id$ */
  2. /* par.c - Routines to simulate parallelism */
  3. #include "ocm_proc.h"
  4. static void search_next(), DEADLOCK();
  5. void resumenext()
  6. /* Stops the current process, by saving its stack, and determines a new one
  7. * to restart. In case the root of the process tree is passed more then once,
  8. * without a process having done something useful, we'll have a deadlock.
  9. */
  10. {
  11. if (group!=nil) {
  12. register struct process *proc= *group->active;
  13. register wordsize size;
  14. size=top_size(group->s_brk);
  15. proc->stack=alloc((unsigned) size);
  16. if (top_save(size, proc->stack)) {
  17. group->active= &proc->next;
  18. search_next();
  19. } else {
  20. free(proc->stack);
  21. load_betweens();
  22. }
  23. } else
  24. if (++deadlock>1) DEADLOCK();
  25. }
  26. static void search_next()
  27. /* Tries to resume the active process, if this is not possible, the process
  28. * tree will be searched for another process. If the process tree is fully
  29. * traversed, search will restart at the root of the tree.
  30. */
  31. {
  32. while (*group->active==nil && group->up!=nil) {
  33. save_between(group);
  34. group=group->up;
  35. group->active= &(*group->active)->next;
  36. }
  37. if (*group->active==nil) {
  38. if (++deadlock>1) DEADLOCK();
  39. group->active= &group->first;
  40. }
  41. highest_group=group;
  42. while ((*group->active)->down!=nil) {
  43. group=(*group->active)->down;
  44. group->active= &group->first;
  45. }
  46. top_load((*group->active)->stack);
  47. }
  48. void parend()
  49. /* Deletes the current process from its process group and searches for a new
  50. * process to run. The entire group is removed if this is the last process in
  51. * the group, execution then continues with the process that set up this group
  52. * in the first place.
  53. */
  54. {
  55. register struct process *junk;
  56. junk= *group->active;
  57. *group->active=junk->next;
  58. free((void *) junk);
  59. if (group->first==nil) {
  60. register struct procgroup *junk;
  61. delete_between(group);
  62. junk=group;
  63. group=group->up;
  64. free((void *) junk);
  65. if (group!=nil)
  66. (*group->active)->down=nil;
  67. } else {
  68. deadlock=0;
  69. search_next();
  70. }
  71. }
  72. static void DEADLOCK()
  73. {
  74. write(2, "DEADLOCK\n", 9);
  75. abort();
  76. }