ARGOBOTS  4dc37e16e1b227a480715ab67dae1dcfb4d2d4e0
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 {
42  ABTI_UB_ASSERT(newmutex);
43 
44  /* Check if the size of ABT_mutex_memory is okay. */
46 
47 #ifndef ABT_CONFIG_ENABLE_VER_20_API
48  /* Argobots 1.x sets newmutex to NULL on error. */
49  *newmutex = ABT_MUTEX_NULL;
50 #endif
51  ABTI_mutex *p_newmutex;
52 
53  int abt_errno = ABTU_malloc(sizeof(ABTI_mutex), (void **)&p_newmutex);
54  ABTI_CHECK_ERROR(abt_errno);
55  ABTI_mutex_init(p_newmutex);
56 
57  /* Return value */
58  *newmutex = ABTI_mutex_get_handle(p_newmutex);
59  return ABT_SUCCESS;
60 }
61 
102 {
104  ABTI_UB_ASSERT(newmutex);
105 
106 #ifndef ABT_CONFIG_ENABLE_VER_20_API
107  /* Argobots 1.x sets newmutex to NULL on error. */
108  *newmutex = ABT_MUTEX_NULL;
109 #endif
111  ABTI_mutex *p_newmutex;
112 
113  int abt_errno = ABTU_malloc(sizeof(ABTI_mutex), (void **)&p_newmutex);
114  ABTI_CHECK_ERROR(abt_errno);
115 
116  ABTI_mutex_init(p_newmutex);
117  if (p_attr)
118  p_newmutex->attrs = p_attr->attrs;
119 
120  /* Return value */
121  *newmutex = ABTI_mutex_get_handle(p_newmutex);
122  return ABT_SUCCESS;
123 }
124 
152 {
154  ABTI_UB_ASSERT(mutex);
155 
156  ABT_mutex h_mutex = *mutex;
157  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(h_mutex);
158  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
159  ABTU_free(p_mutex);
160 
161  /* Return value */
162  *mutex = ABT_MUTEX_NULL;
163  return ABT_SUCCESS;
164 }
165 
190 {
191  ABTI_local *p_local = ABTI_local_get_local();
192  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
193  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
194  ABTI_mutex_lock(&p_local, p_mutex);
195  return ABT_SUCCESS;
196 }
197 
224 {
225  ABTI_local *p_local = ABTI_local_get_local();
226  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
227  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
228  ABTI_mutex_lock(&p_local, p_mutex);
229  return ABT_SUCCESS;
230 }
231 
258 {
259  ABTI_local *p_local = ABTI_local_get_local();
260  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
261  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
262  ABTI_mutex_lock(&p_local, p_mutex);
263  return ABT_SUCCESS;
264 }
265 
293 {
294  ABTI_local *p_local = ABTI_local_get_local();
295  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
296  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
297  int abt_errno = ABTI_mutex_trylock(p_local, p_mutex);
298  /* Trylock always needs to return an error code. */
299  return abt_errno;
300 }
301 
333 {
334  ABTI_local *p_local = ABTI_local_get_local();
335  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
336  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
337  ABTI_mutex_spinlock(p_local, p_mutex);
338  return ABT_SUCCESS;
339 }
340 
366 {
367  ABTI_local *p_local = ABTI_local_get_local();
368  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
369  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
370 
371  /* Check if a given mutex is legal. */
372  /* p_mutex must be locked. */
374  /* If p_mutex is recursive, the caller of this function must be an owner. */
376  p_mutex->owner_id != ABTI_self_get_thread_id(p_local)));
377 
378  ABTI_mutex_unlock(p_local, p_mutex);
379  return ABT_SUCCESS;
380 }
381 
418 {
419  ABTI_local *p_local = ABTI_local_get_local();
420  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
421  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
422 
423  /* Check if a given mutex is legal. */
424  /* p_mutex must be locked. */
426  /* If p_mutex is recursive, the caller of this function must be an owner. */
428  p_mutex->owner_id != ABTI_self_get_thread_id(p_local)));
429 
430  ABTI_mutex_unlock(p_local, p_mutex);
431  return ABT_SUCCESS;
432 }
433 
470 {
471  ABTI_local *p_local = ABTI_local_get_local();
472  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
473  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
474 
475  /* Check if a given mutex is legal. */
476  /* p_mutex must be locked. */
478  /* If p_mutex is recursive, the caller of this function must be an owner. */
480  p_mutex->owner_id != ABTI_self_get_thread_id(p_local)));
481 
482  ABTI_mutex_unlock(p_local, p_mutex);
483  return ABT_SUCCESS;
484 }
485 
513 int ABT_mutex_equal(ABT_mutex mutex1, ABT_mutex mutex2, ABT_bool *result)
514 {
515  ABTI_UB_ASSERT(result);
516 
517  ABTI_mutex *p_mutex1 = ABTI_mutex_get_ptr(mutex1);
518  ABTI_mutex *p_mutex2 = ABTI_mutex_get_ptr(mutex2);
519  *result = (p_mutex1 == p_mutex2) ? ABT_TRUE : ABT_FALSE;
520  return ABT_SUCCESS;
521 }
522 
549 {
551  ABTI_UB_ASSERT(attr);
552 
553  ABTI_mutex *p_mutex = ABTI_mutex_get_ptr(mutex);
554  ABTI_CHECK_NULL_MUTEX_PTR(p_mutex);
555 
556  ABTI_mutex_attr *p_newattr;
557  int abt_errno = ABTU_malloc(sizeof(ABTI_mutex_attr), (void **)&p_newattr);
558  ABTI_CHECK_ERROR(abt_errno);
559 
560  /* Copy values. Nesting count must be initialized. */
561  p_newattr->attrs = p_mutex->attrs;
562 
563  /* Return value */
564  *attr = ABTI_mutex_attr_get_handle(p_newattr);
565  return ABT_SUCCESS;
566 }
ABT_mutex_lock_low
int ABT_mutex_lock_low(ABT_mutex mutex)
Lock a mutex with low priority.
Definition: mutex.c:223
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:1043
ABTI_mutex_is_locked
static ABT_bool ABTI_mutex_is_locked(ABTI_mutex *p_mutex)
Definition: abti_mutex.h:117
ABTI_mutex_lock
static void ABTI_mutex_lock(ABTI_local **pp_local, ABTI_mutex *p_mutex)
Definition: abti_mutex.h:99
ABTI_mutex_attr::attrs
int attrs
Definition: abti.h:198
ABTI_CHECK_ERROR
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:136
ABTI_MUTEX_ATTR_RECURSIVE
#define ABTI_MUTEX_ATTR_RECURSIVE
Definition: abti.h:125
ABT_mutex
struct ABT_mutex_opaque * ABT_mutex
Mutex handle type.
Definition: abt.h:987
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:469
ABT_mutex_lock
int ABT_mutex_lock(ABT_mutex mutex)
Lock a mutex.
Definition: mutex.c:189
ABTI_mutex::attrs
int attrs
Definition: abti.h:202
ABTI_mutex_unlock
static void ABTI_mutex_unlock(ABTI_local *p_local, ABTI_mutex *p_mutex)
Definition: abti_mutex.h:187
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:513
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:292
ABTI_CHECK_NULL_MUTEX_PTR
#define ABTI_CHECK_NULL_MUTEX_PTR(p)
Definition: abti_error.h:274
abti.h
ABT_mutex_attr
struct ABT_mutex_attr_opaque * ABT_mutex_attr
Mutex attribute handle type.
Definition: abt.h:994
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:257
ABTU_malloc
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:235
ABTI_initialized
ABT_bool ABTI_initialized(void)
Definition: global.c:187
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:35
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:784
ABTI_self_get_thread_id
static ABTI_thread_id ABTI_self_get_thread_id(ABTI_local *p_local)
Definition: abti_self.h:22
ABT_FALSE
#define ABT_FALSE
False constant for ABT_bool.
Definition: abt.h:786
ABTI_mutex_attr
Definition: abti.h:197
ABT_mutex_spinlock
int ABT_mutex_spinlock(ABT_mutex mutex)
Lock a mutex in a busy-wait form.
Definition: mutex.c:332
ABTI_UB_ASSERT
#define ABTI_UB_ASSERT(cond)
Definition: abti_error.h:19
ABTI_mutex_spinlock
static void ABTI_mutex_spinlock(ABTI_local *p_local, ABTI_mutex *p_mutex)
Definition: abti_mutex.h:155
ABTU_free
static void ABTU_free(void *ptr)
Definition: abtu.h:228
ABTI_mutex_trylock
static int ABTI_mutex_trylock(ABTI_local *p_local, ABTI_mutex *p_mutex)
Definition: abti_mutex.h:128
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:417
ABT_mutex_memory
A struct that can be converted to ABT_mutex.
Definition: abt.h:1133
ABTI_local
struct ABTI_local ABTI_local
Definition: abti.h:132
ABT_MUTEX_NULL
#define ABT_MUTEX_NULL
Definition: abt.h:1109
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:101
ABT_mutex_unlock
int ABT_mutex_unlock(ABT_mutex mutex)
Unlock a mutex.
Definition: mutex.c:365
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:548
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:151
ABTI_mutex::owner_id
ABTI_thread_id owner_id
Definition: abti.h:207
ABTI_mutex
Definition: abti.h:201