aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMicael Karlberg <[email protected]>2019-03-13 18:11:43 +0100
committerMicael Karlberg <[email protected]>2019-04-17 16:56:33 +0200
commit97f9c6fd26544265f7cf774ff18e8ca2edf5840d (patch)
tree339414b511e352c181b49f16abe253215d78e752
parent143b23ea12795a149f655c644fe519e8ccaa4183 (diff)
downloadotp-97f9c6fd26544265f7cf774ff18e8ca2edf5840d.tar.gz
otp-97f9c6fd26544265f7cf774ff18e8ca2edf5840d.tar.bz2
otp-97f9c6fd26544265f7cf774ff18e8ca2edf5840d.zip
[socket] Restructure the close function(s)
To increase readability the close function has been slightly restructured.
-rw-r--r--erts/emulator/nifs/common/socket_nif.c206
1 files changed, 134 insertions, 72 deletions
diff --git a/erts/emulator/nifs/common/socket_nif.c b/erts/emulator/nifs/common/socket_nif.c
index 20aa17c3f4..c30359fb64 100644
--- a/erts/emulator/nifs/common/socket_nif.c
+++ b/erts/emulator/nifs/common/socket_nif.c
@@ -1080,6 +1080,11 @@ static ERL_NIF_TERM nrecvmsg(ErlNifEnv* env,
int flags);
static ERL_NIF_TERM nclose(ErlNifEnv* env,
SocketDescriptor* descP);
+static BOOLEAN_T nclose_check(ErlNifEnv* env,
+ SocketDescriptor* descP,
+ ERL_NIF_TERM* reason);
+static ERL_NIF_TERM nclose_do(ErlNifEnv* env,
+ SocketDescriptor* descP);
static ERL_NIF_TERM nshutdown(ErlNifEnv* env,
SocketDescriptor* descP,
int how);
@@ -6597,10 +6602,6 @@ ERL_NIF_TERM nclose(ErlNifEnv* env,
{
ERL_NIF_TERM reply, reason;
BOOLEAN_T doClose;
- int selectRes;
- int domain = descP->domain;
- int type = descP->type;
- int protocol = descP->protocol;
SSDBG( descP, ("SOCKET",
"nclose -> [%d] entry (0x%lX, 0x%lX, 0x%lX, 0x%lX)\r\n",
@@ -6612,12 +6613,48 @@ ERL_NIF_TERM nclose(ErlNifEnv* env,
MLOCK(descP->closeMtx);
+ doClose = nclose_check(env, descP, &reason);
+
+ if (doClose) {
+ reply = nclose_do(env, descP);
+ } else {
+ reply = esock_make_error(env, reason);
+ }
+
+ MUNLOCK(descP->closeMtx);
+
+ SSDBG( descP,
+ ("SOCKET", "nclose -> [%d] done when: "
+ "\r\n state: 0x%lX"
+ "\r\n reply: %T"
+ "\r\n", descP->sock, descP->state, reply) );
+
+ return reply;
+}
+
+
+
+/* *** nclose_check ***
+ *
+ * Check if we should try to perform the first stage close.
+ */
+static
+BOOLEAN_T nclose_check(ErlNifEnv* env,
+ SocketDescriptor* descP,
+ ERL_NIF_TERM* reason)
+{
+ BOOLEAN_T doClose;
+
if (descP->state == SOCKET_STATE_CLOSED) {
- reason = atom_closed;
+
doClose = FALSE;
+ *reason = atom_closed;
+
} else if (descP->state == SOCKET_STATE_CLOSING) {
- reason = atom_closing;
+
doClose = FALSE;
+ *reason = atom_closing;
+
} else {
/* Store the PID of the caller,
@@ -6627,90 +6664,115 @@ ERL_NIF_TERM nclose(ErlNifEnv* env,
*/
if (enif_self(env, &descP->closerPid) == NULL) {
- MUNLOCK(descP->closeMtx);
- return esock_make_error(env, atom_exself);
- }
- /* Monitor the caller, since we should complete this operation even if
- * the caller dies (for whatever reason).
- *
- * <KOLLA>
- *
- * Can we actiually use this for anything?
- *
- * </KOLLA>
- */
+ doClose = FALSE;
+ *reason = atom_exself;
- if (MONP("nclose -> closer",
- env, descP,
- &descP->closerPid,
- &descP->closerMon) != 0) {
- MUNLOCK(descP->closeMtx);
- return esock_make_error(env, atom_exmon);
- }
-
- descP->closeLocal = TRUE;
- descP->state = SOCKET_STATE_CLOSING;
- descP->isReadable = FALSE;
- descP->isWritable = FALSE;
- doClose = TRUE;
- }
-
- if (doClose) {
- descP->closeEnv = enif_alloc_env();
- descP->closeRef = MKREF(descP->closeEnv);
- selectRes = esock_select_stop(env, descP->sock, descP);
- if (selectRes & ERL_NIF_SELECT_STOP_CALLED) {
- /* Prep done - inform the caller it can finalize (close) directly */
- SSDBG( descP,
- ("SOCKET", "nclose -> [%d] stop was called\r\n", descP->sock) );
- dec_socket(domain, type, protocol);
- reply = esock_atom_ok;
- } else if (selectRes & ERL_NIF_SELECT_STOP_SCHEDULED) {
- /* The stop callback function has been *scheduled* which means that we
- * have to wait for it to complete. */
- SSDBG( descP,
- ("SOCKET", "nclose -> [%d] stop was scheduled\r\n",
- descP->sock) );
- dec_socket(domain, type, protocol); // SHALL WE DO THIS AT finalize?
- reply = esock_make_ok2(env, enif_make_copy(env, descP->closeRef));
} else {
- SSDBG( descP,
- ("SOCKET", "nclose -> [%d] stop failed: %d\r\n",
- descP->sock, selectRes) );
-
- /* <KOLLA>
+ /* Monitor the caller, since we should complete this operation even if
+ * the caller dies (for whatever reason).
+ *
+ * <KOLLA>
*
- * WE SHOULD REALLY HAVE A WAY TO CLOBBER THE SOCKET,
- * SO WE DON'T LET STUFF LEAK.
- * NOW, BECAUSE WE FAILED TO SELECT, WE CANNOT FINISH
- * THE CLOSE, WHAT TO DO? ABORT?
+ * Can we actually use this for anything?
*
* </KOLLA>
*/
- // No point in having this?
- DEMONP("nclose -> closer", env, descP, &descP->closerMon);
+ if (MONP("nclose_check -> closer",
+ env, descP,
+ &descP->closerPid,
+ &descP->closerMon) != 0) {
- reason = MKT2(env, atom_select, MKI(env, selectRes));
- reply = esock_make_error(env, reason);
+ doClose = FALSE;
+ *reason = atom_exmon;
+
+ } else {
+
+ descP->closeLocal = TRUE;
+ descP->state = SOCKET_STATE_CLOSING;
+ descP->isReadable = FALSE;
+ descP->isWritable = FALSE;
+ doClose = TRUE;
+ *reason = esock_atom_undefined; // NOT used !!
+
+ }
}
+ }
+
+ return doClose;
+
+}
+
+
+
+/* *** nclose_do ***
+ *
+ * Perform (do) the first stage close.
+ */
+static
+ERL_NIF_TERM nclose_do(ErlNifEnv* env,
+ SocketDescriptor* descP)
+{
+ int domain = descP->domain;
+ int type = descP->type;
+ int protocol = descP->protocol;
+ int sres;
+ ERL_NIF_TERM reply, reason;
+
+ descP->closeEnv = enif_alloc_env();
+ descP->closeRef = MKREF(descP->closeEnv);
+ sres = esock_select_stop(env, descP->sock, descP);
+
+ if (sres & ERL_NIF_SELECT_STOP_CALLED) {
+
+ /* Prep done - inform the caller it can finalize (close) directly */
+ SSDBG( descP,
+ ("SOCKET", "nclose -> [%d] stop was called\r\n", descP->sock) );
+
+ dec_socket(domain, type, protocol);
+ reply = esock_atom_ok;
+
+ } else if (sres & ERL_NIF_SELECT_STOP_SCHEDULED) {
+
+ /* The stop callback function has been *scheduled* which means that we
+ * have to wait for it to complete. */
+ SSDBG( descP,
+ ("SOCKET", "nclose -> [%d] stop was scheduled\r\n",
+ descP->sock) );
+
+ dec_socket(domain, type, protocol); // SHALL WE DO THIS AT finalize?
+ reply = esock_make_ok2(env, enif_make_copy(env, descP->closeRef));
} else {
- reply = esock_make_error(env, reason);
- }
- MUNLOCK(descP->closeMtx);
+ SSDBG( descP,
+ ("SOCKET", "nclose -> [%d] stop failed: %d\r\n",
+ descP->sock, sres) );
- SSDBG( descP,
- ("SOCKET", "nclose -> [%d] done when: "
- "\r\n state: 0x%lX"
- "\r\n reply: %T"
- "\r\n", descP->sock, descP->state, reply) );
+ /* <KOLLA>
+ *
+ * WE SHOULD REALLY HAVE A WAY TO CLOBBER THE SOCKET,
+ * SO WE DON'T LET STUFF LEAK.
+ * NOW, BECAUSE WE FAILED TO SELECT, WE CANNOT FINISH
+ * THE CLOSE, WHAT TO DO? ABORT?
+ *
+ * </KOLLA>
+ */
+
+ // Do we need this?
+ DEMONP("nclose_do -> closer", env, descP, &descP->closerMon);
+
+ reason = MKT2(env, esock_atom_select_failed, MKI(env, sres));
+ reply = esock_make_error(env, reason);
+ }
return reply;
}
+
+
+
#endif // if !defined(__WIN32__)