2004-08-19 03:24:27 +04:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The University of Tennessee and The University
|
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2004-11-28 23:09:25 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2007-06-12 20:25:26 +04:00
|
|
|
* Copyright (c) 2007 Los Alamos National Security, LLC. All rights
|
|
|
|
* reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-08-19 03:24:27 +04:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
#ifndef OPAL_MUTEX_WINDOWS_H
|
|
|
|
#define OPAL_MUTEX_WINDOWS_H 1
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @file:
|
|
|
|
*
|
|
|
|
* Mutual exclusion functions: Windows implementation.
|
|
|
|
*
|
|
|
|
* Functions for locking of critical sections.
|
|
|
|
*
|
|
|
|
* On Windows, base everything on InterlockedExchange().
|
|
|
|
*/
|
|
|
|
|
2009-03-04 18:35:54 +03:00
|
|
|
#include "opal_config.h"
|
2005-07-03 20:06:07 +04:00
|
|
|
#include "opal/class/opal_object.h"
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal/sys/atomic.h"
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2007-06-12 20:25:26 +04:00
|
|
|
BEGIN_C_DECLS
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
struct opal_mutex_t {
|
2005-07-03 20:06:07 +04:00
|
|
|
opal_object_t super;
|
2004-08-19 03:24:27 +04:00
|
|
|
volatile LONG m_lock;
|
2007-06-12 20:25:26 +04:00
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
#if !OPAL_HAVE_THREAD_SUPPORT && OPAL_ENABLE_DEBUG
|
2007-06-12 20:25:26 +04:00
|
|
|
int m_lock_debug;
|
2007-11-05 16:03:35 +03:00
|
|
|
const char *m_lock_file;
|
2007-06-12 20:25:26 +04:00
|
|
|
int m_lock_line;
|
|
|
|
#endif
|
2004-08-19 03:24:27 +04:00
|
|
|
};
|
|
|
|
|
2006-08-20 19:54:04 +04:00
|
|
|
OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_mutex_t);
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline int opal_mutex_trylock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2007-06-15 02:28:28 +04:00
|
|
|
return (0 == InterlockedExchange(&m->m_lock, 1) ? 1 : 0);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline void opal_mutex_lock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
|
|
|
while (InterlockedExchange(&m->m_lock, 1)) {
|
|
|
|
while (m->m_lock == 1) {
|
|
|
|
/* spin */;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline void opal_mutex_unlock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
|
|
|
InterlockedExchange(&m->m_lock, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline int opal_mutex_atomic_trylock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
return opal_mutex_trylock(m);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline void opal_mutex_atomic_lock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_lock(m);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline void opal_mutex_atomic_unlock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(m);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2007-06-12 20:25:26 +04:00
|
|
|
END_C_DECLS
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
#endif /* OPAL_MUTEX_WINDOWS_H */
|