/* -*- 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>
G_BEGIN_DECLS
+#ifndef HOST_WIN32
+
typedef struct {
pthread_mutex_t mutex;
gboolean complete;
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 */
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)
#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)
#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 */
return(mono_mutex_unlock (mutex));
}
-#ifdef __cplusplus
-}
-#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__ */