[PATCH 1/4] tests/t-kdf: few changes to pthread example and fix win32/win64 builds

Jussi Kivilinna jussi.kivilinna at iki.fi
Fri Jan 28 20:06:13 CET 2022


* src/gcrypt.h.in (gcry_kdf_thread_ops_t): New based on
'struct gcry_kdf_thread_ops'.
(gcry_kdf_compute): Use 'gcry_kdf_thread_ops_t' instead of
'struct gcry_kdf_thread_ops'.
* tests/Makefile.am: Define 't_kdf_LDADD' and 't_kdf_CFLAGS' on
win32/win64 target too.
* tests/t-kdf.c (pthread_jobs_launch_job): Set 'oldest_thread_idx' on
first thread creation.
(wait_all_jobs_completion): Reset 'oldest_thread_idx' to -1.
(my_kdf_derive): Merge HAVE_PTHREAD ifdefs; Initialize 'oldest_thread_idx'
to -1.
--

Windows build was not working because of missing HAVE_PTHREAD in
't-kdf.c' and LDADD/CFLAGS issue in 'Makefile.am'.

Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
 src/gcrypt.h.in   |  7 ++++---
 tests/Makefile.am |  2 ++
 tests/t-kdf.c     | 46 +++++++++++++++++++++++-----------------------
 3 files changed, 29 insertions(+), 26 deletions(-)

diff --git a/src/gcrypt.h.in b/src/gcrypt.h.in
index 5e016932..680f634f 100644
--- a/src/gcrypt.h.in
+++ b/src/gcrypt.h.in
@@ -1620,11 +1620,12 @@ typedef int (*gcry_kdf_lauch_job_t) (void *jobs_context,
 typedef int (*gcry_kdf_wait_all_jobs_completion_t) (void *jobs_context);
 
 /* Exposed structure for KDF computation to decouple thread functionality.  */
-struct gcry_kdf_thread_ops {
+typedef struct gcry_kdf_thread_ops
+{
   void *jobs_context;
   gcry_kdf_lauch_job_t launch_job;
   gcry_kdf_wait_all_jobs_completion_t wait_all_jobs_completion;
-};
+} gcry_kdf_thread_ops_t;
 
 gcry_error_t gcry_kdf_open (gcry_kdf_hd_t *hd, int algo, int subalgo,
                             const unsigned long *param, unsigned int paramlen,
@@ -1633,7 +1634,7 @@ gcry_error_t gcry_kdf_open (gcry_kdf_hd_t *hd, int algo, int subalgo,
                             const void *key, size_t keylen,
                             const void *ad, size_t adlen);
 gcry_error_t gcry_kdf_compute (gcry_kdf_hd_t h,
-                               const struct gcry_kdf_thread_ops *ops);
+                               const gcry_kdf_thread_ops_t *ops);
 gcry_error_t gcry_kdf_final (gcry_kdf_hd_t h, size_t resultlen, void *result);
 void gcry_kdf_close (gcry_kdf_hd_t h);
 
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b42156f0..e6953fd3 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -89,6 +89,8 @@ if HAVE_W32_SYSTEM
 xtestsuite_libs = ../src/.libs/libgcrypt-20.dll \
                   $(prefix)/bin/libgpg-error*-0.dll
 xtestsuite_driver = .libs/testdrv.exe
+t_kdf_LDADD = $(standard_ldadd) $(GPG_ERROR_LIBS) @LDADD_FOR_TESTS_KLUDGE@
+t_kdf_CFLAGS = $(GPG_ERROR_CFLAGS)
 else
 xtestsuite_libs = ../src/.libs/libgcrypt.so*
 xtestsuite_driver = testdrv
diff --git a/tests/t-kdf.c b/tests/t-kdf.c
index 59559a4c..d61159e3 100644
--- a/tests/t-kdf.c
+++ b/tests/t-kdf.c
@@ -1255,7 +1255,8 @@ struct user_defined_threads_ctx
   int num_threads_running;
   pthread_attr_t attr;
   pthread_t thread[MAX_THREADS];
-  struct job_thread_param {
+  struct job_thread_param
+  {
     void (*job) (void *work_priv);
     void *priv;
   } work[MAX_THREADS];
@@ -1275,8 +1276,7 @@ pthread_jobs_launch_job (void *jobs_context,
 {
   struct user_defined_threads_ctx *ctx = jobs_context;
 
-  if (ctx->num_threads_running
-      && ctx->next_thread_idx == ctx->oldest_thread_idx)
+  if (ctx->next_thread_idx == ctx->oldest_thread_idx)
     {
       assert (ctx->num_threads_running == MAX_THREADS);
       /* thread limit reached, join a thread */
@@ -1289,6 +1289,8 @@ pthread_jobs_launch_job (void *jobs_context,
   ctx->work[ctx->next_thread_idx].priv = work_priv;
   pthread_create (&ctx->thread[ctx->next_thread_idx], &ctx->attr,
                   job_thread, &ctx->work[ctx->next_thread_idx]);
+  if (ctx->oldest_thread_idx < 0)
+    ctx->oldest_thread_idx = ctx->next_thread_idx;
   ctx->next_thread_idx = (ctx->next_thread_idx + 1) % MAX_THREADS;
   ctx->num_threads_running++;
   return 0;
@@ -1308,7 +1310,7 @@ wait_all_jobs_completion (void *jobs_context)
 
   /* reset context for next round of parallel work */
   ctx->num_threads_running = 0;
-  ctx->oldest_thread_idx = 0;
+  ctx->oldest_thread_idx = -1;
   ctx->next_thread_idx = 0;
 
   return 0;
@@ -1327,9 +1329,8 @@ my_kdf_derive (int parallel,
 {
   gcry_error_t err;
   gcry_kdf_hd_t hd;
-#ifdef HAVE_PTHREAD
-  struct user_defined_threads_ctx jobs_context;
-#endif
+
+  (void)parallel;
 
   err = gcry_kdf_open (&hd, algo, subalgo, params, paramslen,
                        pass, passlen, salt, saltlen, key, keylen,
@@ -1340,7 +1341,16 @@ my_kdf_derive (int parallel,
 #ifdef HAVE_PTHREAD
   if (parallel)
     {
+      struct user_defined_threads_ctx jobs_context;
+      const gcry_kdf_thread_ops_t ops =
+      {
+        &jobs_context,
+        pthread_jobs_launch_job,
+        wait_all_jobs_completion
+      };
+
       memset (&jobs_context, 0, sizeof (struct user_defined_threads_ctx));
+      jobs_context.oldest_thread_idx = -1;
 
       if (pthread_attr_init (&jobs_context.attr))
 	{
@@ -1357,26 +1367,16 @@ my_kdf_derive (int parallel,
 	  gcry_kdf_close (hd);
 	  return err;
 	}
-    }
-#endif
-
-  if (!parallel)
-    err = gcry_kdf_compute (hd, NULL);
-  else
-    {
-      struct gcry_kdf_thread_ops ops = {
-        &jobs_context,
-        pthread_jobs_launch_job,
-        wait_all_jobs_completion
-      };
 
       err = gcry_kdf_compute (hd, &ops);
-    }
 
-#ifdef HAVE_PTHREAD
-  if (parallel)
-    pthread_attr_destroy (&jobs_context. attr);
+      pthread_attr_destroy (&jobs_context. attr);
+    }
+  else
 #endif
+    {
+      err = gcry_kdf_compute (hd, NULL);
+    }
 
   if (!err)
     err = gcry_kdf_final (hd, outlen, out);
-- 
2.32.0




More information about the Gcrypt-devel mailing list