Skip to content

Commit 1da5584

Browse files
authored
Merge pull request #1911 from hjelmn/threads
opal/thread: clean up and add additional OPAL_THREAD macros
2 parents e67c3d0 + aac6112 commit 1da5584

File tree

8 files changed

+94
-77
lines changed

8 files changed

+94
-77
lines changed

ompi/mca/osc/sm/osc_sm_active_target.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
22
/*
33
* Copyright (c) 2012 Sandia National Laboratories. All rights reserved.
4-
* Copyright (c) 2014-2015 Los Alamos National Security, LLC. All rights
4+
* Copyright (c) 2014-2016 Los Alamos National Security, LLC. All rights
55
* reserved.
66
* Copyright (c) 2014 The University of Tennessee and The University
77
* of Tennessee Research Foundation. All rights
@@ -133,7 +133,7 @@ ompi_osc_sm_start(struct ompi_group_t *group,
133133

134134
OBJ_RETAIN(group);
135135

136-
if (!OPAL_ATOMIC_CMPSET(&module->start_group, NULL, group)) {
136+
if (!OPAL_ATOMIC_CMPSET_PTR(&module->start_group, NULL, group)) {
137137
OBJ_RELEASE(group);
138138
return OMPI_ERR_RMA_SYNC;
139139
}
@@ -185,7 +185,7 @@ ompi_osc_sm_complete(struct ompi_win_t *win)
185185
opal_atomic_mb();
186186

187187
group = module->start_group;
188-
if (NULL == group || !OPAL_ATOMIC_CMPSET(&module->start_group, group, NULL)) {
188+
if (NULL == group || !OPAL_ATOMIC_CMPSET_PTR(&module->start_group, group, NULL)) {
189189
return OMPI_ERR_RMA_SYNC;
190190
}
191191

ompi/mpi/c/comm_get_errhandler.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12
/*
23
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
34
* University Research and Technology
@@ -12,6 +13,8 @@
1213
* Copyright (c) 2007-2009 Cisco Systems, Inc. All rights reserved.
1314
* Copyright (c) 2015 Research Organization for Information Science
1415
* and Technology (RIST). All rights reserved.
16+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
17+
* reserved.
1518
* $COPYRIGHT$
1619
*
1720
* Additional copyrights may follow
@@ -66,7 +69,7 @@ int MPI_Comm_get_errhandler(MPI_Comm comm, MPI_Errhandler *errhandler)
6669
error_handler became atomic. */
6770
do {
6871
tmp = comm->error_handler;
69-
} while (!OPAL_ATOMIC_CMPSET(&(comm->error_handler), tmp, tmp));
72+
} while (!OPAL_ATOMIC_CMPSET_PTR(&(comm->error_handler), tmp, tmp));
7073

7174
/* Retain the errhandler, corresponding to object refcount decrease
7275
in errhandler_free.c. */

ompi/mpi/c/comm_set_errhandler.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12
/*
23
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
34
* University Research and Technology
@@ -11,6 +12,8 @@
1112
* All rights reserved.
1213
* Copyright (c) 2015 Research Organization for Information Science
1314
* and Technology (RIST). All rights reserved.
15+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
16+
* reserved.
1417
* $COPYRIGHT$
1518
*
1619
* Additional copyrights may follow
@@ -69,9 +72,7 @@ int MPI_Comm_set_errhandler(MPI_Comm comm, MPI_Errhandler errhandler)
6972
/* Ditch the old errhandler, and decrement its refcount. On 64
7073
bits environments we have to make sure the reading of the
7174
error_handler became atomic. */
72-
do {
73-
tmp = comm->error_handler;
74-
} while (!OPAL_ATOMIC_CMPSET(&(comm->error_handler), tmp, errhandler));
75+
tmp = OPAL_ATOMIC_SWAP_PTR(&comm->error_handler, errhandler);
7576
OBJ_RELEASE(tmp);
7677

7778
/* All done */

ompi/mpi/c/file_get_errhandler.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12
/*
23
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
34
* University Research and Technology
@@ -12,6 +13,8 @@
1213
* Copyright (c) 2008 Cisco Systems, Inc. All rights reserved.
1314
* Copyright (c) 2015 Research Organization for Information Science
1415
* and Technology (RIST). All rights reserved.
16+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
17+
* reserved.
1518
* $COPYRIGHT$
1619
*
1720
* Additional copyrights may follow
@@ -65,7 +68,7 @@ int MPI_File_get_errhandler( MPI_File file, MPI_Errhandler *errhandler)
6568
error_handler became atomic. */
6669
do {
6770
tmp = file->error_handler;
68-
} while (!OPAL_ATOMIC_CMPSET(&(file->error_handler), tmp, tmp));
71+
} while (!OPAL_ATOMIC_CMPSET_PTR(&(file->error_handler), tmp, tmp));
6972

7073
/* Retain the errhandler, corresponding to object refcount
7174
decrease in errhandler_free.c. */

ompi/mpi/c/file_set_errhandler.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12
/*
23
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
34
* University Research and Technology
@@ -12,6 +13,8 @@
1213
* Copyright (c) 2008 Cisco Systems, Inc. All rights reserved.
1314
* Copyright (c) 2015 Research Organization for Information Science
1415
* and Technology (RIST). All rights reserved.
16+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
17+
* reserved.
1518
* $COPYRIGHT$
1619
*
1720
* Additional copyrights may follow
@@ -68,9 +71,7 @@ int MPI_File_set_errhandler( MPI_File file, MPI_Errhandler errhandler)
6871
/* Ditch the old errhandler, and decrement its refcount. On 64
6972
bits environments we have to make sure the reading of the
7073
error_handler became atomic. */
71-
do {
72-
tmp = file->error_handler;
73-
} while (!OPAL_ATOMIC_CMPSET(&(file->error_handler), tmp, errhandler));
74+
tmp = OPAL_ATOMIC_SWAP_PTR (&file->error_handler, errhandler);
7475
OBJ_RELEASE(tmp);
7576

7677
/* All done */

ompi/mpi/c/win_get_errhandler.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12
/*
23
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
34
* University Research and Technology
@@ -12,6 +13,8 @@
1213
* Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
1314
* Copyright (c) 2015 Research Organization for Information Science
1415
* and Technology (RIST). All rights reserved.
16+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
17+
* reserved.
1518
* $COPYRIGHT$
1619
*
1720
* Additional copyrights may follow
@@ -58,7 +61,7 @@ int MPI_Win_get_errhandler(MPI_Win win, MPI_Errhandler *errhandler)
5861
error_handler became atomic. */
5962
do {
6063
tmp = win->error_handler;
61-
} while (!OPAL_ATOMIC_CMPSET(&(win->error_handler), tmp, tmp));
64+
} while (!OPAL_ATOMIC_CMPSET_PTR(&(win->error_handler), tmp, tmp));
6265

6366
/* Retain the errhandler, corresponding to object refcount
6467
decrease in errhandler_free.c. */

ompi/mpi/c/win_set_errhandler.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12
/*
23
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
34
* University Research and Technology
@@ -12,6 +13,8 @@
1213
* Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
1314
* Copyright (c) 2015 Research Organization for Information Science
1415
* and Technology (RIST). All rights reserved.
16+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
17+
* reserved.
1518
* $COPYRIGHT$
1619
*
1720
* Additional copyrights may follow
@@ -63,9 +66,7 @@ int MPI_Win_set_errhandler(MPI_Win win, MPI_Errhandler errhandler)
6366
/* Ditch the old errhandler, and decrement its refcount. On 64
6467
bits environments we have to make sure the reading of the
6568
error_handler became atomic. */
66-
do {
67-
tmp = win->error_handler;
68-
} while (!OPAL_ATOMIC_CMPSET(&(win->error_handler), tmp, errhandler));
69+
tmp = OPAL_ATOMIC_SWAP_PTR(&win->error_handler, errhandler);
6970
OBJ_RELEASE(tmp);
7071

7172
/* All done */

opal/threads/thread_usage.h

Lines changed: 67 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -93,82 +93,87 @@ static inline bool opal_set_using_threads(bool have)
9393
* indicates that threads are in use by the application or library.
9494
*/
9595

96-
static inline int32_t
97-
OPAL_THREAD_ADD32(volatile int32_t *addr, int delta)
98-
{
99-
int32_t ret;
96+
#define OPAL_THREAD_DEFINE_ATOMIC_ADD(type, suffix) \
97+
static inline type opal_thread_add_ ## suffix (volatile type *addr, type delta) \
98+
{ \
99+
if (OPAL_UNLIKELY(opal_using_threads())) { \
100+
return opal_atomic_add_ ## suffix (addr, delta); \
101+
} \
102+
\
103+
return (*addr += delta); \
104+
}
100105

101-
if (OPAL_UNLIKELY(opal_using_threads())) {
102-
ret = opal_atomic_add_32(addr, delta);
103-
} else {
104-
ret = (*addr += delta);
105-
}
106+
#define OPAL_THREAD_DEFINE_ATOMIC_CMPSET(type, addr_type, suffix) \
107+
static inline bool opal_thread_cmpset_bool_ ## suffix (volatile addr_type *addr, type compare, type value) \
108+
{ \
109+
type ret; \
110+
\
111+
if (OPAL_UNLIKELY(opal_using_threads())) { \
112+
return opal_atomic_cmpset_ ## suffix ((volatile type *) addr, compare, value); \
113+
} \
114+
\
115+
if ((type) *addr == compare) { \
116+
((type *) addr)[0] = value; \
117+
return true; \
118+
} \
119+
\
120+
return false; \
121+
}
106122

107-
return ret;
123+
#define OPAL_THREAD_DEFINE_ATOMIC_SWAP(type, addr_type, suffix) \
124+
static inline type opal_thread_swap_ ## suffix (volatile addr_type *ptr, type newvalue) \
125+
{ \
126+
if (opal_using_threads ()) { \
127+
return opal_atomic_swap_ ## suffix ((volatile type *) ptr, newvalue); \
128+
} \
129+
\
130+
type old = ((type *) ptr)[0]; \
131+
((type *) ptr)[0] = newvalue; \
132+
\
133+
return old; \
108134
}
109135

110-
#if OPAL_HAVE_ATOMIC_MATH_64
111-
static inline int64_t
112-
OPAL_THREAD_ADD64(volatile int64_t *addr, int delta)
113-
{
114-
int64_t ret;
136+
OPAL_THREAD_DEFINE_ATOMIC_ADD(int32_t, 32)
137+
OPAL_THREAD_DEFINE_ATOMIC_ADD(size_t, size_t)
138+
OPAL_THREAD_DEFINE_ATOMIC_CMPSET(int32_t, int32_t, 32)
139+
OPAL_THREAD_DEFINE_ATOMIC_CMPSET(void *, intptr_t, ptr)
140+
OPAL_THREAD_DEFINE_ATOMIC_SWAP(int32_t, int32_t, 32)
141+
OPAL_THREAD_DEFINE_ATOMIC_SWAP(void *, intptr_t, ptr)
115142

116-
if (OPAL_UNLIKELY(opal_using_threads())) {
117-
ret = opal_atomic_add_64(addr, delta);
118-
} else {
119-
ret = (*addr += delta);
120-
}
143+
#define OPAL_THREAD_ADD32 opal_thread_add_32
144+
#define OPAL_ATOMIC_ADD32 opal_thread_add_32
121145

122-
return ret;
123-
}
124-
#endif
146+
#define OPAL_THREAD_ADD_SIZE_T opal_thread_add_size_t
147+
#define OPAL_ATOMIC_ADD_SIZE_T opal_thread_add_size_t
125148

126-
static inline size_t
127-
OPAL_THREAD_ADD_SIZE_T(volatile size_t *addr, int delta)
128-
{
129-
size_t ret;
149+
#define OPAL_THREAD_CMPSET_32 opal_thread_cmpset_bool_32
150+
#define OPAL_ATOMIC_CMPSET_32 opal_thread_cmpset_bool_32
130151

131-
if (OPAL_UNLIKELY(opal_using_threads())) {
132-
ret = opal_atomic_add_size_t(addr, delta);
133-
} else {
134-
ret = (*addr += delta);
135-
}
152+
#define OPAL_THREAD_CMPSET_PTR(x, y, z) opal_thread_cmpset_bool_ptr ((volatile intptr_t *) x, (void *) y, (void *) z)
153+
#define OPAL_ATOMIC_CMPSET_PTR OPAL_THREAD_CMPSET_PTR
136154

137-
return ret;
138-
}
155+
#define OPAL_THREAD_SWAP_32 opal_thread_swap_32
156+
#define OPAL_ATOMIC_SWAP_32 opal_thread_swap_32
139157

140-
/* BWB: FIX ME: remove if possible */
141-
#define OPAL_CMPSET(x, y, z) ((*(x) == (y)) ? ((*(x) = (z)), 1) : 0)
158+
#define OPAL_THREAD_SWAP_PTR(x, y) opal_thread_swap_ptr ((volatile intptr_t *) x, (void *) y)
159+
#define OPAL_ATOMIC_SWAP_PTR OPAL_THREAD_SWAP_PTR
142160

143-
#if OPAL_HAVE_ATOMIC_CMPSET_32
144-
#define OPAL_ATOMIC_CMPSET_32(x, y, z) \
145-
(opal_using_threads() ? opal_atomic_cmpset_32(x, y, z) : OPAL_CMPSET(x, y, z))
146-
#endif
147-
#if OPAL_HAVE_ATOMIC_CMPSET_64
148-
#define OPAL_ATOMIC_CMPSET_64(x, y, z) \
149-
(opal_using_threads() ? opal_atomic_cmpset_64(x, y, z) : OPAL_CMPSET(x, y, z))
150-
#endif
151-
#if OPAL_HAVE_ATOMIC_CMPSET_32 || OPAL_HAVE_ATOMIC_CMPSET_64
152-
#define OPAL_ATOMIC_CMPSET(x, y, z) \
153-
(opal_using_threads() ? opal_atomic_cmpset(x, y, z) : OPAL_CMPSET(x, y, z))
154-
#endif
155-
#if OPAL_HAVE_ATOMIC_CMPSET_32 || OPAL_HAVE_ATOMIC_CMPSET_64
156-
#define OPAL_ATOMIC_CMPSET_PTR(x, y, z) \
157-
(opal_using_threads() ? opal_atomic_cmpset_ptr(x, y, z) : OPAL_CMPSET(x, y, z))
158-
#endif
161+
/* define 64-bit macros is 64-bit atomic math is available */
162+
#if OPAL_HAVE_ATOMIC_MATH_64
159163

160-
static inline void *opal_thread_swap_ptr (volatile void *ptr, void *newvalue)
161-
{
162-
if (opal_using_threads ()) {
163-
return opal_atomic_swap_ptr (ptr, newvalue);
164-
}
164+
OPAL_THREAD_DEFINE_ATOMIC_ADD(int64_t, 64)
165+
OPAL_THREAD_DEFINE_ATOMIC_CMPSET(int64_t, int64_t, 64)
166+
OPAL_THREAD_DEFINE_ATOMIC_SWAP(int64_t, int64_t, 64)
165167

166-
void *old = ((void **) ptr)[0];
167-
((void **) ptr)[0] = newvalue;
168+
#define OPAL_THREAD_ADD64 opal_thread_add_64
169+
#define OPAL_ATOMIC_ADD64 opal_thread_add_64
168170

169-
return old;
170-
}
171+
#define OPAL_THREAD_CMPSET_64 opal_thread_cmpset_bool_64
172+
#define OPAL_ATOMIC_CMPSET_64 opal_thread_cmpset_bool_64
171173

172-
#define OPAL_ATOMIC_SWAP_PTR(x, y) opal_thread_swap_ptr (x, y)
174+
#define OPAL_THREAD_SWAP_64 opal_thread_swap_64
175+
#define OPAL_ATOMIC_SWAP_64 opal_thread_swap_64
176+
177+
#endif
173178

174179
#endif /* !defined(OPAL_THREAD_USAGE_H) */

0 commit comments

Comments
 (0)