updates.
authorPekka Riikonen <priikone@silcnet.org>
Sun, 8 Jul 2001 07:59:36 +0000 (07:59 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sun, 8 Jul 2001 07:59:36 +0000 (07:59 +0000)
CHANGES
lib/silcutil/silcmutex.h
lib/silcutil/silcthread.h
lib/silcutil/unix/silcunixmutex.c
lib/silcutil/unix/silcunixthread.c
lib/silcutil/win32/Makefile.am
lib/silcutil/win32/silcwin32mutex.c [new file with mode: 0644]
lib/silcutil/win32/silcwin32thread.c [new file with mode: 0644]

diff --git a/CHANGES b/CHANGES
index 4e515e65cf2670e0dd9f0b497c6e527e7ce0ded0..012d0b51a102cddd15a8e757df3c9e4aacd6b8c4 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,8 @@
+Sun Jul  8 11:16:01 EEST 2001  Pekka Riikonen <priikone@silcnet.org>
+
+       * Implemented the SILC Mutex API and SILC Thread API for WIN32
+         in lib/silcutil/win32/.
+
 Sun Jul  8 00:18:15 EEST 2001  Pekka Riikonen <priikone@silcnet.org>
 
        * Defined SILC Mutex API and SILC Thread API and implemented
index 89ee8a55f30d9859c1fd559cc4143ba8890027f9..70eed3c3c263338bda5806acdf72e7c7cf88e8c5 100644 (file)
@@ -104,7 +104,7 @@ void silc_mutex_lock(SilcMutex mutex);
  *
  * DESCRIPTION
  *
- *    Unlocks the mutex and thus releases it for another threads that
+ *    Unlocks the mutex and thus releases it for another thread that
  *    may be waiting for the lock.
  *
  * NOTES
index 5d4932d8fb574e8284178ba1176a7137624c44c0..8f2f7094dad83867edeca6add839c187c997fddb 100644 (file)
@@ -23,7 +23,7 @@
  * DESCRIPTION
  *
  * Interface for SILC Thread implementation. This is platform independent
- * implementation of threads for applications needing concurrent execution
+ * implementation of threads for applications that need concurrent execution
  * with the application's main thread. The threads created with this 
  * interface executes concurrently with the calling thread.
  *
@@ -122,7 +122,7 @@ void silc_thread_exit(void *exit_value);
  *
  * DESCRIPTION
  *
- *    Returns a pointer of the current thread.
+ *    Returns a pointer to the current thread.
  *
  ***/
 SilcThread silc_thread_self(void);
index c6b21573344c2b5665a8f986f0f508fdbec50cfb..7875eb80a303391279de87512c8e0a717f58c806 100644 (file)
@@ -32,6 +32,7 @@ SilcMutex silc_mutex_alloc(void)
 {
   SilcMutex mutex = silc_calloc(1, sizeof(*mutex));
   pthread_mutex_init(&mutex->mutex, NULL);
+  return mutex;
 }
 
 void silc_mutex_free(SilcMutex mutex)
index 2c205c4105be551dc1b0013213c2b6353aae3ae4..9cd68c3ba276532f774b78adb6bef5ef9b4cdcda 100644 (file)
@@ -65,7 +65,7 @@ SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
 
 void silc_thread_exit(void *exit_value)
 {
-  pthread_exit(NULL);
+  pthread_exit(exit_value);
 }
 
 SilcThread silc_thread_self(void)
index 70e1d45f7fa0a4e7ebd50fded0176619ef6a7bba..57b8c23ad91054fd20e86b87a711b75b70b2ba3f 100644 (file)
@@ -24,6 +24,8 @@ libsilcwin32util_a_SOURCES =  \
        silcwin32net.c          \
        silcwin32schedule.c     \
        silcwin32sockconn.c     \
-       silcwin32util.c
+       silcwin32util.c         \
+       silcwin32mutex.c        \
+       silcwin32thread.c
 
 include $(top_srcdir)/Makefile.defines.in
diff --git a/lib/silcutil/win32/silcwin32mutex.c b/lib/silcutil/win32/silcwin32mutex.c
new file mode 100644 (file)
index 0000000..690c2a2
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+
+  silcwin32mutex.c
+
+  Author: Pekka Riikonen <priikone@silcnet.org>
+
+  Copyright (C) 2001 Pekka Riikonen
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+  
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+*/
+/* $Id$ */
+
+#include "silcincludes.h"
+
+#ifdef SILC_THREADS
+
+/* SILC Mutex structure */
+struct SilcMutexStruct {
+  HANDLE mutex;
+};
+
+SilcMutex silc_mutex_alloc(void)
+{
+  SilcMutex mutex = silc_calloc(1, sizeof(*mutex));
+  mutex->mutex = CreateMutex(NULL, FALSE, NULL);
+  if (!mutex->mutex) {
+    silc_free(mutex);
+    return NULL;
+  }
+  return mutex;
+}
+
+void silc_mutex_free(SilcMutex mutex)
+{
+  CloseHandle(mutex->mutex);
+  silc_free(mutex);
+}
+
+void silc_mutex_lock(SilcMutex mutex)
+{
+  WaitForSingleObject(mutex->mutex, INFINITE);
+}
+
+void silc_mutex_unlock(SilcMutex mutex)
+{
+  ReleaseMutex(mutex->mutex);
+}
+
+#endif /* SILC_THREADS */
diff --git a/lib/silcutil/win32/silcwin32thread.c b/lib/silcutil/win32/silcwin32thread.c
new file mode 100644 (file)
index 0000000..5c32cb3
--- /dev/null
@@ -0,0 +1,131 @@
+/*
+
+  silcwin32thread.c
+
+  Author: Pekka Riikonen <priikone@silcnet.org>
+
+  Copyright (C) 2001 Pekka Riikonen
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+  
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+*/
+/* These routines are based on GLib's WIN32 gthread implementation and
+   thus credits should go there. */
+/* $Id$ */
+
+#include "silcincludes.h"
+
+#ifdef SILC_THREADS
+
+/* Thread structure for WIN32 */
+typedef struct {
+  HANDLE thread;
+  SilcThreadStart start_func;
+  void *context;
+  bool waitable;
+} *SilcWin32Thread;
+
+static DWROD silc_thread_tls;
+
+/* Actual routine that is called by WIN32 when the thread is created.
+   We will call the start_func from here. When this returns the thread
+   is destroyed. */
+
+int __stdcall silc_thread_win32_start(void *context)
+{
+  SilcWin32Thread thread = (SilcWin32Thread)context;
+
+  TlsSetValue(silc_thread_tls, context);
+  thread->start_func(thread->context);
+  silc_thread_exit();
+
+  return 0;
+}
+
+SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
+                             bool waitable)
+{
+  SilcWin32Thread thread;
+  DWROD id;
+
+  SILC_LOG_DEBUG(("Creating new thread"));
+
+  thread = silc_calloc(1, sizeof(*thread));
+  thread->start_func = start_func;
+  thread->context = context;
+  thread->waitable = waitable;
+  thread->thread = _beginthreadex(NULL, 0, silc_thread_win32_start,
+                                 thread, 0, &id);
+  if (!thread->thread) {
+    SILC_LOG_ERROR(("Could not create new thread"));
+    silc_free(thread);
+    return NULL;
+  }
+
+  return (SilcThread)thread;
+}
+
+void silc_thread_exit(void *exit_value)
+{
+  SilcWin32Thread thread = TlsGetValue(silc_thread_tls);
+  
+  if (thread) {
+    /* If the thread is waitable the memory is freed only in silc_thread_wait
+       by another thread. If not waitable, free it now. */
+    if (!thread->waitable)
+      {
+       CloseHandle(thread->thread);
+       silc_free(thread);
+      }
+
+    TlsSetValue(silc_thread_tls, NULL);
+  }
+
+  _endthreadex(0);
+}
+
+SilcThread silc_thread_self(void)
+{
+  SilcWin32Thread self = TlsGetValue(silc_thread_tls);
+
+  if (!self) {
+    /* This should only happen for the main thread! */
+    HANDLE handle = GetCurrentThread ();
+    HANDLE process = GetCurrentProcess ();
+    self = silc_calloc(1, sizeof(*self));
+    DuplicateHandle(process, handle, process, 
+                   &self->thread, 0, FALSE, 
+                   DUPLICATE_SAME_ACCESS);
+    TlsSetValue(silc_thread_tls, self);
+  }
+
+  return (SilcThread)self;
+}
+
+bool silc_thread_wait(SilcThread thread, void **exit_value)
+{
+  SILC_LOG_DEBUG(("Waiting for thread %p", thread));
+
+  if (!thread->waitable)
+    return FALSE;
+
+  /* The thread is waitable thus we will free all memory after the
+     WaitForSingleObject returns, the thread is destroyed after that. */
+  WaitForSingleObject(thread->thread, INFINITE);
+  CloseHandle(thread->thread);
+  silc_free(thread);
+  if (exit_value)
+    *exit_value = NULL;
+
+  return TRUE;
+}
+
+#endif /* SILC_THREADS */