File: threads_qthreads_mutex.h

package info (click to toggle)
openmpi 5.0.8-9
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 201,680 kB
  • sloc: ansic: 613,078; makefile: 42,350; 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 (205 lines) | stat: -rw-r--r-- 7,167 bytes parent folder | download | duplicates (2)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* -*- 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_QTHREADS_THREADS_QTHREADS_MUTEX_H
#define OPAL_MCA_THREADS_QTHREADS_THREADS_QTHREADS_MUTEX_H 1

#include "opal_config.h"

#include <stdio.h>

#include "opal/class/opal_object.h"
#include "opal/constants.h"
#include "opal/mca/threads/qthreads/threads_qthreads.h"
#include "opal/sys/atomic.h"
#include "opal/util/show_help.h"

BEGIN_C_DECLS

typedef qthread_spinlock_t opal_thread_internal_mutex_t;

#define OPAL_THREAD_INTERNAL_MUTEX_INITIALIZER           QTHREAD_MUTEX_INITIALIZER
#define OPAL_THREAD_INTERNAL_RECURSIVE_MUTEX_INITIALIZER QTHREAD_RECURSIVE_MUTEX_INITIALIZER

static inline int opal_thread_internal_mutex_init(opal_thread_internal_mutex_t *p_mutex,
                                                  bool recursive)
{
    opal_threads_ensure_init_qthreads();
    #if OPAL_ENABLE_DEBUG
    int ret = qthread_spinlock_init(p_mutex,recursive);
    if (QTHREAD_SUCCESS != ret) {
        opal_show_help("help-opal-threads.txt", "mutex init failed", true);
    }
    #else
    qthread_spinlock_init(p_mutex,recursive);
    #endif
    return OPAL_SUCCESS;
}

static inline void opal_thread_internal_mutex_lock(opal_thread_internal_mutex_t *p_mutex)
{
    opal_threads_ensure_init_qthreads();
    #if OPAL_ENABLE_DEBUG
    int ret = qthread_spinlock_lock(p_mutex);
    if (QTHREAD_SUCCESS != ret) {
        opal_show_help("help-opal-threads.txt", "mutex lock failed", true);
    }
    #else
    qthread_spinlock_lock(p_mutex);
    #endif
}

static inline int opal_thread_internal_mutex_trylock(opal_thread_internal_mutex_t *p_mutex)
{
    opal_threads_ensure_init_qthreads();
    int ret = qthread_spinlock_trylock(p_mutex);
    if (QTHREAD_OPFAIL == ret) {
        return 1;
    } else if (QTHREAD_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)
{
    opal_threads_ensure_init_qthreads();
    int ret; 
    #if OPAL_ENABLE_DEBUG
    ret = qthread_spinlock_unlock(p_mutex);
    if (QTHREAD_SUCCESS != ret) {
        opal_show_help("help-opal-threads.txt", "mutex unlock failed", true);
    }
    #else
    qthread_spinlock_unlock(p_mutex);
    #endif
    /* For fairness of locking. */
    qthread_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 struct opal_thread_cond_waiter_t {
    int m_signaled;
    struct opal_thread_cond_waiter_t *m_prev;
} opal_thread_cond_waiter_t;

typedef struct {
    opal_thread_internal_mutex_t m_lock;
    opal_thread_cond_waiter_t *m_waiter_head;
    opal_thread_cond_waiter_t *m_waiter_tail;
} opal_thread_internal_cond_t;

#define OPAL_THREAD_INTERNAL_COND_INITIALIZER                                          \
    {                                                                                  \
        .m_lock = QTHREAD_MUTEX_INITIALIZER, .m_waiter_head = NULL, .m_waiter_tail = NULL, \
    }

static inline int opal_thread_internal_cond_init(opal_thread_internal_cond_t *p_cond)
{
    qthread_spinlock_init(&p_cond->m_lock, false /* is_recursive */);
    p_cond->m_waiter_head = NULL;
    p_cond->m_waiter_tail = NULL;
    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)
{
    opal_threads_ensure_init_qthreads();
    /* This thread is taking "lock", so only this thread can access this
     * condition variable.  */
    qthread_spinlock_lock(&p_cond->m_lock);
    opal_thread_cond_waiter_t waiter = {0, NULL};
    if (NULL == p_cond->m_waiter_head) {
        p_cond->m_waiter_tail = &waiter;
    } else {
        p_cond->m_waiter_head->m_prev = &waiter;
    }
    p_cond->m_waiter_head = &waiter;
    qthread_spinlock_unlock(&p_cond->m_lock);
    while (1) {       
        opal_thread_internal_mutex_unlock(p_mutex);
        qthread_yield();
        opal_thread_internal_mutex_lock(p_mutex);
        /* Check if someone woke me up. */
        qthread_spinlock_lock(&p_cond->m_lock);
        int signaled = waiter.m_signaled;
        qthread_spinlock_unlock(&p_cond->m_lock);
        if (1 == signaled) {
            break;
        }
        /* Unlock the lock again. */
    }
}

static inline void opal_thread_internal_cond_broadcast(opal_thread_internal_cond_t *p_cond)
{
    qthread_spinlock_lock(&p_cond->m_lock);
    while (NULL != p_cond->m_waiter_tail) {
        opal_thread_cond_waiter_t *p_cur_tail = p_cond->m_waiter_tail;
        p_cond->m_waiter_tail = p_cur_tail->m_prev;
        /* Awaken one of threads in a FIFO manner. */
        p_cur_tail->m_signaled = 1;
    }
    /* No waiters. */
    p_cond->m_waiter_head = NULL;
    qthread_spinlock_unlock(&p_cond->m_lock);
}

static inline void opal_thread_internal_cond_signal(opal_thread_internal_cond_t *p_cond)
{
    qthread_spinlock_lock(&p_cond->m_lock);
    if (NULL != p_cond->m_waiter_tail) {
        opal_thread_cond_waiter_t *p_cur_tail = p_cond->m_waiter_tail;
        p_cond->m_waiter_tail = p_cur_tail->m_prev;
        /* Awaken one of threads. */
        p_cur_tail->m_signaled = 1;
        if (NULL == p_cond->m_waiter_tail) {
            p_cond->m_waiter_head = NULL;
        }
    }
    qthread_spinlock_unlock(&p_cond->m_lock);
}

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_QTHREADS_THREADS_QTHREADS_MUTEX_H */