ARGOBOTS  1.1
barrier.c
Go to the documentation of this file.
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  * See COPYRIGHT in top-level directory.
4  */
5 
6 #include "abti.h"
7 
47 int ABT_barrier_create(uint32_t num_waiters, ABT_barrier *newbarrier)
48 {
49 #ifndef ABT_CONFIG_ENABLE_VER_20_API
50  /* Argobots 1.x sets newbarrier to NULL on error. */
51  *newbarrier = ABT_BARRIER_NULL;
52 #endif
53  int abt_errno;
54  ABTI_barrier *p_newbarrier;
55  ABTI_CHECK_TRUE(num_waiters != 0, ABT_ERR_INV_ARG);
56  size_t arg_num_waiters = num_waiters;
57 
58  abt_errno = ABTU_malloc(sizeof(ABTI_barrier), (void **)&p_newbarrier);
59  ABTI_CHECK_ERROR(abt_errno);
60 
61  ABTD_spinlock_clear(&p_newbarrier->lock);
62  p_newbarrier->num_waiters = arg_num_waiters;
63  p_newbarrier->counter = 0;
64  ABTI_waitlist_init(&p_newbarrier->waitlist);
65  /* Return value */
66  *newbarrier = ABTI_barrier_get_handle(p_newbarrier);
67  return ABT_SUCCESS;
68 }
69 
99 int ABT_barrier_reinit(ABT_barrier barrier, uint32_t num_waiters)
100 {
101  ABTI_barrier *p_barrier = ABTI_barrier_get_ptr(barrier);
102  ABTI_CHECK_NULL_BARRIER_PTR(p_barrier);
103  ABTI_ASSERT(p_barrier->counter == 0);
104  ABTI_CHECK_TRUE(num_waiters != 0, ABT_ERR_INV_ARG);
105  size_t arg_num_waiters = num_waiters;
106 
107  /* Only when num_waiters is different from p_barrier->num_waiters, we
108  * change p_barrier. */
109  if (arg_num_waiters != p_barrier->num_waiters) {
110  /* We can reuse waiters and waiter_type arrays */
111  p_barrier->num_waiters = arg_num_waiters;
112  }
113  return ABT_SUCCESS;
114 }
115 
140 {
141  ABT_barrier h_barrier = *barrier;
142  ABTI_barrier *p_barrier = ABTI_barrier_get_ptr(h_barrier);
143  ABTI_CHECK_NULL_BARRIER_PTR(p_barrier);
144 
145  /* The lock needs to be acquired to safely free the barrier structure.
146  * However, we do not have to unlock it because the entire structure is
147  * freed here. */
148  ABTD_spinlock_acquire(&p_barrier->lock);
149 
150  /* p_barrier->counter must be checked after taking a lock. */
151  ABTI_ASSERT(p_barrier->counter == 0);
152 
153  ABTU_free(p_barrier);
154 
155  /* Return value */
156  *barrier = ABT_BARRIER_NULL;
157  return ABT_SUCCESS;
158 }
159 
190 {
191  ABTI_local *p_local = ABTI_local_get_local();
192  ABTI_barrier *p_barrier = ABTI_barrier_get_ptr(barrier);
193  ABTI_CHECK_NULL_BARRIER_PTR(p_barrier);
194 
195 #ifndef ABT_CONFIG_ENABLE_VER_20_API
196  /* Calling a barrier on a tasklet is not allowed. */
197  if (ABTI_IS_ERROR_CHECK_ENABLED && p_local) {
198  ABTI_xstream *p_local_xstream = ABTI_local_get_xstream(p_local);
199  ABTI_CHECK_TRUE(p_local_xstream->p_thread->type &
202  }
203 #endif
204 
205  ABTD_spinlock_acquire(&p_barrier->lock);
206 
207  ABTI_ASSERT(p_barrier->counter < p_barrier->num_waiters);
208  p_barrier->counter++;
209 
210  /* If we do not have all the waiters yet */
211  if (p_barrier->counter < p_barrier->num_waiters) {
212  ABTI_waitlist_wait_and_unlock(&p_local, &p_barrier->waitlist,
213  &p_barrier->lock,
215  (void *)p_barrier);
216  } else {
217  ABTI_waitlist_broadcast(p_local, &p_barrier->waitlist);
218  /* Reset counter */
219  p_barrier->counter = 0;
220  ABTD_spinlock_release(&p_barrier->lock);
221  }
222  return ABT_SUCCESS;
223 }
224 
248 int ABT_barrier_get_num_waiters(ABT_barrier barrier, uint32_t *num_waiters)
249 {
250  ABTI_barrier *p_barrier = ABTI_barrier_get_ptr(barrier);
251  ABTI_CHECK_NULL_BARRIER_PTR(p_barrier);
252 
253  *num_waiters = p_barrier->num_waiters;
254  return ABT_SUCCESS;
255 }
ABT_ERR_BARRIER
#define ABT_ERR_BARRIER
Error code: error related to a barrier.
Definition: abt.h:350
disclaimer
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following disclaimer
Definition: LICENSE_1_0.txt:11
ABTI_barrier::counter
volatile size_t counter
Definition: abti.h:467
following
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the following
Definition: LICENSE_1_0.txt:10
ABTI_barrier_get_ptr
static ABTI_barrier * ABTI_barrier_get_ptr(ABT_barrier barrier)
Definition: abti_barrier.h:12
ABTI_waitlist_broadcast
static void ABTI_waitlist_broadcast(ABTI_local *p_local, ABTI_waitlist *p_waitlist)
Definition: abti_waitlist.h:247
CONTRACT
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN CONTRACT
Definition: LICENSE_1_0.txt:21
ABTI_barrier::waitlist
ABTI_waitlist waitlist
Definition: abti.h:469
MERCHANTABILITY
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
Definition: LICENSE_1_0.txt:18
grant
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license grant
Definition: LICENSE_1_0.txt:11
ABTI_CHECK_ERROR
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:120
license
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this license(the "Software") to use
ABTI_xstream::type
ABTI_xstream_type type
Definition: abti.h:270
ABTI_THREAD_TYPE_YIELDABLE
#define ABTI_THREAD_TYPE_YIELDABLE
Definition: abti.h:86
IMPLIED
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR IMPLIED
Definition: LICENSE_1_0.txt:18
so
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do so
Definition: LICENSE_1_0.txt:8
ABTI_barrier_get_handle
static ABT_barrier ABTI_barrier_get_handle(ABTI_barrier *p_barrier)
Definition: abti_barrier.h:27
ABTI_IS_ERROR_CHECK_ENABLED
#define ABTI_IS_ERROR_CHECK_ENABLED
Definition: abti.h:20
ABTI_barrier
Definition: abti.h:465
ABTI_xstream
Definition: abti.h:264
LIABILITY
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY
Definition: LICENSE_1_0.txt:21
IS
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS IS
Definition: LICENSE_1_0.txt:17
abti.h
FROM
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN TORT OR ARISING FROM
Definition: LICENSE_1_0.txt:22
ABTD_spinlock_acquire
static void ABTD_spinlock_acquire(ABTD_spinlock *p_lock)
Definition: abtd_spinlock.h:28
ABT_barrier
struct ABT_barrier_opaque * ABT_barrier
Barrier handle type.
Definition: abt.h:987
PURPOSE
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE
Definition: LICENSE_1_0.txt:19
ABTI_barrier::num_waiters
size_t num_waiters
Definition: abti.h:466
ABTU_malloc
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:262
ABT_barrier_free
int ABT_barrier_free(ABT_barrier *barrier)
Free a barrier.
Definition: barrier.c:139
ABTI_CHECK_NULL_BARRIER_PTR
#define ABTI_CHECK_NULL_BARRIER_PTR(p)
Definition: abti_error.h:294
part
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in part
Definition: LICENSE_1_0.txt:12
ABTI_ASSERT
#define ABTI_ASSERT(cond)
Definition: abti_error.h:12
ABTI_local_get_local
static ABTI_local * ABTI_local_get_local(void)
Definition: abti_local.h:41
charge
Boost Software License Version Permission is hereby free of charge
Definition: LICENSE_1_0.txt:3
distribute
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this distribute
Definition: LICENSE_1_0.txt:5
ABT_SUCCESS
#define ABT_SUCCESS
Error code: the routine returns successfully.
Definition: abt.h:92
ABT_SYNC_EVENT_TYPE_BARRIER
@ ABT_SYNC_EVENT_TYPE_BARRIER
Definition: abt.h:682
ABT_BARRIER_NULL
#define ABT_BARRIER_NULL
Definition: abt.h:1072
ABTD_spinlock_clear
static void ABTD_spinlock_clear(ABTD_spinlock *p_lock)
Definition: abtd_spinlock.h:23
ABT_ERR_INV_ARG
#define ABT_ERR_INV_ARG
Error code: invalid user argument.
Definition: abt.h:250
ABTU_free
static void ABTU_free(void *ptr)
Definition: abtu.h:217
ABTD_spinlock_release
static void ABTD_spinlock_release(ABTD_spinlock *p_lock)
Definition: abtd_spinlock.h:42
ABTI_barrier::lock
ABTD_spinlock lock
Definition: abti.h:468
OTHERWISE
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN TORT OR OTHERWISE
Definition: LICENSE_1_0.txt:21
Software
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the Software
Definition: LICENSE_1_0.txt:6
KIND
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY KIND
Definition: LICENSE_1_0.txt:17
ABTI_local
struct ABTI_local ABTI_local
Definition: abti.h:110
granted
Boost Software License Version Permission is hereby granted
Definition: LICENSE_1_0.txt:3
ABTI_CHECK_TRUE
#define ABTI_CHECK_TRUE(cond, abt_errno)
Definition: abti_error.h:130
ABTI_waitlist_init
static void ABTI_waitlist_init(ABTI_waitlist *p_waitlist)
Definition: abti_waitlist.h:11
ABT_barrier_reinit
int ABT_barrier_reinit(ABT_barrier barrier, uint32_t num_waiters)
Reinitialize a barrier with a new number of waiters.
Definition: barrier.c:99
execute
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this execute
Definition: LICENSE_1_0.txt:6
August
Boost Software License Version August
Definition: LICENSE_1_0.txt:1
ABTI_local_get_xstream
static ABTI_xstream * ABTI_local_get_xstream(ABTI_local *p_local)
Definition: abti_local.h:86
ABTI_waitlist_wait_and_unlock
static void ABTI_waitlist_wait_and_unlock(ABTI_local **pp_local, ABTI_waitlist *p_waitlist, ABTD_spinlock *p_lock, ABT_sync_event_type sync_event_type, void *p_sync)
Definition: abti_waitlist.h:21
display
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this display
Definition: LICENSE_1_0.txt:5
reproduce
Boost Software License Version Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this reproduce
Definition: LICENSE_1_0.txt:5
ABT_barrier_create
int ABT_barrier_create(uint32_t num_waiters, ABT_barrier *newbarrier)
Create a new barrier.
Definition: barrier.c:47
ABT_barrier_wait
int ABT_barrier_wait(ABT_barrier barrier)
Wait on a barrier.
Definition: barrier.c:189
ABT_barrier_get_num_waiters
int ABT_barrier_get_num_waiters(ABT_barrier barrier, uint32_t *num_waiters)
Get the number of waiters of a barrier.
Definition: barrier.c:248