Flush (work in progress)
[mono.git] / mono / io-layer / mono-mutex.h
index 8e233167629ef3bc4ac7956f8fa49260a62c288b..74ff0655d42bb4cdd2aa68ab7dc9e4e810a7e094 100644 (file)
@@ -1,43 +1,25 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
- *  Authors: Jeffrey Stedfast <fejj@ximian.com>
+ * mono-mutex.h: Portability wrappers around POSIX Mutexes
  *
- *  Copyright 2002 Ximain, Inc. (www.ximian.com)
- *
- *  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.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
+ * Authors: Jeffrey Stedfast <fejj@ximian.com>
  *
+ * Copyright 2002 Ximian, Inc. (www.ximian.com)
  */
 
 
 #ifndef __MONO_MUTEX_H__
 #define __MONO_MUTEX_H__
 
-#ifdef __cplusplus
-extern "C" {
-#pragma }
-#endif /* __cplusplus */
-
 #include <glib.h>
+#ifdef HAVE_PTHREAD_H
 #include <pthread.h>
+#endif
 #include <time.h>
 
-#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
-extern int pthread_mutex_timedlock (pthread_mutex_t *mutex,
-                                   const struct timespec *timeout);
-#endif /* HAVE_PTHREAD_MUTEX_TIMEDLOCK */
+G_BEGIN_DECLS
 
+#ifndef HOST_WIN32
 
 typedef struct {
        pthread_mutex_t mutex;
@@ -94,7 +76,6 @@ typedef struct _mono_mutex_t {
 #define MONO_MUTEX_INITIALIZER { 0, MONO_THREAD_NONE, 0, 0, PTHREAD_MUTEX_INITIALIZER, 0 }
 #define MONO_RECURSIVE_MUTEX_INITIALIZER { 0, MONO_THREAD_NONE, 0, 0, PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER }
 
-
 int mono_mutexattr_init (mono_mutexattr_t *attr);
 int mono_mutexattr_settype (mono_mutexattr_t *attr, int type);
 int mono_mutexattr_gettype (mono_mutexattr_t *attr, int *type);
@@ -119,6 +100,7 @@ int mono_cond_wait (pthread_cond_t *cond, mono_mutex_t *mutex);
 int mono_cond_timedwait (pthread_cond_t *cond, mono_mutex_t *mutex, const struct timespec *timeout);
 #define mono_cond_signal(cond) pthread_cond_signal (cond)
 #define mono_cond_broadcast(cond) pthread_cond_broadcast (cond)
+#define mono_cond_destroy(cond)
 
 #else /* system is equipped with a fully-functional pthread mutex library */
 
@@ -136,9 +118,11 @@ int mono_cond_timedwait (pthread_cond_t *cond, mono_mutex_t *mutex, const struct
 
 typedef pthread_mutex_t mono_mutex_t;
 typedef pthread_mutexattr_t mono_mutexattr_t;
+typedef pthread_cond_t mono_cond_t;
 
 #define MONO_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
 #define MONO_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER
+#define MONO_COND_INITIALIZER PTHREAD_COND_INITIALIZER
 
 #define mono_mutexattr_init(attr) pthread_mutexattr_init (attr)
 #define mono_mutexattr_settype(attr,type) pthread_mutexattr_settype (attr, type)
@@ -149,6 +133,7 @@ typedef pthread_mutexattr_t mono_mutexattr_t;
 #define mono_mutexattr_getprotocol(attr,protocol) pthread_mutexattr_getprotocol (attr, protocol)
 #define mono_mutexattr_setprioceiling(attr,prioceiling) pthread_mutexattr_setprioceiling (attr, prioceiling)
 #define mono_mutexattr_getprioceiling(attr,prioceiling) pthread_mutexattr_getprioceiling (attr, prioceiling)
+#define mono_mutexattr_destroy(attr) pthread_mutexattr_destroy (attr)
 
 #define mono_mutex_init(mutex,attr) pthread_mutex_init (mutex, attr)
 #define mono_mutex_lock(mutex) pthread_mutex_lock (mutex)
@@ -162,11 +147,41 @@ typedef pthread_mutexattr_t mono_mutexattr_t;
 #define mono_cond_timedwait(cond,mutex,timeout) pthread_cond_timedwait (cond, mutex, timeout)
 #define mono_cond_signal(cond) pthread_cond_signal (cond)
 #define mono_cond_broadcast(cond) pthread_cond_broadcast (cond)
+#define mono_cond_destroy(cond)
 
 #endif /* USE_MONO_MUTEX */
 
-#ifdef __cplusplus
+/* This is a function so it can be passed to pthread_cleanup_push -
+ * that is a macro and giving it a macro as a parameter breaks.
+ */
+G_GNUC_UNUSED
+static inline int mono_mutex_unlock_in_cleanup (mono_mutex_t *mutex)
+{
+       return(mono_mutex_unlock (mutex));
 }
-#endif /* __cplusplus */
+
+#else
+
+typedef CRITICAL_SECTION mono_mutex_t;
+typedef HANDLE mono_cond_t;
+
+#define mono_mutex_init(mutex,attr) InitializeCriticalSection((mutex))
+#define mono_mutex_lock(mutex) EnterCriticalSection((mutex))
+#define mono_mutex_unlock(mutex)  LeaveCriticalSection((mutex))
+#define mono_mutex_destroy(mutex) DeleteCriticalSection((mutex))
+
+
+#define mono_cond_init(cond,attr) do{*(cond) = CreateEvent(NULL,FALSE,FALSE,NULL); } while (0)
+#define mono_cond_wait(cond,mutex) WaitForSingleObject(*(cond),INFINITE)
+#define mono_cond_timedwait(cond,mutex,timeout) WaitForSingleObject(*(cond),timeout)
+#define mono_cond_signal(cond) SetEvent(*(cond))
+#define mono_cond_broadcast(cond) (!SetEvent(*(cond)))
+#define mono_cond_destroy(cond) CloseHandle(*(cond))
+
+#define MONO_MUTEX_INITIALIZER NULL
+#define MONO_COND_INITIALIZER NULL
+#endif
+
+G_END_DECLS
 
 #endif /* __MONO_MUTEX_H__ */