atnotify.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <libc.h>
  11. #define NFN 33
  12. static int (*onnot[NFN])(void*, char*);
  13. static Lock onnotlock;
  14. static
  15. void
  16. notifier(void *v, char *s)
  17. {
  18. int i;
  19. for(i=0; i<NFN; i++)
  20. if(onnot[i] && ((*onnot[i])(v, s))){
  21. noted(NCONT);
  22. return;
  23. }
  24. noted(NDFLT);
  25. }
  26. int
  27. atnotify(int (*f)(void*, char*), int in)
  28. {
  29. int i, n, ret;
  30. static int init;
  31. if(!init){
  32. notify(notifier);
  33. init = 1;
  34. }
  35. ret = 0;
  36. lock(&onnotlock);
  37. if(in){
  38. for(i=0; i<NFN; i++)
  39. if(onnot[i] == 0) {
  40. onnot[i] = f;
  41. ret = 1;
  42. break;
  43. }
  44. }else{
  45. n = 0;
  46. for(i=0; i<NFN; i++)
  47. if(onnot[i]){
  48. if(ret==0 && onnot[i]==f){
  49. onnot[i] = 0;
  50. ret = 1;
  51. }else
  52. n++;
  53. }
  54. if(n == 0){
  55. init = 0;
  56. notify(0);
  57. }
  58. }
  59. unlock(&onnotlock);
  60. return ret;
  61. }