-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from badgeteam/mutex
Simple spinlock mutexes.
- Loading branch information
Showing
4 changed files
with
239 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
// SPDX-License-Identifier: MIT | ||
|
||
#pragma once | ||
|
||
#include "badge_err.h" | ||
#include "time.h" | ||
|
||
#include <stdatomic.h> | ||
|
||
// Magic value for the magic field. | ||
#define MUTEX_MAGIC (int)0xcafebabe | ||
|
||
typedef struct { | ||
// Magic value. | ||
atomic_int magic; | ||
// Mutex allows sharing. | ||
bool is_shared; | ||
// Share count and/or is locked. | ||
atomic_int shares; | ||
} mutex_t; | ||
|
||
#define MUTEX_T_INIT ((mutex_t){MUTEX_MAGIC, 0, 0}) | ||
#define MUTEX_T_INIT_SHARED ((mutex_t){MUTEX_MAGIC, 1, 0}) | ||
|
||
// Initialise a mutex for unshared use. | ||
void mutex_init(badge_err_t *ec, mutex_t *mutex); | ||
// Initialise a mutex for shared use. | ||
void mutex_init_shared(badge_err_t *ec, mutex_t *mutex); | ||
// Clean up the mutex. | ||
void mutex_destroy(badge_err_t *ec, mutex_t *mutex); | ||
// Try to acquire `mutex` within `max_wait_us` microseconds. | ||
// Returns true if the mutex was successully acquired. | ||
bool mutex_acquire(badge_err_t *ec, mutex_t *mutex, timestamp_us_t max_wait_us); | ||
// Release `mutex`, if it was initially acquired by this thread. | ||
// Returns true if the mutex was successfully released. | ||
bool mutex_release(badge_err_t *ec, mutex_t *mutex); | ||
// Try to acquire a share in `mutex` within `max_wait_us` microseconds. | ||
// Returns true if the share was successfully acquired. | ||
bool mutex_acquire_shared(badge_err_t *ec, mutex_t *mutex, timestamp_us_t max_wait_us); | ||
// Release `mutex`, if it was initially acquired by this thread. | ||
// Returns true if the mutex was successfully released. | ||
bool mutex_release_shared(badge_err_t *ec, mutex_t *mutex); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.