Skip to content

opal/mca/threads: remove code duplication #8790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions opal/mca/threads/argobots/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# All rights reserved.
# Copyright (c) 2008 Cisco Systems, Inc. All rights reserved.
# Copyright (c) 2019 Sandia National Laboratories. All rights reserved.
# Copyright (c) 2021 Argonne National Laboratory. All rights reserved.
#
# $COPYRIGHT$
#
# Additional copyrights may follow
Expand All @@ -25,12 +27,9 @@ sources = \
threads_argobots_component.c \
threads_argobots_condition.c \
threads_argobots_module.c \
threads_argobots_mutex.c \
threads_argobots_mutex.h \
threads_argobots_threads.h \
threads_argobots_tsd.h \
threads_argobots_wait_sync.c \
threads_argobots_wait_sync.h
threads_argobots_tsd.h

#lib = libmca_threads_argobots.la
lib_sources = $(sources)
Expand Down
3 changes: 0 additions & 3 deletions opal/mca/threads/argobots/configure.m4
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ AC_DEFUN([MCA_opal_threads_argobots_POST_CONFIG],[
AC_DEFINE_UNQUOTED([MCA_threads_tsd_base_include_HEADER],
["opal/mca/threads/argobots/threads_argobots_tsd.h"],
[Header to include for tsd implementation])
AC_DEFINE_UNQUOTED([MCA_threads_wait_sync_base_include_HEADER],
["opal/mca/threads/argobots/threads_argobots_wait_sync.h"],
[Header to include for wait_sync implementation])
THREAD_CFLAGS="$TPKG_CFLAGS"
THREAD_FCFLAGS="$TPKG_FCFLAGS"
THREAD_CXXFLAGS="$TPKG_CXXFLAGS"
Expand Down
182 changes: 71 additions & 111 deletions opal/mca/threads/argobots/threads_argobots_mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,108 +31,71 @@

#include "opal_config.h"

#include <errno.h>
#include <stdio.h>
#include <string.h>

#include "opal/class/opal_object.h"
#include "opal/constants.h"
#include "opal/mca/threads/argobots/threads_argobots.h"
#include "opal/mca/threads/mutex.h"
#include "opal/sys/atomic.h"
#include "opal/util/output.h"

BEGIN_C_DECLS

struct opal_mutex_t {
opal_object_t super;
typedef ABT_mutex_memory opal_thread_internal_mutex_t;

ABT_mutex_memory m_lock_argobots;
int m_recursive;
#define OPAL_THREAD_INTERNAL_MUTEX_INITIALIZER ABT_MUTEX_INITIALIZER
#define OPAL_THREAD_INTERNAL_RECURSIVE_MUTEX_INITIALIZER ABT_RECURSIVE_MUTEX_INITIALIZER

#if OPAL_ENABLE_DEBUG
int m_lock_debug;
const char *m_lock_file;
int m_lock_line;
#endif

opal_atomic_lock_t m_lock_atomic;
};

OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_mutex_t);
OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_recursive_mutex_t);

#if OPAL_ENABLE_DEBUG
# define OPAL_MUTEX_STATIC_INIT \
{ \
.super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), .m_lock_argobots = ABT_MUTEX_INITIALIZER, \
.m_recursive = 0, .m_lock_debug = 0, .m_lock_file = NULL, .m_lock_line = 0, \
.m_lock_atomic = OPAL_ATOMIC_LOCK_INIT, \
}
#else
# define OPAL_MUTEX_STATIC_INIT \
{ \
.super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), .m_lock_argobots = ABT_MUTEX_INITIALIZER, \
.m_recursive = 0, .m_lock_atomic = OPAL_ATOMIC_LOCK_INIT, \
}
#endif
static inline int opal_thread_internal_mutex_init(opal_thread_internal_mutex_t *p_mutex,
bool recursive)
{
if (recursive) {
const ABT_mutex_memory init_mutex = ABT_RECURSIVE_MUTEX_INITIALIZER;
memcpy(p_mutex, &init_mutex, sizeof(ABT_mutex_memory));
} else {
const ABT_mutex_memory init_mutex = ABT_MUTEX_INITIALIZER;
memcpy(p_mutex, &init_mutex, sizeof(ABT_mutex_memory));
}
return OPAL_SUCCESS;
}

static inline void opal_thread_internal_mutex_lock(opal_thread_internal_mutex_t *p_mutex)
{
ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE(p_mutex);
#if OPAL_ENABLE_DEBUG
# define OPAL_RECURSIVE_MUTEX_STATIC_INIT \
{ \
.super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), \
.m_lock_argobots = ABT_RECURSIVE_MUTEX_INITIALIZER, .m_recursive = 1, \
.m_lock_debug = 0, .m_lock_file = NULL, .m_lock_line = 0, \
.m_lock_atomic = OPAL_ATOMIC_LOCK_INIT, \
}
int ret = ABT_mutex_lock(mutex);
if (ABT_SUCCESS != ret) {
opal_output(0, "opal_thread_internal_mutex_lock()");
}
#else
# define OPAL_RECURSIVE_MUTEX_STATIC_INIT \
{ \
.super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), \
.m_lock_argobots = ABT_RECURSIVE_MUTEX_INITIALIZER, .m_recursive = 1, \
.m_lock_atomic = OPAL_ATOMIC_LOCK_INIT, \
}
ABT_mutex_lock(mutex);
#endif
}

/************************************************************************
*
* mutex operations (non-atomic versions)
*
************************************************************************/

static inline int opal_mutex_trylock(opal_mutex_t *m)
static inline int opal_thread_internal_mutex_trylock(opal_thread_internal_mutex_t *p_mutex)
{
ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE(&m->m_lock_argobots);
ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE(p_mutex);
int ret = ABT_mutex_trylock(mutex);
if (ABT_ERR_MUTEX_LOCKED == ret) {
return 1;
} else if (ABT_SUCCESS != ret) {
#if OPAL_ENABLE_DEBUG
opal_output(0, "opal_mutex_trylock()");
opal_output(0, "opal_thread_internal_mutex_trylock()");
#endif
return 1;
} else {
return 0;
}
return 0;
}

static inline void opal_mutex_lock(opal_mutex_t *m)
static inline void opal_thread_internal_mutex_unlock(opal_thread_internal_mutex_t *p_mutex)
{
ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE(&m->m_lock_argobots);
#if OPAL_ENABLE_DEBUG
int ret = ABT_mutex_lock(mutex);
if (ABT_SUCCESS != ret) {
opal_output(0, "opal_mutex_lock()");
}
#else
ABT_mutex_lock(mutex);
#endif
}

static inline void opal_mutex_unlock(opal_mutex_t *m)
{
ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE(&m->m_lock_argobots);
ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE(p_mutex);
#if OPAL_ENABLE_DEBUG
int ret = ABT_mutex_unlock(mutex);
if (ABT_SUCCESS != ret) {
opal_output(0, "opal_mutex_unlock()");
opal_output(0, "opal_thread_internal_mutex_unlock()");
}
#else
ABT_mutex_unlock(mutex);
Expand All @@ -141,65 +104,62 @@ static inline void opal_mutex_unlock(opal_mutex_t *m)
ABT_thread_yield();
}

/************************************************************************
*
* mutex operations (atomic versions)
*
************************************************************************/
static inline void opal_thread_internal_mutex_destroy(opal_thread_internal_mutex_t *p_mutex)
{
/* No specific operation is needed to destroy opal_thread_internal_mutex_t. */
}

#if OPAL_HAVE_ATOMIC_SPINLOCKS
typedef ABT_cond_memory opal_thread_internal_cond_t;

/************************************************************************
* Spin Locks
************************************************************************/
#define OPAL_THREAD_INTERNAL_COND_INITIALIZER ABT_COND_INITIALIZER

static inline int opal_mutex_atomic_trylock(opal_mutex_t *m)
static inline int opal_thread_internal_cond_init(opal_thread_internal_cond_t *p_cond)
{
return opal_atomic_trylock(&m->m_lock_atomic);
const ABT_cond_memory init_cond = ABT_COND_INITIALIZER;
memcpy(p_cond, &init_cond, sizeof(ABT_cond_memory));
return OPAL_SUCCESS;
}

static inline void opal_mutex_atomic_lock(opal_mutex_t *m)
static inline void opal_thread_internal_cond_wait(opal_thread_internal_cond_t *p_cond,
opal_thread_internal_mutex_t *p_mutex)
{
opal_atomic_lock(&m->m_lock_atomic);
ABT_mutex mutex = ABT_MUTEX_MEMORY_GET_HANDLE(p_mutex);
ABT_cond cond = ABT_COND_MEMORY_GET_HANDLE(p_cond);
#if OPAL_ENABLE_DEBUG
int ret = ABT_cond_wait(cond, mutex);
assert(ABT_SUCCESS == ret);
#else
ABT_cond_wait(cond, mutex);
#endif
}

static inline void opal_mutex_atomic_unlock(opal_mutex_t *m)
static inline void opal_thread_internal_cond_broadcast(opal_thread_internal_cond_t *p_cond)
{
opal_atomic_unlock(&m->m_lock_atomic);
}

ABT_cond cond = ABT_COND_MEMORY_GET_HANDLE(p_cond);
#if OPAL_ENABLE_DEBUG
int ret = ABT_cond_broadcast(cond);
assert(ABT_SUCCESS == ret);
#else

/************************************************************************
* Standard locking
************************************************************************/

static inline int opal_mutex_atomic_trylock(opal_mutex_t *m)
{
return opal_mutex_trylock(m);
ABT_cond_broadcast(cond);
#endif
}

static inline void opal_mutex_atomic_lock(opal_mutex_t *m)
static inline void opal_thread_internal_cond_signal(opal_thread_internal_cond_t *p_cond)
{
opal_mutex_lock(m);
ABT_cond cond = ABT_COND_MEMORY_GET_HANDLE(p_cond);
#if OPAL_ENABLE_DEBUG
int ret = ABT_cond_signal(cond);
assert(ABT_SUCCESS == ret);
#else
ABT_cond_signal(cond);
#endif
}

static inline void opal_mutex_atomic_unlock(opal_mutex_t *m)
static inline void opal_thread_internal_cond_destroy(opal_thread_internal_cond_t *p_cond)
{
opal_mutex_unlock(m);
/* No destructor is needed. */
}

#endif

typedef ABT_cond_memory opal_cond_t;
#define OPAL_CONDITION_STATIC_INIT ABT_COND_INITIALIZER

int opal_cond_init(opal_cond_t *cond);
int opal_cond_wait(opal_cond_t *cond, opal_mutex_t *lock);
int opal_cond_broadcast(opal_cond_t *cond);
int opal_cond_signal(opal_cond_t *cond);
int opal_cond_destroy(opal_cond_t *cond);

END_C_DECLS

#endif /* OPAL_MCA_THREADS_ARGOBOTS_THREADS_ARGOBOTS_MUTEX_H */
Loading