pastebin - collaborative debugging tool
rovema.kpaste.net RSS


msnfs41client: libtirpc cond_var&cleanup patches, 2023-10-30
Posted by Anonymous on Mon 30th Oct 2023 18:07
raw | new post

  1. From e7592f4c4b9d05bef05a6a75ce4301fcc12a13b9 Mon Sep 17 00:00:00 2001
  2. From: Roland Mainz <roland.mainz@nrubsig.org>
  3. Date: Mon, 30 Oct 2023 18:18:33 +0100
  4. Subject: [PATCH 1/2] libtitpc: |cond_signal()| should be called with mutex
  5.  held
  6.  
  7. In libtirpc/src/clnt_dg.c and libtirpc/src/clnt_vc.c |cond_signal()|
  8. is called after unlocking the mutex (clnt_fd_lock).
  9. On Linux/Solaris/POSIX we use |cond_signal()| == |posix_cond_signal()|,
  10. where the the manual allows that usage, but it also mentions that
  11. for consistent scheduling, |posix_cond_signal()| should be called with
  12. the waiting mutex locked.
  13.  
  14. But on Windows we use |cond_signal()|==|WakeConditionVariable()|, which
  15. seems to be much more picky, so we call it while holding the mutex.
  16.  
  17. This is basically what Linux libtirpc already did with
  18. https://git.linux-nfs.org/?p=steved/libtirpc.git;a=commitdiff;h=21718bbbfa2a4bf4992bd295e25cbc67868dcfc1
  19.  
  20. Signed-off-by: Cedric Blancher <cedric.blancher@gmail.com>
  21. ---
  22. libtirpc/src/clnt_dg.c |  8 ++++----
  23.  libtirpc/src/clnt_vc.c | 10 +++++-----
  24.  2 files changed, 9 insertions(+), 9 deletions(-)
  25.  
  26. diff --git a/libtirpc/src/clnt_dg.c b/libtirpc/src/clnt_dg.c
  27. index 529a401..e5b62ff 100644
  28. --- a/libtirpc/src/clnt_dg.c
  29. +++ b/libtirpc/src/clnt_dg.c
  30. @@ -99,18 +99,18 @@ static cond_t       *dg_cv;
  31.  #define        release_fd_lock(fd, mask) {             \
  32.         mutex_lock(&clnt_fd_lock);      \
  33.         dg_fd_locks[(fd)] = 0;          \
  34. -       mutex_unlock(&clnt_fd_lock);    \
  35.         thr_sigsetmask(SIG_SETMASK, &(mask), NULL); \
  36.         cond_signal(&dg_cv[(fd)]);      \
  37. +       mutex_unlock(&clnt_fd_lock);    \
  38.  }
  39.  #else
  40.  /* XXX Needs Windows signal/event stuff XXX */
  41.  #define        release_fd_lock(fd, mask) {             \
  42.         mutex_lock(&clnt_fd_lock);      \
  43.         dg_fd_locks[(fd)] = 0;          \
  44. -       mutex_unlock(&clnt_fd_lock);    \
  45.         \
  46.         cond_signal(&dg_cv[(fd)]);      \
  47. +       mutex_unlock(&clnt_fd_lock);    \
  48.  }
  49.  #endif
  50.  
  51. @@ -610,9 +610,9 @@ clnt_dg_freeres(cl, xdr_res, res_ptr)
  52.                 cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
  53.         xdrs->x_op = XDR_FREE;
  54.         dummy = (*xdr_res)(xdrs, res_ptr);
  55. -       mutex_unlock(&clnt_fd_lock);
  56.  //     thr_sigsetmask(SIG_SETMASK, &mask, NULL);
  57.         cond_signal(&dg_cv[cu->cu_fd]);
  58. +       mutex_unlock(&clnt_fd_lock);
  59.         return (dummy);
  60.  }
  61.  
  62. @@ -801,9 +801,9 @@ clnt_dg_destroy(cl)
  63.         if (cl->cl_tp && cl->cl_tp[0])
  64.                 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
  65.         mem_free(cl, sizeof (CLIENT));
  66. -       mutex_unlock(&clnt_fd_lock);
  67.  //     thr_sigsetmask(SIG_SETMASK, &mask, NULL);
  68.         cond_signal(&dg_cv[cu_fd]);
  69. +       mutex_unlock(&clnt_fd_lock);
  70.  }
  71.  
  72.  static struct clnt_ops *
  73. diff --git a/libtirpc/src/clnt_vc.c b/libtirpc/src/clnt_vc.c
  74. index fa8a2d3..658d9ab 100644
  75. --- a/libtirpc/src/clnt_vc.c
  76. +++ b/libtirpc/src/clnt_vc.c
  77. @@ -165,18 +165,18 @@ static cond_t   *vc_cv;
  78.  #define release_fd_lock(fd, mask) {    \
  79.         mutex_lock(&clnt_fd_lock);      \
  80.         vc_fd_locks[(fd)] = 0;          \
  81. -       mutex_unlock(&clnt_fd_lock);    \
  82.         thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL);        \
  83.         cond_signal(&vc_cv[(fd)]);      \
  84. +       mutex_unlock(&clnt_fd_lock);    \
  85.  }
  86.  #else
  87.  /* XXX Need Windows signal/event stuff XXX */
  88.  #define release_fd_lock(fd, mask) {    \
  89.         mutex_lock(&clnt_fd_lock);      \
  90.         vc_fd_locks[(fd)] = 0;          \
  91. -       mutex_unlock(&clnt_fd_lock);    \
  92.         \
  93.         cond_broadcast(&vc_cv[(fd)]);   \
  94. +       mutex_unlock(&clnt_fd_lock);    \
  95.  }
  96.  #endif
  97.  
  98. @@ -746,9 +746,9 @@ clnt_vc_freeres(cl, xdr_res, res_ptr)
  99.                 cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
  100.         xdrs->x_op = XDR_FREE;
  101.         dummy = (*xdr_res)(xdrs, res_ptr);
  102. -       mutex_unlock(&clnt_fd_lock);
  103.  //     thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
  104.         cond_signal(&vc_cv[ct->ct_fd]);
  105. +       mutex_unlock(&clnt_fd_lock);
  106.  
  107.         return dummy;
  108.  }
  109. @@ -922,8 +922,8 @@ clnt_vc_destroy(cl)
  110.          fprintf(stdout, "%04x: sending shutdown to callback thread %04x\n",
  111.              GetCurrentThreadId(), cl->cb_thread);
  112.          cl->shutdown = 1;
  113. -        mutex_unlock(&clnt_fd_lock);
  114.          cond_signal(&vc_cv[ct_fd]);
  115. +        mutex_unlock(&clnt_fd_lock);
  116.          status = WaitForSingleObject(cl->cb_thread, INFINITE);
  117.          fprintf(stdout, "%04x: terminated callback thread\n", GetCurrentThreadId());
  118.          mutex_lock(&clnt_fd_lock);
  119. @@ -943,9 +943,9 @@ clnt_vc_destroy(cl)
  120.         if (cl->cl_tp && cl->cl_tp[0])
  121.                 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
  122.         mem_free(cl, sizeof(CLIENT));
  123. -       mutex_unlock(&clnt_fd_lock);
  124.  //     thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
  125.         cond_signal(&vc_cv[ct_fd]);
  126. +       mutex_unlock(&clnt_fd_lock);
  127.  }
  128.  
  129.  /*
  130. --
  131. 2.39.0
  132.  
  133. From 5e3261693e8c766e742eaa4787d8d51bf02a422b Mon Sep 17 00:00:00 2001
  134. From: Roland Mainz <roland.mainz@nrubsig.org>
  135. Date: Mon, 30 Oct 2023 18:49:22 +0100
  136. Subject: [PATCH 2/2] libtirpc/src/clnt_vc.c: Cleanup |GetCurrentThreadId()|
  137.  leftovers
  138.  
  139. Cleanup |GetCurrentThreadId()| leftovers in libtirpc/src/clnt_vc.c,
  140. e.g. |vc_fd_locks[(fd)] = GetCurrentThreadId();| should use
  141. |vc_fd_locks[(fd)] = 1;| like the original libtirpc codebase (see
  142. commit for "libtirpc: Use fd number instead of SOCKET handle for
  143. locking").
  144.  
  145. Also clean up other |GetCurrentThreadId()| to get rid of compiler
  146. warnings.
  147.  
  148. Signed-off-by: Cedric Blancher <cedric.blancher@gmail.com>
  149. ---
  150. libtirpc/src/clnt_vc.c | 33 +++++++++++++++++----------------
  151.  1 file changed, 17 insertions(+), 16 deletions(-)
  152.  
  153. diff --git a/libtirpc/src/clnt_vc.c b/libtirpc/src/clnt_vc.c
  154. index 658d9ab..7bf73b4 100644
  155. --- a/libtirpc/src/clnt_vc.c
  156. +++ b/libtirpc/src/clnt_vc.c
  157. @@ -184,7 +184,7 @@ static cond_t   *vc_cv;
  158.         mutex_lock(&clnt_fd_lock); \
  159.         while (vc_fd_locks[(fd)]) \
  160.                 cond_wait(&vc_cv[(fd)], &clnt_fd_lock); \
  161. -       vc_fd_locks[(fd)] = GetCurrentThreadId(); \
  162. +       vc_fd_locks[(fd)] = 1; \
  163.         mutex_unlock(&clnt_fd_lock); \
  164.  }
  165.  
  166. @@ -207,7 +207,7 @@ static unsigned int WINAPI clnt_cb_thread(void *args)
  167.      struct rpc_msg reply_msg;    
  168.      char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
  169.  
  170. -    fprintf(stderr/*stdout*/, "%04x: Creating callback thread\n", GetCurrentThreadId());
  171. +    fprintf(stderr/*stdout*/, "%04lx: Creating callback thread\n", (long)GetCurrentThreadId());
  172.      while(1) {
  173.          cb_req header;
  174.          void *res = NULL;
  175. @@ -222,11 +222,11 @@ static unsigned int WINAPI clnt_cb_thread(void *args)
  176.                  if (!vc_fd_locks[ct->ct_fd])
  177.                      break;
  178.         }
  179. -       vc_fd_locks[ct->ct_fd] = GetCurrentThreadId();
  180. +       vc_fd_locks[ct->ct_fd] = 1;
  181.         mutex_unlock(&clnt_fd_lock);
  182.  
  183.          if (cl->shutdown) {
  184. -            fprintf(stdout, "%04x: callback received shutdown signal\n", GetCurrentThreadId());
  185. +            fprintf(stdout, "%04lx: callback received shutdown signal\n", (long)GetCurrentThreadId());
  186.              release_fd_lock(ct->ct_fd, mask);
  187.              goto out;
  188.          }
  189. @@ -260,11 +260,11 @@ process_rpc_call:
  190.          ct->reply_msg.rm_call.cb_cred.oa_base = cred_area;
  191.          ct->reply_msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
  192.          if (!xdr_getcallbody(xdrs, &ct->reply_msg)) {
  193. -            fprintf(stderr, "%04x: xdr_getcallbody failed\n", GetCurrentThreadId());            
  194. +            fprintf(stderr, "%04lx: xdr_getcallbody failed\n", (long)GetCurrentThreadId());
  195.              goto skip_process;
  196.          } else
  197. -            fprintf(stdout, "%04x: callbody: rpcvers %d cb_prog %d cb_vers %d cb_proc %d\n",
  198. -                GetCurrentThreadId(),
  199. +            fprintf(stdout, "%04lx: callbody: rpcvers %d cb_prog %d cb_vers %d cb_proc %d\n",
  200. +                (long)GetCurrentThreadId(),
  201.                  ct->reply_msg.rm_call.cb_rpcvers, ct->reply_msg.rm_call.cb_prog,
  202.                  ct->reply_msg.rm_call.cb_vers, ct->reply_msg.rm_call.cb_proc);
  203.          header.rq_prog = ct->reply_msg.rm_call.cb_prog;
  204. @@ -273,13 +273,14 @@ process_rpc_call:
  205.          header.xdr = xdrs;
  206.          status = (*cl->cb_fn)(cl->cb_args, &header, &res);
  207.          if (status) {
  208. -            fprintf(stderr, "%04x: callback function failed with %d\n", status);
  209. +            fprintf(stderr, "%04lx: callback function failed with %d\n",
  210. +                (long)GetCurrentThreadId(), status);
  211.          }
  212.          
  213.          xdrs->x_op = XDR_ENCODE;
  214.          __xdrrec_setblock(xdrs);
  215.          reply_msg.rm_xid = ct->reply_msg.rm_xid;
  216. -        fprintf(stdout, "%04x: cb: replying to xid %d\n", GetCurrentThreadId(),
  217. +        fprintf(stdout, "%04lx: cb: replying to xid %d\n", (long)GetCurrentThreadId(),
  218.              ct->reply_msg.rm_xid);
  219.          ct->reply_msg.rm_xid = 0;
  220.          reply_msg.rm_direction = REPLY;
  221. @@ -295,7 +296,7 @@ process_rpc_call:
  222.              (*cl->cb_xdr)(xdrs, res); /* free the results */
  223.          }
  224.          if (! xdrrec_endofrecord(xdrs, 1)) {
  225. -            fprintf(stderr, "%04x: failed to send REPLY\n", GetCurrentThreadId());
  226. +            fprintf(stderr, "%04lx: failed to send REPLY\n", (long)GetCurrentThreadId());
  227.          }
  228.  skip_process:
  229.          ct->reply_msg.rm_direction = -1;
  230. @@ -485,8 +486,8 @@ clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz, cb_xdr, cb_fn, cb_args)
  231.              fprintf(stderr, "_beginthreadex failed %d\n", GetLastError());
  232.              goto err;
  233.          } else
  234. -            fprintf(stdout, "%04x: started the callback thread %04x\n",
  235. -                GetCurrentThreadId(), cl->cb_thread);
  236. +            fprintf(stdout, "%04lx: started the callback thread %04lx\n",
  237. +                (long)GetCurrentThreadId(), (long)GetThreadId(cl->cb_thread));
  238.      } else
  239.          cl->cb_thread = INVALID_HANDLE_VALUE;
  240.         return (cl);
  241. @@ -591,7 +592,7 @@ call_again:
  242.                 while ((vc_fd_locks[ct->ct_fd]) ||
  243.                      (ct->reply_msg.rm_xid && ct->reply_msg.rm_xid != x_id))
  244.                         cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
  245. -               vc_fd_locks[ct->ct_fd] = GetCurrentThreadId();
  246. +               vc_fd_locks[ct->ct_fd] = 1;
  247.                 mutex_unlock(&clnt_fd_lock);
  248.          }
  249.  #endif
  250. @@ -919,13 +920,13 @@ clnt_vc_destroy(cl)
  251.  
  252.      if (cl->cb_thread != INVALID_HANDLE_VALUE) {
  253.          int status;
  254. -        fprintf(stdout, "%04x: sending shutdown to callback thread %04x\n",
  255. -            GetCurrentThreadId(), cl->cb_thread);
  256. +        fprintf(stdout, "%04lx: sending shutdown to callback thread %04lx\n",
  257. +            (long)GetCurrentThreadId(), (long)GetThreadId(cl->cb_thread));
  258.          cl->shutdown = 1;
  259.          cond_signal(&vc_cv[ct_fd]);
  260.          mutex_unlock(&clnt_fd_lock);
  261.          status = WaitForSingleObject(cl->cb_thread, INFINITE);
  262. -        fprintf(stdout, "%04x: terminated callback thread\n", GetCurrentThreadId());
  263. +        fprintf(stdout, "%04lx: terminated callback thread\n", (long)GetCurrentThreadId());
  264.          mutex_lock(&clnt_fd_lock);
  265.          while (vc_fd_locks[ct_fd])
  266.              cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
  267. --
  268. 2.39.0

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at