Changeset 9bde0d5 in mainline for uspace/lib/c/generic/malloc.c
- Timestamp:
- 2018-07-18T19:56:43Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 42f5860
- Parents:
- 40abf56
- git-author:
- Jiří Zárevúcky <jiri.zarevucky@…> (2018-07-18 19:47:28)
- git-committer:
- Jiří Zárevúcky <jiri.zarevucky@…> (2018-07-18 19:56:43)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/malloc.c
r40abf56 r9bde0d5 44 44 #include <bitops.h> 45 45 #include <mem.h> 46 #include <f utex.h>46 #include <fibril_synch.h> 47 47 #include <stdlib.h> 48 48 #include <adt/gcdlcm.h> … … 194 194 195 195 /** Futex for thread-safe heap manipulation */ 196 static futex_t malloc_futex = FUTEX_INITIALIZER;196 static FIBRIL_RMUTEX_INITIALIZE(malloc_mutex); 197 197 198 198 #define malloc_assert(expr) safe_assert(expr) 199 200 #ifdef FUTEX_UPGRADABLE201 /** True if the heap may be accessed from multiple threads. */202 static bool multithreaded = false;203 204 /** Makes accesses to the heap thread safe. */205 void malloc_enable_multithreaded(void)206 {207 multithreaded = true;208 }209 199 210 200 /** Serializes access to the heap from multiple threads. */ 211 201 static inline void heap_lock(void) 212 202 { 213 if (multithreaded) { 214 futex_down(&malloc_futex); 215 } else { 216 /* 217 * Malloc never switches fibrils while the heap is locked. 218 * Similarly, it never creates new threads from within the 219 * locked region. Therefore, if there are no other threads 220 * except this one, the whole operation will complete without 221 * any interruptions. 222 */ 223 } 203 fibril_rmutex_lock(&malloc_mutex); 224 204 } 225 205 … … 227 207 static inline void heap_unlock(void) 228 208 { 229 if (multithreaded) { 230 futex_up(&malloc_futex); 231 } else { 232 /* 233 * Malloc never switches fibrils while the heap is locked. 234 * Similarly, it never creates new threads from within the 235 * locked region. Therefore, if there are no other threads 236 * except this one, the whole operation will complete without 237 * any interruptions. 238 */ 239 } 240 } 241 242 #else 243 244 /** Makes accesses to the heap thread safe. */ 245 void malloc_enable_multithreaded(void) 246 { 247 /* No-op. Already using thread-safe heap locking operations. */ 248 } 249 250 /** Serializes access to the heap from multiple threads. */ 251 static inline void heap_lock(void) 252 { 253 futex_down(&malloc_futex); 254 } 255 256 /** Serializes access to the heap from multiple threads. */ 257 static inline void heap_unlock(void) 258 { 259 futex_up(&malloc_futex); 260 } 261 #endif 262 209 fibril_rmutex_unlock(&malloc_mutex); 210 } 263 211 264 212 /** Initialize a heap block
Note:
See TracChangeset
for help on using the changeset viewer.