Changeset 508b0df1 in mainline for uspace/lib/c/include/refcount.h
- Timestamp:
- 2018-09-06T20:21:52Z (6 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 78de83de, fc10e1b
- Parents:
- 4621d23
- git-author:
- Jiří Zárevúcky <jiri.zarevucky@…> (2018-08-13 03:53:39)
- git-committer:
- Jiří Zárevúcky <jiri.zarevucky@…> (2018-09-06 20:21:52)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/include/refcount.h
r4621d23 r508b0df1 40 40 #define LIBC_REFCOUNT_H_ 41 41 42 // TODO: #include <stdatomic.h>43 44 42 #include <assert.h> 45 #include < atomic.h>43 #include <stdatomic.h> 46 44 #include <stdbool.h> 47 45 48 46 /* Wrapped in a structure to prevent direct manipulation. */ 49 47 typedef struct atomic_refcount { 50 //volatile atomic_int __cnt; 51 atomic_t __cnt; 48 volatile atomic_int __cnt; 52 49 } atomic_refcount_t; 53 50 54 51 static inline void refcount_init(atomic_refcount_t *rc) 55 52 { 56 //atomic_store_explicit(&rc->__cnt, 0, memory_order_relaxed); 57 atomic_set(&rc->__cnt, 0); 53 atomic_store_explicit(&rc->__cnt, 0, memory_order_relaxed); 58 54 } 59 55 … … 72 68 // still needs to be synchronized independently of the refcount. 73 69 74 //int old = atomic_fetch_add_explicit(&rc->__cnt, 1, 75 // memory_order_relaxed); 76 77 atomic_signed_t old = atomic_postinc(&rc->__cnt); 70 int old = atomic_fetch_add_explicit(&rc->__cnt, 1, 71 memory_order_relaxed); 78 72 79 73 /* old < 0 indicates that the function is used incorrectly. */ … … 94 88 // XXX: The decrementers don't need to synchronize with each other, 95 89 // but they do need to synchronize with the one doing deallocation. 96 //int old = atomic_fetch_sub_explicit(&rc->__cnt, 1, 97 // memory_order_release); 98 99 atomic_signed_t old = atomic_postdec(&rc->__cnt); 90 int old = atomic_fetch_sub_explicit(&rc->__cnt, 1, 91 memory_order_release); 100 92 101 93 assert(old >= 0); … … 104 96 // XXX: We are holding the last reference, so we must now 105 97 // synchronize with all the other decrementers. 106 //int val = atomic_load_explicit(&rc->__cnt, 107 // memory_order_acquire); 108 //assert(val == -1); 109 return true; 98 99 int val = atomic_load_explicit(&rc->__cnt, 100 memory_order_acquire); 101 assert(val == -1); 102 103 /* 104 * The compiler probably wouldn't optimize the memory barrier 105 * away, but better safe than sorry. 106 */ 107 return val < 0; 110 108 } 111 109
Note:
See TracChangeset
for help on using the changeset viewer.