- From e7592f4c4b9d05bef05a6a75ce4301fcc12a13b9 Mon Sep 17 00:00:00 2001
 - From: Roland Mainz <roland.mainz@nrubsig.org>
 - Date: Mon, 30 Oct 2023 18:18:33 +0100
 - Subject: [PATCH 1/2] libtitpc: |cond_signal()| should be called with mutex
 - held
 - In libtirpc/src/clnt_dg.c and libtirpc/src/clnt_vc.c |cond_signal()|
 - is called after unlocking the mutex (clnt_fd_lock).
 - On Linux/Solaris/POSIX we use |cond_signal()| == |posix_cond_signal()|,
 - where the the manual allows that usage, but it also mentions that
 - for consistent scheduling, |posix_cond_signal()| should be called with
 - the waiting mutex locked.
 - But on Windows we use |cond_signal()|==|WakeConditionVariable()|, which
 - seems to be much more picky, so we call it while holding the mutex.
 - This is basically what Linux libtirpc already did with
 - https://git.linux-nfs.org/?p=steved/libtirpc.git;a=commitdiff;h=21718bbbfa2a4bf4992bd295e25cbc67868dcfc1
 - Signed-off-by: Cedric Blancher <cedric.blancher@gmail.com>
 - ---
 - libtirpc/src/clnt_dg.c | 8 ++++----
 - libtirpc/src/clnt_vc.c | 10 +++++-----
 - 2 files changed, 9 insertions(+), 9 deletions(-)
 - diff --git a/libtirpc/src/clnt_dg.c b/libtirpc/src/clnt_dg.c
 - index 529a401..e5b62ff 100644
 - --- a/libtirpc/src/clnt_dg.c
 - +++ b/libtirpc/src/clnt_dg.c
 - @@ -99,18 +99,18 @@ static cond_t *dg_cv;
 - #define release_fd_lock(fd, mask) { \
 - mutex_lock(&clnt_fd_lock); \
 - dg_fd_locks[(fd)] = 0; \
 - - mutex_unlock(&clnt_fd_lock); \
 - thr_sigsetmask(SIG_SETMASK, &(mask), NULL); \
 - cond_signal(&dg_cv[(fd)]); \
 - + mutex_unlock(&clnt_fd_lock); \
 - }
 - #else
 - /* XXX Needs Windows signal/event stuff XXX */
 - #define release_fd_lock(fd, mask) { \
 - mutex_lock(&clnt_fd_lock); \
 - dg_fd_locks[(fd)] = 0; \
 - - mutex_unlock(&clnt_fd_lock); \
 - \
 - cond_signal(&dg_cv[(fd)]); \
 - + mutex_unlock(&clnt_fd_lock); \
 - }
 - #endif
 - @@ -610,9 +610,9 @@ clnt_dg_freeres(cl, xdr_res, res_ptr)
 - cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
 - xdrs->x_op = XDR_FREE;
 - dummy = (*xdr_res)(xdrs, res_ptr);
 - - mutex_unlock(&clnt_fd_lock);
 - // thr_sigsetmask(SIG_SETMASK, &mask, NULL);
 - cond_signal(&dg_cv[cu->cu_fd]);
 - + mutex_unlock(&clnt_fd_lock);
 - return (dummy);
 - }
 - @@ -801,9 +801,9 @@ clnt_dg_destroy(cl)
 - if (cl->cl_tp && cl->cl_tp[0])
 - mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
 - mem_free(cl, sizeof (CLIENT));
 - - mutex_unlock(&clnt_fd_lock);
 - // thr_sigsetmask(SIG_SETMASK, &mask, NULL);
 - cond_signal(&dg_cv[cu_fd]);
 - + mutex_unlock(&clnt_fd_lock);
 - }
 - static struct clnt_ops *
 - diff --git a/libtirpc/src/clnt_vc.c b/libtirpc/src/clnt_vc.c
 - index fa8a2d3..658d9ab 100644
 - --- a/libtirpc/src/clnt_vc.c
 - +++ b/libtirpc/src/clnt_vc.c
 - @@ -165,18 +165,18 @@ static cond_t *vc_cv;
 - #define release_fd_lock(fd, mask) { \
 - mutex_lock(&clnt_fd_lock); \
 - vc_fd_locks[(fd)] = 0; \
 - - mutex_unlock(&clnt_fd_lock); \
 - thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL); \
 - cond_signal(&vc_cv[(fd)]); \
 - + mutex_unlock(&clnt_fd_lock); \
 - }
 - #else
 - /* XXX Need Windows signal/event stuff XXX */
 - #define release_fd_lock(fd, mask) { \
 - mutex_lock(&clnt_fd_lock); \
 - vc_fd_locks[(fd)] = 0; \
 - - mutex_unlock(&clnt_fd_lock); \
 - \
 - cond_broadcast(&vc_cv[(fd)]); \
 - + mutex_unlock(&clnt_fd_lock); \
 - }
 - #endif
 - @@ -746,9 +746,9 @@ clnt_vc_freeres(cl, xdr_res, res_ptr)
 - cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
 - xdrs->x_op = XDR_FREE;
 - dummy = (*xdr_res)(xdrs, res_ptr);
 - - mutex_unlock(&clnt_fd_lock);
 - // thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
 - cond_signal(&vc_cv[ct->ct_fd]);
 - + mutex_unlock(&clnt_fd_lock);
 - return dummy;
 - }
 - @@ -922,8 +922,8 @@ clnt_vc_destroy(cl)
 - fprintf(stdout, "%04x: sending shutdown to callback thread %04x\n",
 - GetCurrentThreadId(), cl->cb_thread);
 - cl->shutdown = 1;
 - - mutex_unlock(&clnt_fd_lock);
 - cond_signal(&vc_cv[ct_fd]);
 - + mutex_unlock(&clnt_fd_lock);
 - status = WaitForSingleObject(cl->cb_thread, INFINITE);
 - fprintf(stdout, "%04x: terminated callback thread\n", GetCurrentThreadId());
 - mutex_lock(&clnt_fd_lock);
 - @@ -943,9 +943,9 @@ clnt_vc_destroy(cl)
 - if (cl->cl_tp && cl->cl_tp[0])
 - mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
 - mem_free(cl, sizeof(CLIENT));
 - - mutex_unlock(&clnt_fd_lock);
 - // thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
 - cond_signal(&vc_cv[ct_fd]);
 - + mutex_unlock(&clnt_fd_lock);
 - }
 - /*
 - --
 - 2.39.0
 - From 5e3261693e8c766e742eaa4787d8d51bf02a422b Mon Sep 17 00:00:00 2001
 - From: Roland Mainz <roland.mainz@nrubsig.org>
 - Date: Mon, 30 Oct 2023 18:49:22 +0100
 - Subject: [PATCH 2/2] libtirpc/src/clnt_vc.c: Cleanup |GetCurrentThreadId()|
 - leftovers
 - Cleanup |GetCurrentThreadId()| leftovers in libtirpc/src/clnt_vc.c,
 - e.g. |vc_fd_locks[(fd)] = GetCurrentThreadId();| should use
 - |vc_fd_locks[(fd)] = 1;| like the original libtirpc codebase (see
 - commit for "libtirpc: Use fd number instead of SOCKET handle for
 - locking").
 - Also clean up other |GetCurrentThreadId()| to get rid of compiler
 - warnings.
 - Signed-off-by: Cedric Blancher <cedric.blancher@gmail.com>
 - ---
 - libtirpc/src/clnt_vc.c | 33 +++++++++++++++++----------------
 - 1 file changed, 17 insertions(+), 16 deletions(-)
 - diff --git a/libtirpc/src/clnt_vc.c b/libtirpc/src/clnt_vc.c
 - index 658d9ab..7bf73b4 100644
 - --- a/libtirpc/src/clnt_vc.c
 - +++ b/libtirpc/src/clnt_vc.c
 - @@ -184,7 +184,7 @@ static cond_t *vc_cv;
 - mutex_lock(&clnt_fd_lock); \
 - while (vc_fd_locks[(fd)]) \
 - cond_wait(&vc_cv[(fd)], &clnt_fd_lock); \
 - - vc_fd_locks[(fd)] = GetCurrentThreadId(); \
 - + vc_fd_locks[(fd)] = 1; \
 - mutex_unlock(&clnt_fd_lock); \
 - }
 - @@ -207,7 +207,7 @@ static unsigned int WINAPI clnt_cb_thread(void *args)
 - struct rpc_msg reply_msg;
 - char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
 - - fprintf(stderr/*stdout*/, "%04x: Creating callback thread\n", GetCurrentThreadId());
 - + fprintf(stderr/*stdout*/, "%04lx: Creating callback thread\n", (long)GetCurrentThreadId());
 - while(1) {
 - cb_req header;
 - void *res = NULL;
 - @@ -222,11 +222,11 @@ static unsigned int WINAPI clnt_cb_thread(void *args)
 - if (!vc_fd_locks[ct->ct_fd])
 - break;
 - }
 - - vc_fd_locks[ct->ct_fd] = GetCurrentThreadId();
 - + vc_fd_locks[ct->ct_fd] = 1;
 - mutex_unlock(&clnt_fd_lock);
 - if (cl->shutdown) {
 - - fprintf(stdout, "%04x: callback received shutdown signal\n", GetCurrentThreadId());
 - + fprintf(stdout, "%04lx: callback received shutdown signal\n", (long)GetCurrentThreadId());
 - release_fd_lock(ct->ct_fd, mask);
 - goto out;
 - }
 - @@ -260,11 +260,11 @@ process_rpc_call:
 - ct->reply_msg.rm_call.cb_cred.oa_base = cred_area;
 - ct->reply_msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
 - if (!xdr_getcallbody(xdrs, &ct->reply_msg)) {
 - - fprintf(stderr, "%04x: xdr_getcallbody failed\n", GetCurrentThreadId());
 - + fprintf(stderr, "%04lx: xdr_getcallbody failed\n", (long)GetCurrentThreadId());
 - goto skip_process;
 - } else
 - - fprintf(stdout, "%04x: callbody: rpcvers %d cb_prog %d cb_vers %d cb_proc %d\n",
 - - GetCurrentThreadId(),
 - + fprintf(stdout, "%04lx: callbody: rpcvers %d cb_prog %d cb_vers %d cb_proc %d\n",
 - + (long)GetCurrentThreadId(),
 - ct->reply_msg.rm_call.cb_rpcvers, ct->reply_msg.rm_call.cb_prog,
 - ct->reply_msg.rm_call.cb_vers, ct->reply_msg.rm_call.cb_proc);
 - header.rq_prog = ct->reply_msg.rm_call.cb_prog;
 - @@ -273,13 +273,14 @@ process_rpc_call:
 - header.xdr = xdrs;
 - status = (*cl->cb_fn)(cl->cb_args, &header, &res);
 - if (status) {
 - - fprintf(stderr, "%04x: callback function failed with %d\n", status);
 - + fprintf(stderr, "%04lx: callback function failed with %d\n",
 - + (long)GetCurrentThreadId(), status);
 - }
 - xdrs->x_op = XDR_ENCODE;
 - __xdrrec_setblock(xdrs);
 - reply_msg.rm_xid = ct->reply_msg.rm_xid;
 - - fprintf(stdout, "%04x: cb: replying to xid %d\n", GetCurrentThreadId(),
 - + fprintf(stdout, "%04lx: cb: replying to xid %d\n", (long)GetCurrentThreadId(),
 - ct->reply_msg.rm_xid);
 - ct->reply_msg.rm_xid = 0;
 - reply_msg.rm_direction = REPLY;
 - @@ -295,7 +296,7 @@ process_rpc_call:
 - (*cl->cb_xdr)(xdrs, res); /* free the results */
 - }
 - if (! xdrrec_endofrecord(xdrs, 1)) {
 - - fprintf(stderr, "%04x: failed to send REPLY\n", GetCurrentThreadId());
 - + fprintf(stderr, "%04lx: failed to send REPLY\n", (long)GetCurrentThreadId());
 - }
 - skip_process:
 - ct->reply_msg.rm_direction = -1;
 - @@ -485,8 +486,8 @@ clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz, cb_xdr, cb_fn, cb_args)
 - fprintf(stderr, "_beginthreadex failed %d\n", GetLastError());
 - goto err;
 - } else
 - - fprintf(stdout, "%04x: started the callback thread %04x\n",
 - - GetCurrentThreadId(), cl->cb_thread);
 - + fprintf(stdout, "%04lx: started the callback thread %04lx\n",
 - + (long)GetCurrentThreadId(), (long)GetThreadId(cl->cb_thread));
 - } else
 - cl->cb_thread = INVALID_HANDLE_VALUE;
 - return (cl);
 - @@ -591,7 +592,7 @@ call_again:
 - while ((vc_fd_locks[ct->ct_fd]) ||
 - (ct->reply_msg.rm_xid && ct->reply_msg.rm_xid != x_id))
 - cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
 - - vc_fd_locks[ct->ct_fd] = GetCurrentThreadId();
 - + vc_fd_locks[ct->ct_fd] = 1;
 - mutex_unlock(&clnt_fd_lock);
 - }
 - #endif
 - @@ -919,13 +920,13 @@ clnt_vc_destroy(cl)
 - if (cl->cb_thread != INVALID_HANDLE_VALUE) {
 - int status;
 - - fprintf(stdout, "%04x: sending shutdown to callback thread %04x\n",
 - - GetCurrentThreadId(), cl->cb_thread);
 - + fprintf(stdout, "%04lx: sending shutdown to callback thread %04lx\n",
 - + (long)GetCurrentThreadId(), (long)GetThreadId(cl->cb_thread));
 - cl->shutdown = 1;
 - cond_signal(&vc_cv[ct_fd]);
 - mutex_unlock(&clnt_fd_lock);
 - status = WaitForSingleObject(cl->cb_thread, INFINITE);
 - - fprintf(stdout, "%04x: terminated callback thread\n", GetCurrentThreadId());
 - + fprintf(stdout, "%04lx: terminated callback thread\n", (long)GetCurrentThreadId());
 - mutex_lock(&clnt_fd_lock);
 - while (vc_fd_locks[ct_fd])
 - cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
 - --
 - 2.39.0
 
msnfs41client: libtirpc cond_var&cleanup patches, 2023-10-30
Posted by Anonymous on Mon 30th Oct 2023 18:07
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
 rovema.kpaste.net RSS