]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
Win32 changes for benchmark. Patch by LRN.
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Thu, 7 Apr 2011 21:32:39 +0000 (23:32 +0200)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Thu, 7 Apr 2011 21:32:39 +0000 (23:32 +0200)
src/benchmark.c

index 1bc0dd8687ad6157e11eb6b707b6c9338a68d337..00d572a94689d766c95baa856f7a98f1ab10fb31 100644 (file)
@@ -35,11 +35,51 @@ static unsigned char data[64 * 1024];
 
 static int must_finish = 0;
 
+#if !defined(_WIN32)
 static void
 alarm_handler (int signo)
 {
   must_finish = 1;
 }
+#else
+#include <windows.h>
+DWORD WINAPI
+alarm_handler (LPVOID lpParameter)
+{
+  HANDLE wtimer = *((HANDLE *) lpParameter);
+  WaitForSingleObject (wtimer, INFINITE);
+  must_finish = 1;
+  return 0;
+}
+
+#define W32_ALARM_VARIABLES HANDLE wtimer = NULL, wthread = NULL; \
+  LARGE_INTEGER alarm_timeout = { 0 , 0 }
+#define W32_ALARM_TRIGGER(timeout, leave) { \
+  wtimer = CreateWaitableTimer (NULL, TRUE, NULL); \
+  if (wtimer == NULL) \
+    { \
+      fprintf (stderr, "error: CreateWaitableTimer %u\n", GetLastError ()); \
+      leave; \
+    } \
+  wthread = CreateThread (NULL, 0, alarm_handler, &wtimer, 0, NULL); \
+  if (wthread == NULL) \
+    { \
+      fprintf (stderr, "error: CreateThread %u\n", GetLastError ()); \
+      leave; \
+    } \
+  alarm_timeout.QuadPart = timeout * 10000000; \
+  if (SetWaitableTimer (wtimer, &alarm_timeout, 0, NULL, NULL, FALSE) == 0) \
+    { \
+      fprintf (stderr, "error: SetWaitableTimer %u\n", GetLastError ()); \
+      leave; \
+    } \
+  }
+#define W32_ALARM_CLEANUP { \
+  if (wtimer != NULL) \
+    CloseHandle (wtimer); \
+  if (wthread != NULL) \
+    CloseHandle (wthread);}
+#endif
 
 static void
 tls_log_func (int level, const char *str)
@@ -97,6 +137,9 @@ cipher_mac_bench (int algo, int mac_algo, int size)
   int keysize = gnutls_cipher_get_key_size (algo);
   char metric[16];
   int step = size*1024;
+#if defined(_WIN32)
+  W32_ALARM_VARIABLES;
+#endif
 
   _key = malloc (keysize);
   if (_key == NULL)
@@ -119,7 +162,11 @@ cipher_mac_bench (int algo, int mac_algo, int size)
   fflush (stdout);
 
   must_finish = 0;
+#if !defined(_WIN32)
   alarm (5);
+#else
+  W32_ALARM_TRIGGER(5, goto leave);
+#endif
 
   gettime (&start);
 
@@ -182,6 +229,9 @@ cipher_bench (int algo, int size, int aead)
   int keysize = gnutls_cipher_get_key_size (algo);
   char metric[16];
   int step = size*1024;
+#if defined(_WIN32)
+  W32_ALARM_VARIABLES;
+#endif
 
   _key = malloc (keysize);
   if (_key == NULL)
@@ -241,7 +291,9 @@ cipher_bench (int algo, int size, int aead)
 leave:
   free (_key);
   free (_iv);
-
+#if defined(_WIN32)
+  W32_ALARM_CLEANUP;
+#endif
 }
 
 static void
@@ -265,7 +317,11 @@ mac_bench (int algo, int size)
   fflush (stdout);
 
   must_finish = 0;
+#if !defined(_WIN32)
   alarm (5);
+#else
+  W32_ALARM_TRIGGER(5, goto leave);
+#endif
 
   gettime (&start);
 
@@ -287,7 +343,10 @@ mac_bench (int algo, int size)
 
   printf ("Hashed %.2f %s in %.2f secs: ", ddata, metric, secs);
   printf ("%.2f %s/sec\n", dspeed, metric);
-
+#if defined(_WIN32)
+leave:
+  W32_ALARM_CLEANUP;
+#endif
   free (_key);
 }
 
@@ -299,7 +358,9 @@ main (int argc, char **argv)
   if (argc > 1)
     debug_level = 2;
 
+#if !defined(_WIN32)
   signal (SIGALRM, alarm_handler);
+#endif
 
   gnutls_global_set_log_function (tls_log_func);
   gnutls_global_set_log_level (debug_level);