File: threads_argobots_mutex.h

package info (click to toggle)
openmpi 5.0.8-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,684 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (166 lines) | stat: -rw-r--r-- 5,583 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2006 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2007-2018 Los Alamos National Security, LLC.  All rights
 *                         reserved.
 * Copyright (c) 2015-2016 Research Organization for Information Science
 *                         and Technology (RIST). All rights reserved.
 * Copyright (c) 2019      Sandia National Laboratories.  All rights reserved.
 * Copyright (c) 2020      Triad National Security, LLC. All rights
 *                         reserved.
 * Copyright (c) 2020      Cisco Systems, Inc.  All rights reserved.
 * Copyright (c) 2021      Argonne National Laboratory.  All rights reserved.
 * Copyright (c) 2022      Sandia National Laboratories.  All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#ifndef OPAL_MCA_THREADS_ARGOBOTS_THREADS_ARGOBOTS_MUTEX_H
#define OPAL_MCA_THREADS_ARGOBOTS_THREADS_ARGOBOTS_MUTEX_H

#include "opal_config.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/util/output.h"
#include "opal/util/show_help.h"

BEGIN_C_DECLS

typedef ABT_mutex_memory opal_thread_internal_mutex_t;

#define OPAL_THREAD_INTERNAL_MUTEX_INITIALIZER           ABT_MUTEX_INITIALIZER
#define OPAL_THREAD_INTERNAL_RECURSIVE_MUTEX_INITIALIZER ABT_RECURSIVE_MUTEX_INITIALIZER

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
    int ret = ABT_mutex_lock(mutex);
    if (ABT_SUCCESS != ret) {
        opal_show_help("help-opal-threads.txt", "mutex lock failed", true);
    }
#else
    ABT_mutex_lock(mutex);
#endif
}

static inline int opal_thread_internal_mutex_trylock(opal_thread_internal_mutex_t *p_mutex)
{
    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_show_help("help-opal-threads.txt", "mutex trylock failed", true);
#endif
        return 1;
    }
    return 0;
}

static inline void opal_thread_internal_mutex_unlock(opal_thread_internal_mutex_t *p_mutex)
{
    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_show_help("help-opal-threads.txt", "mutex unlock failed", true);
    }
#else
    ABT_mutex_unlock(mutex);
#endif
    /* For fairness of locking. */
    ABT_thread_yield();
}

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. */
}

typedef ABT_cond_memory opal_thread_internal_cond_t;

#define OPAL_THREAD_INTERNAL_COND_INITIALIZER ABT_COND_INITIALIZER

static inline int opal_thread_internal_cond_init(opal_thread_internal_cond_t *p_cond)
{
    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_thread_internal_cond_wait(opal_thread_internal_cond_t *p_cond,
                                                  opal_thread_internal_mutex_t *p_mutex)
{
    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_thread_internal_cond_broadcast(opal_thread_internal_cond_t *p_cond)
{
    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
    ABT_cond_broadcast(cond);
#endif
}

static inline void opal_thread_internal_cond_signal(opal_thread_internal_cond_t *p_cond)
{
    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_thread_internal_cond_destroy(opal_thread_internal_cond_t *p_cond)
{
    /* No destructor is needed. */
}

END_C_DECLS

#endif /* OPAL_MCA_THREADS_ARGOBOTS_THREADS_ARGOBOTS_MUTEX_H */