File: condition.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 (146 lines) | stat: -rw-r--r-- 4,245 bytes parent folder | download | duplicates (5)
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
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2020 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      Los Alamos National Security, LLC.  All rights
 *                         reserved.
 * Copyright (c) 2015      Research Organization for Information Science
 *                         and Technology (RIST). All rights reserved.
 * Copyright (c) 2019      Sandia National Laboratories. All rights reserved.
 * Copyright (c) 2019      Triad National Security, LLC. All rights
 *                         reserved.
 *
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#ifndef OPAL_MCA_THREADS_CONDITION_H
#define OPAL_MCA_THREADS_CONDITION_H

#include "opal_config.h"
#ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#endif
#include <pthread.h>
#include <time.h>

#include "opal/constants.h"
#include "opal/mca/threads/mutex.h"
#include "opal/runtime/opal_progress.h"

BEGIN_C_DECLS

/*
 * Combine pthread support w/ polled progress to allow run-time selection
 * of threading vs. non-threading progress.
 */

struct opal_condition_t {
    opal_object_t super;
    volatile int c_waiting;
    volatile int c_signaled;
};
typedef struct opal_condition_t opal_condition_t;

OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_condition_t);

static inline int opal_condition_wait(opal_condition_t *c, opal_mutex_t *m)
{
    int rc = OPAL_SUCCESS;
    c->c_waiting++;

    if (opal_using_threads()) {
        if (c->c_signaled) {
            c->c_waiting--;
            opal_mutex_unlock(m);
            opal_progress();
            opal_mutex_lock(m);
            return rc;
        }
        while (0 == c->c_signaled) {
            opal_mutex_unlock(m);
            opal_progress();
            opal_mutex_lock(m);
        }
    } else {
        while (0 == c->c_signaled) {
            opal_progress();
        }
    }

    c->c_signaled--;
    c->c_waiting--;
    return rc;
}

static inline int opal_condition_timedwait(opal_condition_t *c, opal_mutex_t *m,
                                           const struct timespec *abstime)
{
    struct timeval tv;
    struct timeval absolute;
    int rc = OPAL_SUCCESS;

    c->c_waiting++;
    if (opal_using_threads()) {
        absolute.tv_sec = abstime->tv_sec;
        absolute.tv_usec = abstime->tv_nsec / 1000;
        gettimeofday(&tv, NULL);
        if (0 == c->c_signaled) {
            do {
                opal_mutex_unlock(m);
                opal_progress();
                gettimeofday(&tv, NULL);
                opal_mutex_lock(m);
            } while (0 == c->c_signaled
                     && (tv.tv_sec <= absolute.tv_sec
                         || (tv.tv_sec == absolute.tv_sec && tv.tv_usec < absolute.tv_usec)));
        }
    } else {
        absolute.tv_sec = abstime->tv_sec;
        absolute.tv_usec = abstime->tv_nsec / 1000;
        gettimeofday(&tv, NULL);
        if (0 == c->c_signaled) {
            do {
                opal_progress();
                gettimeofday(&tv, NULL);
            } while (0 == c->c_signaled
                     && (tv.tv_sec <= absolute.tv_sec
                         || (tv.tv_sec == absolute.tv_sec && tv.tv_usec < absolute.tv_usec)));
        }
    }

    if (0 != c->c_signaled) {
        c->c_signaled--;
    }
    c->c_waiting--;
    return rc;
}

static inline int opal_condition_signal(opal_condition_t *c)
{
    if (c->c_waiting) {
        c->c_signaled++;
    }
    return OPAL_SUCCESS;
}

static inline int opal_condition_broadcast(opal_condition_t *c)
{
    c->c_signaled = c->c_waiting;
    return OPAL_SUCCESS;
}

END_C_DECLS

#endif /* OPAL_MCA_THREADS_CONDITION_H */