summaryrefslogtreecommitdiffstats
path: root/src/mercury/mercury_thread.c
blob: 1c0e9769fa9b4bc146a8604c16f5401a94def599 (plain)
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
/*
 * Copyright (C) 2013-2019 Argonne National Laboratory, Department of Energy,
 *                    UChicago Argonne, LLC and The HDF Group.
 * All rights reserved.
 *
 * The full copyright notice, including terms governing use, modification,
 * and redistribution, is contained in the COPYING file that can be
 * found at the root of the source code distribution tree.
 */

#include "mercury_thread.h"

/*---------------------------------------------------------------------------*/
void
hg_thread_init(hg_thread_t *thread)
{
#ifdef _WIN32
    *thread = NULL;
#else
    *thread = 0;
#endif
}

/*---------------------------------------------------------------------------*/
int
hg_thread_create(hg_thread_t *thread, hg_thread_func_t f, void *data)
{
#ifdef _WIN32
    *thread = CreateThread(NULL, 0, f, data, 0, NULL);
    if (*thread == NULL)
        return HG_UTIL_FAIL;
#else
    if (pthread_create(thread, NULL, f, data))
        return HG_UTIL_FAIL;
#endif

    return HG_UTIL_SUCCESS;
}

/*---------------------------------------------------------------------------*/
void
hg_thread_exit(hg_thread_ret_t ret)
{
#ifdef _WIN32
    ExitThread(ret);
#else
    pthread_exit(ret);
#endif
}

/*---------------------------------------------------------------------------*/
int
hg_thread_join(hg_thread_t thread)
{
#ifdef _WIN32
    WaitForSingleObject(thread, INFINITE);
    CloseHandle(thread);
#else
    if (pthread_join(thread, NULL))
        return HG_UTIL_FAIL;
#endif

    return HG_UTIL_SUCCESS;
}

/*---------------------------------------------------------------------------*/
int
hg_thread_cancel(hg_thread_t thread)
{
#ifdef _WIN32
    WaitForSingleObject(thread, 0);
    CloseHandle(thread);
#else
    if (pthread_cancel(thread))
        return HG_UTIL_FAIL;
#endif

    return HG_UTIL_SUCCESS;
}

/*---------------------------------------------------------------------------*/
int
hg_thread_yield(void)
{
#ifdef _WIN32
    SwitchToThread();
#elif defined(__APPLE__)
    pthread_yield_np();
#else
    pthread_yield();
#endif

    return HG_UTIL_SUCCESS;
}

/*---------------------------------------------------------------------------*/
int
hg_thread_key_create(hg_thread_key_t *key)
{
    if (!key)
        return HG_UTIL_FAIL;

#ifdef _WIN32
    if ((*key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
        return HG_UTIL_FAIL;
#else
    if (pthread_key_create(key, NULL))
        return HG_UTIL_FAIL;
#endif

    return HG_UTIL_SUCCESS;
}

/*---------------------------------------------------------------------------*/
int
hg_thread_key_delete(hg_thread_key_t key)
{
#ifdef _WIN32
    if (!TlsFree(key))
        return HG_UTIL_FAIL;
#else
    if (pthread_key_delete(key))
        return HG_UTIL_FAIL;
#endif

    return HG_UTIL_SUCCESS;
}

/*---------------------------------------------------------------------------*/
int
hg_thread_getaffinity(hg_thread_t thread, hg_cpu_set_t *cpu_mask)
{
#if defined(_WIN32)
    return HG_UTIL_FAIL;
#elif defined(__APPLE__)
    (void) thread;
    (void) cpu_mask;
    return HG_UTIL_FAIL;
#else
    if (pthread_getaffinity_np(thread, sizeof(hg_cpu_set_t), cpu_mask))
        return HG_UTIL_FAIL;
    return HG_UTIL_SUCCESS;
#endif
}

/*---------------------------------------------------------------------------*/
int
hg_thread_setaffinity(hg_thread_t thread, const hg_cpu_set_t *cpu_mask)
{
#if defined(_WIN32)
    if (!SetThreadAffinityMask(thread, *cpu_mask))
        return HG_UTIL_FAIL;
#elif defined(__APPLE__)
    (void) thread;
    (void) cpu_mask;
    return HG_UTIL_FAIL;
#else
    if (pthread_setaffinity_np(thread, sizeof(hg_cpu_set_t), cpu_mask))
        return HG_UTIL_FAIL;
    return HG_UTIL_SUCCESS;
#endif
}