atexit.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 NEXIT 33
  12. typedef struct Onex Onex;
  13. struct Onex{
  14. void (*f)(void);
  15. int pid;
  16. };
  17. static Lock onexlock;
  18. Onex onex[NEXIT];
  19. int
  20. atexit(void (*f)(void))
  21. {
  22. int i;
  23. lock(&onexlock);
  24. for(i=0; i<NEXIT; i++)
  25. if(onex[i].f == 0) {
  26. onex[i].pid = getpid();
  27. onex[i].f = f;
  28. unlock(&onexlock);
  29. return 1;
  30. }
  31. unlock(&onexlock);
  32. return 0;
  33. }
  34. void
  35. atexitdont(void (*f)(void))
  36. {
  37. int i, pid;
  38. pid = getpid();
  39. for(i=0; i<NEXIT; i++)
  40. if(onex[i].f == f && onex[i].pid == pid)
  41. onex[i].f = 0;
  42. }
  43. void
  44. exits(char *s)
  45. {
  46. int i, pid;
  47. void (*f)(void);
  48. pid = getpid();
  49. for(i = NEXIT-1; i >= 0; i--)
  50. if((f = onex[i].f) && pid == onex[i].pid) {
  51. onex[i].f = 0;
  52. (*f)();
  53. }
  54. _exits(s);
  55. }