ps.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. int
  15. nprocs(void)
  16. {
  17. return procalloc.nproc;
  18. }
  19. void
  20. pshash(Proc *p)
  21. {
  22. int h;
  23. h = p->pid % nelem(procalloc.ht);
  24. lock(&procalloc.l);
  25. p->pidhash = procalloc.ht[h];
  26. procalloc.ht[h] = p;
  27. unlock(&procalloc.l);
  28. }
  29. void
  30. psunhash(Proc *p)
  31. {
  32. int h;
  33. Proc **l;
  34. h = p->pid % nelem(procalloc.ht);
  35. lock(&procalloc.l);
  36. for(l = &procalloc.ht[h]; *l != nil; l = &(*l)->pidhash)
  37. if(*l == p){
  38. *l = p->pidhash;
  39. break;
  40. }
  41. unlock(&procalloc.l);
  42. }
  43. int
  44. psindex(int pid)
  45. {
  46. Proc *p;
  47. int h;
  48. int s;
  49. s = -1;
  50. h = pid % nelem(procalloc.ht);
  51. lock(&procalloc.l);
  52. for(p = procalloc.ht[h]; p != nil; p = p->pidhash)
  53. if(p->pid == pid){
  54. s = p->index;
  55. break;
  56. }
  57. unlock(&procalloc.l);
  58. return s;
  59. }
  60. Proc*
  61. psincref(int i)
  62. {
  63. /*
  64. * Placeholder.
  65. */
  66. if(i >= conf.nproc)
  67. return nil;
  68. return &procalloc.arena[i];
  69. }
  70. void
  71. psdecref(Proc *p)
  72. {
  73. /*
  74. * Placeholder.
  75. */
  76. USED(p);
  77. }
  78. void
  79. psrelease(Proc* p)
  80. {
  81. p->qnext = procalloc.free;
  82. procalloc.free = p;
  83. procalloc.nproc--;
  84. }
  85. Proc*
  86. psalloc(void)
  87. {
  88. Proc *p;
  89. lock(&procalloc.l);
  90. for(;;) {
  91. if((p = procalloc.free) != nil)
  92. break;
  93. unlock(&procalloc.l);
  94. resrcwait("no procs");
  95. lock(&procalloc.l);
  96. }
  97. procalloc.free = p->qnext;
  98. procalloc.nproc++;
  99. unlock(&procalloc.l);
  100. return p;
  101. }
  102. void
  103. psinit(int nproc)
  104. {
  105. Proc *p;
  106. int i;
  107. procalloc.free = malloc(nproc*sizeof(Proc));
  108. if(procalloc.free == nil)
  109. panic("cannot allocate %u procs (%uMB)\n", nproc, nproc*sizeof(Proc)/(1024*1024));
  110. procalloc.arena = procalloc.free;
  111. p = procalloc.free;
  112. for(i=0; i<nproc-1; i++,p++){
  113. p->qnext = p+1;
  114. p->index = i;
  115. }
  116. p->qnext = 0;
  117. p->index = i;
  118. }