iocall.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #include <thread.h>
  12. #include "threadimpl.h"
  13. int32_t
  14. iocall(Ioproc *io, int32_t (*op)(va_list*), ...)
  15. {
  16. int ret, inted;
  17. Ioproc *msg;
  18. if(send(io->c, &io) == -1){
  19. werrstr("interrupted");
  20. return -1;
  21. }
  22. assert(!io->inuse);
  23. io->inuse = 1;
  24. io->op = op;
  25. va_start(io->arg, op);
  26. msg = io;
  27. inted = 0;
  28. while(send(io->creply, &msg) == -1){
  29. msg = nil;
  30. inted = 1;
  31. }
  32. if(inted){
  33. werrstr("interrupted");
  34. return -1;
  35. }
  36. /*
  37. * If we get interrupted, we have to stick around so that
  38. * the IO proc has someone to talk to. Send it an interrupt
  39. * and try again.
  40. */
  41. inted = 0;
  42. while(recv(io->creply, nil) == -1){
  43. inted = 1;
  44. iointerrupt(io);
  45. }
  46. USED(inted);
  47. va_end(io->arg);
  48. ret = io->ret;
  49. if(ret < 0)
  50. errstr(io->err, sizeof io->err);
  51. io->inuse = 0;
  52. /* release resources */
  53. while(send(io->creply, &io) == -1)
  54. ;
  55. return ret;
  56. }