ARGOBOTS  1.1
mutex.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 
40 {
41  /* Check if the size of ABT_mutex_memory is okay. */
43 
44 #ifndef ABT_CONFIG_ENABLE_VER_20_API
45  /* Argobots 1.x sets newmutex to NULL on error. */
46  *newmutex = ABT_MUTEX_NULL;
47 #endif
48  ABTI_mutex *p_newmutex;
49 
50  int abt_errno = ABTU_malloc(sizeof(ABTI_mutex), (void **)&p_newmutex);
51  ABTI_CHECK_ERROR(abt_errno);
52  ABTI_mutex_init(p_newmutex);
53 
54  /* Return value */
55  *newmutex = ABTI_mutex_get_handle(p_newmutex);
56  return ABT_SUCCESS;
57 }
58 
99 {
100 #ifndef ABT_CONFIG_ENABLE_VER_20_API
101  /* Argobots 1.x sets newmutex to NULL on error. */
102  *newmutex = ABT_MUTEX_NULL;
103 #endif
105  ABTI_mutex *p_newmutex;
106 
107  int abt_errno = ABTU_malloc(sizeof(ABTI_mutex), (void **)&p_newmutex);
108  ABTI_CHECK_ERROR(abt_errno);
109 
110  ABTI_mutex_init(p_newmutex);
111  if (p_attr)
112  p_newmutex->attrs = p_attr->attrs;
113 
114  /* Return value */
115  *newmutex = ABTI_mutex_get_handle(p_newmutex);
116  return ABT_SUCCESS;
117 }
118 
146 {
147  ABT_mutex h_mutex = *mutex;
148  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(h_mutex);
149  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
150 
151  ABTU_free(p_mutex);
152 
153  /* Return value */
154  *mutex = ABT_MUTEX_NULL;
155  return ABT_SUCCESS;
156 }
157 
182 {
183  ABTI_local *p_local = ABTI_local_get_local();
184  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
185  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
186  ABTI_mutex_lock(&p_local, p_mutex);
187  return ABT_SUCCESS;
188 }
189 
216 {
217  ABTI_local *p_local = ABTI_local_get_local();
218  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
219  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
220  ABTI_mutex_lock(&p_local, p_mutex);
221  return ABT_SUCCESS;
222 }
223 
250 {
251  ABTI_local *p_local = ABTI_local_get_local();
252  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
253  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
254  ABTI_mutex_lock(&p_local, p_mutex);
255  return ABT_SUCCESS;
256 }
257 
285 {
286  ABTI_local *p_local = ABTI_local_get_local();
287  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
288  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
289  int abt_errno = ABTI_mutex_trylock(p_local, p_mutex);
290  /* Trylock always needs to return an error code. */
291  return abt_errno;
292 }
293 
325 {
326  ABTI_local *p_local = ABTI_local_get_local();
327  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
328  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
329  ABTI_mutex_spinlock(p_local, p_mutex);
330  return ABT_SUCCESS;
331 }
332 
358 {
359  ABTI_local *p_local = ABTI_local_get_local();
360  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
361  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
362  ABTI_mutex_unlock(p_local, p_mutex);
363  return ABT_SUCCESS;
364 }
365 
402 {
403  ABTI_local *p_local = ABTI_local_get_local();
404  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
405  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
406  ABTI_mutex_unlock(p_local, p_mutex);
407  return ABT_SUCCESS;
408 }
409 
446 {
447  ABTI_local *p_local = ABTI_local_get_local();
448  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
449  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
450  ABTI_mutex_unlock(p_local, p_mutex);
451  return ABT_SUCCESS;
452 }
453 
481 int ABT_mutex_equal(ABT_mutex mutex1, ABT_mutex mutex2, ABT_bool *result)
482 {
483  ABTI_mutex *p_mutex1 = ABTI_mutex_get_ptr(mutex1);
484  ABTI_mutex *p_mutex2 = ABTI_mutex_get_ptr(mutex2);
485  *result = (p_mutex1 == p_mutex2) ? ABT_TRUE : ABT_FALSE;
486  return ABT_SUCCESS;
487 }
488 
515 {
516  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
517  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
518 
519  ABTI_mutex_attr *p_newattr;
520  int abt_errno = ABTU_malloc(sizeof(ABTI_mutex_attr), (void **)&p_newattr);
521  ABTI_CHECK_ERROR(abt_errno);
522 
523  /* Copy values. Nesting count must be initialized. */
524  p_newattr->attrs = p_mutex->attrs;
525 
526  /* Return value */
527  *attr = ABTI_mutex_attr_get_handle(p_newattr);
528  return ABT_SUCCESS;
529 }
ABT_mutex_lock_low
int ABT_mutex_lock_low(ABT_mutex mutex)
Lock a mutex with low priority.
Definition: mutex.c:215
ABTI_mutex_get_ptr
static ABTI_mutex * ABTI_mutex_get_ptr(ABT_mutex mutex)
Definition: abti_mutex.h:9
ABT_bool
int ABT_bool
Boolean type.
Definition: abt.h:1001
ABTI_mutex_lock
static void ABTI_mutex_lock(ABTI_local **pp_local, ABTI_mutex *p_mutex)
Definition: abti_mutex.h:98
ABTI_mutex_attr::attrs
int attrs
Definition: abti.h:171
ABTI_CHECK_ERROR
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:120
ABT_mutex
struct ABT_mutex_opaque * ABT_mutex
Mutex handle type.
Definition: abt.h:945
ABT_mutex_unlock_de
int ABT_mutex_unlock_de(ABT_mutex mutex)
Unlock a mutex and try to hand it over a waiter associated with an execution stream that is different...
Definition: mutex.c:445
ABT_mutex_lock
int ABT_mutex_lock(ABT_mutex mutex)
Lock a mutex.
Definition: mutex.c:181
ABTI_mutex::attrs
int attrs
Definition: abti.h:175
ABTI_mutex_unlock
static void ABTI_mutex_unlock(ABTI_local *p_local, ABTI_mutex *p_mutex)
Definition: abti_mutex.h:181
ABT_mutex_equal
int ABT_mutex_equal(ABT_mutex mutex1, ABT_mutex mutex2, ABT_bool *result)
Compare two mutex handles for equality.
Definition: mutex.c:481
ABTI_mutex_attr_get_handle
static ABT_mutex_attr ABTI_mutex_attr_get_handle(ABTI_mutex_attr *p_attr)
Definition: abti_mutex_attr.h:26
ABTI_mutex_attr_get_ptr
static ABTI_mutex_attr * ABTI_mutex_attr_get_ptr(ABT_mutex_attr attr)
Definition: abti_mutex_attr.h:11
ABT_mutex_trylock
int ABT_mutex_trylock(ABT_mutex mutex)
Attempt to lock a mutex.
Definition: mutex.c:284
ABTI_CHECK_NULL_MUTEX_PTR
#define ABTI_CHECK_NULL_MUTEX_PTR(p)
Definition: abti_error.h:240
abti.h
ABT_mutex_attr
struct ABT_mutex_attr_opaque * ABT_mutex_attr
Mutex attribute handle type.
Definition: abt.h:952
ABT_mutex_create
int ABT_mutex_create(ABT_mutex *newmutex)
Create a new mutex.
Definition: mutex.c:39
ABT_mutex_lock_high
int ABT_mutex_lock_high(ABT_mutex mutex)
Lock a mutex with high priority.
Definition: mutex.c:249
ABTU_malloc
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:262
ABTI_local_get_local
static ABTI_local * ABTI_local_get_local(void)
Definition: abti_local.h:41
ABTI_STATIC_ASSERT
#define ABTI_STATIC_ASSERT(cond)
Definition: abti_error.h:19
ABT_SUCCESS
#define ABT_SUCCESS
Error code: the routine returns successfully.
Definition: abt.h:92
ABT_TRUE
#define ABT_TRUE
True constant for ABT_bool.
Definition: abt.h:748
ABT_FALSE
#define ABT_FALSE
False constant for ABT_bool.
Definition: abt.h:750
ABTI_mutex_attr
Definition: abti.h:170
ABT_mutex_spinlock
int ABT_mutex_spinlock(ABT_mutex mutex)
Lock a mutex in a busy-wait form.
Definition: mutex.c:324
ABTI_mutex_spinlock
static void ABTI_mutex_spinlock(ABTI_local *p_local, ABTI_mutex *p_mutex)
Definition: abti_mutex.h:149
ABTU_free
static void ABTU_free(void *ptr)
Definition: abtu.h:217
ABTI_mutex_trylock
static int ABTI_mutex_trylock(ABTI_local *p_local, ABTI_mutex *p_mutex)
Definition: abti_mutex.h:122
ABT_mutex_unlock_se
int ABT_mutex_unlock_se(ABT_mutex mutex)
Unlock a mutex and try to hand it over a waiter associated with the same execution stream.
Definition: mutex.c:401
ABT_mutex_memory
A struct that can be converted to ABT_mutex.
Definition: abt.h:1089
ABTI_local
struct ABTI_local ABTI_local
Definition: abti.h:110
ABT_MUTEX_NULL
#define ABT_MUTEX_NULL
Definition: abt.h:1066
ABT_mutex_create_with_attr
int ABT_mutex_create_with_attr(ABT_mutex_attr attr, ABT_mutex *newmutex)
Create a new mutex with mutex attributes.
Definition: mutex.c:98
ABT_mutex_unlock
int ABT_mutex_unlock(ABT_mutex mutex)
Unlock a mutex.
Definition: mutex.c:357
ABTI_mutex_init
static void ABTI_mutex_init(ABTI_mutex *p_mutex)
Definition: abti_mutex.h:39
ABT_mutex_get_attr
int ABT_mutex_get_attr(ABT_mutex mutex, ABT_mutex_attr *attr)
Get attributes of a mutex.
Definition: mutex.c:514
ABTI_mutex_get_handle
static ABT_mutex ABTI_mutex_get_handle(ABTI_mutex *p_mutex)
Definition: abti_mutex.h:24
ABT_mutex_free
int ABT_mutex_free(ABT_mutex *mutex)
Free a mutex.
Definition: mutex.c:145
ABTI_mutex
Definition: abti.h:174