[amd64] Remove the callee saved registers from MonoLMF, save/restore them normally...
[mono-project.git] / mono / utils / mono-mutex.h
blob01a5c83ac23926876aa37cc6a43660225ad1bcf0
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * mono-mutex.h: Portability wrappers around POSIX Mutexes
5 * Authors: Jeffrey Stedfast <fejj@ximian.com>
7 * Copyright 2002 Ximian, Inc. (www.ximian.com)
8 */
11 #ifndef __MONO_MUTEX_H__
12 #define __MONO_MUTEX_H__
14 #include <glib.h>
15 #ifdef HAVE_PTHREAD_H
16 #include <pthread.h>
17 #endif
18 #include <time.h>
20 #ifdef HOST_WIN32
21 #include <windows.h>
22 #endif
24 G_BEGIN_DECLS
26 #ifndef HOST_WIN32
28 typedef struct {
29 pthread_mutex_t mutex;
30 gboolean complete;
31 } mono_once_t;
33 #define MONO_ONCE_INIT { PTHREAD_MUTEX_INITIALIZER, FALSE }
35 int mono_once (mono_once_t *once, void (*once_init) (void));
37 typedef pthread_mutex_t mono_mutex_t;
38 typedef pthread_cond_t mono_cond_t;
40 #define mono_mutex_init(mutex) pthread_mutex_init (mutex, NULL)
41 #define mono_mutex_lock(mutex) pthread_mutex_lock (mutex)
42 #define mono_mutex_trylock(mutex) pthread_mutex_trylock (mutex)
43 #define mono_mutex_timedlock(mutex,timeout) pthread_mutex_timedlock (mutex, timeout)
44 #define mono_mutex_unlock(mutex) pthread_mutex_unlock (mutex)
45 #define mono_mutex_destroy(mutex) pthread_mutex_destroy (mutex)
47 #define mono_cond_init(cond,attr) pthread_cond_init (cond,attr)
48 #define mono_cond_wait(cond,mutex) pthread_cond_wait (cond, mutex)
49 #define mono_cond_timedwait(cond,mutex,timeout) pthread_cond_timedwait (cond, mutex, timeout)
50 #define mono_cond_signal(cond) pthread_cond_signal (cond)
51 #define mono_cond_broadcast(cond) pthread_cond_broadcast (cond)
52 #define mono_cond_destroy(cond)
54 /* This is a function so it can be passed to pthread_cleanup_push -
55 * that is a macro and giving it a macro as a parameter breaks.
57 G_GNUC_UNUSED
58 static inline int mono_mutex_unlock_in_cleanup (mono_mutex_t *mutex)
60 return(mono_mutex_unlock (mutex));
63 /* Returns zero on success. */
64 static inline int
65 mono_mutex_init_recursive (mono_mutex_t *mutex)
67 int res;
68 pthread_mutexattr_t attr;
70 pthread_mutexattr_init (&attr);
71 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
72 res = pthread_mutex_init (mutex, &attr);
73 pthread_mutexattr_destroy (&attr);
75 return res;
78 #else
80 typedef CRITICAL_SECTION mono_mutex_t;
81 typedef HANDLE mono_cond_t;
83 #define mono_mutex_init(mutex) (InitializeCriticalSection((mutex)), 0)
84 #define mono_mutex_init_recursive(mutex) (InitializeCriticalSection((mutex)), 0)
85 #define mono_mutex_lock(mutex) EnterCriticalSection((mutex))
86 #define mono_mutex_trylock(mutex) TryEnterCriticalSection((mutex))
87 #define mono_mutex_unlock(mutex) LeaveCriticalSection((mutex))
88 #define mono_mutex_destroy(mutex) DeleteCriticalSection((mutex))
91 #define mono_cond_init(cond,attr) do{*(cond) = CreateEvent(NULL,FALSE,FALSE,NULL); } while (0)
92 #define mono_cond_wait(cond,mutex) WaitForSingleObject(*(cond),INFINITE)
93 #define mono_cond_timedwait(cond,mutex,timeout) WaitForSingleObject(*(cond),timeout)
94 #define mono_cond_signal(cond) SetEvent(*(cond))
95 #define mono_cond_broadcast(cond) (!SetEvent(*(cond)))
96 #define mono_cond_destroy(cond) CloseHandle(*(cond))
98 #endif
100 int mono_mutex_init_suspend_safe (mono_mutex_t *mutex);
102 G_END_DECLS
104 #endif /* __MONO_MUTEX_H__ */