Skip to content
This repository
Browse code

Fix socket handling of send operations when the socket is in active m…

…ode.
  • Loading branch information...
commit cadc359b835768c88a65a68998eec2b08d568f65 1 parent 0139502
Michael Truog okeuday authored

Showing 1 changed file with 41 additions and 24 deletions. Show diff stats Hide diff stats

  1. +41 24 c_src/erlzmq_nif.c
65 c_src/erlzmq_nif.c
@@ -470,18 +470,26 @@ NIF(erlzmq_nif_send)
470 470
471 471 memcpy(zmq_msg_data(&req.data.send.msg), binary.data, binary.size);
472 472
473   - enif_mutex_lock(socket->mutex);
474   - if (zmq_send(socket->socket_zmq, &req.data.send.msg,
475   - req.data.send.flags | ZMQ_NOBLOCK)) {
476   - enif_mutex_unlock(socket->mutex);
477   -
478   - int const error = zmq_errno();
479   - if (error != EAGAIN ||
480   - (error == EAGAIN && (req.data.send.flags & ZMQ_NOBLOCK))) {
481   - zmq_msg_close(&req.data.send.msg);
482   - return return_zmq_errno(env, error);
  473 + int polling_thread_send = 1;
  474 + if (! socket->active) {
  475 + enif_mutex_lock(socket->mutex);
  476 + if (zmq_send(socket->socket_zmq, &req.data.send.msg,
  477 + req.data.send.flags | ZMQ_NOBLOCK)) {
  478 + enif_mutex_unlock(socket->mutex);
  479 + int const error = zmq_errno();
  480 + if (error != EAGAIN ||
  481 + (error == EAGAIN && (req.data.send.flags & ZMQ_NOBLOCK))) {
  482 + zmq_msg_close(&req.data.send.msg);
  483 + return return_zmq_errno(env, error);
  484 + }
483 485 }
484   -
  486 + else {
  487 + enif_mutex_unlock(socket->mutex);
  488 + polling_thread_send = 0;
  489 + }
  490 + }
  491 +
  492 + if (polling_thread_send) {
485 493 req.type = ERLZMQ_THREAD_REQUEST_SEND;
486 494 req.data.send.env = enif_alloc_env();
487 495 req.data.send.ref = enif_make_ref(req.data.send.env);
@@ -497,22 +505,26 @@ NIF(erlzmq_nif_send)
497 505
498 506 memcpy(zmq_msg_data(&msg), &req, sizeof(erlzmq_thread_request_t));
499 507
  508 + enif_mutex_lock(socket->context->mutex);
500 509 if (zmq_send(socket->context->thread_socket, &msg, 0)) {
  510 + enif_mutex_unlock(socket->context->mutex);
  511 +
501 512 zmq_msg_close(&msg);
502 513 zmq_msg_close(&req.data.send.msg);
503 514 enif_free_env(req.data.send.env);
504 515 return return_zmq_errno(env, zmq_errno());
505 516 }
  517 + else {
  518 + enif_mutex_unlock(socket->context->mutex);
506 519
507   - zmq_msg_close(&msg);
508   - // each pointer to the socket in a request increments the reference
509   - enif_keep_resource(socket);
510   -
511   - return enif_make_copy(env, req.data.send.ref);
  520 + zmq_msg_close(&msg);
  521 + // each pointer to the socket in a request increments the reference
  522 + enif_keep_resource(socket);
  523 +
  524 + return enif_make_copy(env, req.data.send.ref);
  525 + }
512 526 }
513 527 else {
514   - enif_mutex_unlock(socket->mutex);
515   -
516 528 zmq_msg_close(&req.data.send.msg);
517 529
518 530 return enif_make_atom(env, "ok");
@@ -554,7 +566,6 @@ NIF(erlzmq_nif_recv)
554 566 (error == EAGAIN && (req.data.recv.flags & ZMQ_NOBLOCK))) {
555 567 return return_zmq_errno(env, error);
556 568 }
557   -
558 569 req.type = ERLZMQ_THREAD_REQUEST_RECV;
559 570 req.data.recv.env = enif_alloc_env();
560 571 req.data.recv.ref = enif_make_ref(req.data.recv.env);
@@ -568,17 +579,23 @@ NIF(erlzmq_nif_recv)
568 579
569 580 memcpy(zmq_msg_data(&msg), &req, sizeof(erlzmq_thread_request_t));
570 581
  582 + enif_mutex_lock(socket->context->mutex);
571 583 if (zmq_send(socket->context->thread_socket, &msg, 0)) {
  584 + enif_mutex_unlock(socket->context->mutex);
  585 +
572 586 zmq_msg_close(&msg);
573 587 enif_free_env(req.data.recv.env);
574 588 return return_zmq_errno(env, zmq_errno());
575 589 }
  590 + else {
  591 + enif_mutex_unlock(socket->context->mutex);
576 592
577   - zmq_msg_close(&msg);
578   - // each pointer to the socket in a request increments the reference
579   - enif_keep_resource(socket);
580   -
581   - return enif_make_copy(env, req.data.recv.ref);
  593 + zmq_msg_close(&msg);
  594 + // each pointer to the socket in a request increments the reference
  595 + enif_keep_resource(socket);
  596 +
  597 + return enif_make_copy(env, req.data.recv.ref);
  598 + }
582 599 }
583 600 else {
584 601 enif_mutex_unlock(socket->mutex);

0 comments on commit cadc359

Please sign in to comment.
Something went wrong with that request. Please try again.