atnotify.c 780 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <u.h>
  2. #include <libc.h>
  3. #define NFN 33
  4. static int (*onnot[NFN])(void*, char*);
  5. static Lock onnotlock;
  6. static
  7. void
  8. notifier(void *v, char *s)
  9. {
  10. int i;
  11. for(i=0; i<NFN; i++)
  12. if(onnot[i] && ((*onnot[i])(v, s))){
  13. noted(NCONT);
  14. return;
  15. }
  16. noted(NDFLT);
  17. }
  18. int
  19. atnotify(int (*f)(void*, char*), int in)
  20. {
  21. int i, n, ret;
  22. static int init;
  23. if(!init){
  24. notify(notifier);
  25. init = 1; /* assign = */
  26. }
  27. ret = 0;
  28. lock(&onnotlock);
  29. if(in){
  30. for(i=0; i<NFN; i++)
  31. if(onnot[i] == 0) {
  32. onnot[i] = f;
  33. ret = 1;
  34. break;
  35. }
  36. }else{
  37. n = 0;
  38. for(i=0; i<NFN; i++)
  39. if(onnot[i]){
  40. if(ret==0 && onnot[i]==f){
  41. onnot[i] = 0;
  42. ret = 1;
  43. }else
  44. n++;
  45. }
  46. if(n == 0){
  47. init = 0;
  48. notify(0);
  49. }
  50. }
  51. unlock(&onnotlock);
  52. return ret;
  53. }