_cffi_src.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "pyversion_compat.h"
  4. #include "mutex.h"
  5. #include "ipcmaxlen.h"
  6. #include "zmq_compat.h"
  7. #include <zmq.h>
  8. typedef struct _zhint {
  9. void *sock;
  10. mutex_t *mutex;
  11. size_t id;
  12. } zhint;
  13. void free_python_msg(void *data, void *vhint) {
  14. zmq_msg_t msg;
  15. zhint *hint = (zhint *)vhint;
  16. int rc;
  17. if (hint != NULL) {
  18. zmq_msg_init_size(&msg, sizeof(size_t));
  19. memcpy(zmq_msg_data(&msg), &hint->id, sizeof(size_t));
  20. rc = mutex_lock(hint->mutex);
  21. if (rc != 0) {
  22. fprintf(stderr, "pyzmq-gc mutex lock failed rc=%d\n", rc);
  23. }
  24. rc = zmq_msg_send(&msg, hint->sock, 0);
  25. if (rc < 0) {
  26. /*
  27. * gc socket could have been closed, e.g. during process teardown.
  28. * If so, ignore the failure because there's nothing to do.
  29. */
  30. if (zmq_errno() != ENOTSOCK) {
  31. fprintf(stderr, "pyzmq-gc send failed: %s\n",
  32. zmq_strerror(zmq_errno()));
  33. }
  34. }
  35. rc = mutex_unlock(hint->mutex);
  36. if (rc != 0) {
  37. fprintf(stderr, "pyzmq-gc mutex unlock failed rc=%d\n", rc);
  38. }
  39. zmq_msg_close(&msg);
  40. free(hint);
  41. }
  42. }
  43. int zmq_wrap_msg_init_data(zmq_msg_t *msg, void *data, size_t size,
  44. void *hint) {
  45. return zmq_msg_init_data(msg, data, size, free_python_msg, hint);
  46. }