Changeset 45c8eea in mainline for uspace/lib/c/generic/private/futex.h
- Timestamp:
- 2018-11-11T15:47:39Z (5 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f4cb6c5f
- Parents:
- 269bc459
- git-author:
- Jakub Jermar <jakub@…> (2018-11-10 17:49:44)
- git-committer:
- Jakub Jermar <jakub@…> (2018-11-11 15:47:39)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/private/futex.h
r269bc459 r45c8eea 46 46 typedef struct futex { 47 47 volatile atomic_int val; 48 volatile atomic_int lock;49 48 volatile cap_waitq_handle_t whandle; 50 49 … … 54 53 } futex_t; 55 54 56 extern voidfutex_initialize(futex_t *futex, int value);55 extern errno_t futex_initialize(futex_t *futex, int value); 57 56 58 57 static inline errno_t futex_destroy(futex_t *futex) … … 64 63 65 64 #ifdef CONFIG_DEBUG_FUTEX 66 67 #define FUTEX_INITIALIZE(val) { (val), 0, CAP_NIL, NULL }68 #define FUTEX_INITIALIZER FUTEX_INITIALIZE(1)69 65 70 66 void __futex_assert_is_locked(futex_t *, const char *); … … 85 81 #else 86 82 87 #define FUTEX_INITIALIZE(val) { (val), 0, CAP_NIL }88 #define FUTEX_INITIALIZER FUTEX_INITIALIZE(1)89 90 83 #define futex_lock(fut) (void) futex_down((fut)) 91 84 #define futex_trylock(fut) futex_trydown((fut)) … … 98 91 #endif 99 92 100 static errno_t allocate_waitq(futex_t *futex) 101 { 102 int expected = 0; 103 while (!atomic_compare_exchange_weak_explicit(&futex->lock, &expected, 104 1, memory_order_acquire, memory_order_relaxed)) 105 expected = 0; 106 107 if (futex->whandle == CAP_NIL) { 108 errno_t rc = __SYSCALL1(SYS_WAITQ_CREATE, 109 (sysarg_t) &futex->whandle); 110 if (rc != EOK) { 111 atomic_store_explicit(&futex->lock, 0, 112 memory_order_release); 113 return rc; 114 } 115 } 116 117 atomic_store_explicit(&futex->lock, 0, memory_order_release); 118 return EOK; 93 static inline errno_t futex_allocate_waitq(futex_t *futex) 94 { 95 return __SYSCALL1(SYS_WAITQ_CREATE, (sysarg_t) &futex->whandle); 119 96 } 120 97 … … 140 117 // TODO: Add tests for this. 141 118 142 // Preallocate the waitq handle so that we don't need to risk a failure 143 // during wakeup 144 if (futex->whandle == CAP_NIL) { 145 errno_t rc = allocate_waitq(futex); 146 if (rc != EOK) 147 return rc; 148 } 119 assert(futex->whandle != CAP_NIL); 149 120 150 121 if (atomic_fetch_sub_explicit(&futex->val, 1, memory_order_acquire) > 0)
Note:
See TracChangeset
for help on using the changeset viewer.