1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * mono-mutex.h: Portability wrappers around POSIX Mutexes
5 * Authors: Jeffrey Stedfast <fejj@ximian.com>
7 * Copyright 2002 Ximian, Inc. (www.ximian.com)
11 #ifndef __MONO_MUTEX_H__
12 #define __MONO_MUTEX_H__
29 pthread_mutex_t mutex
;
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.
58 static inline int mono_mutex_unlock_in_cleanup (mono_mutex_t
*mutex
)
60 return(mono_mutex_unlock (mutex
));
63 /* Returns zero on success. */
65 mono_mutex_init_recursive (mono_mutex_t
*mutex
)
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
);
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))
100 int mono_mutex_init_suspend_safe (mono_mutex_t
*mutex
);
104 #endif /* __MONO_MUTEX_H__ */