Changeset d54b303 in mainline
- Timestamp:
- 2012-12-04T05:18:19Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 32d2e60
- Parents:
- 9a3b469
- Location:
- uspace/lib/c
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/Makefile
r9a3b469 rd54b303 52 52 SLIBRARY = libc.so.0.0 53 53 LSONAME = libc.so0 54 55 LIBS = $(LIBURCU_PREFIX)/liburcu.a 56 EXTRA_CFLAGS += -I$(LIBURCU_PREFIX) 54 57 55 58 -
uspace/lib/c/generic/fibril.c
r9a3b469 rd54b303 51 51 #include <futex.h> 52 52 53 #ifdef FUTEX_UPGRADABLE 54 #include <rcu.h> 55 #endif 56 53 57 /** 54 58 * This futex serializes access to ready_list, … … 83 87 { 84 88 fibril_t *fibril = __tcb_get()->fibril_data; 89 90 #ifdef FUTEX_UPGRADABLE 91 rcu_register_fibril(); 92 #endif 85 93 86 94 /* Call the implementing function. */ … … 245 253 246 254 futex_up(&fibril_futex); 255 256 #ifdef FUTEX_UPGRADABLE 257 if (stype == FIBRIL_FROM_DEAD) { 258 rcu_deregister_fibril(); 259 } 260 #endif 261 247 262 context_restore(&dstf->ctx); 248 263 /* not reached */ -
uspace/lib/c/generic/futex.c
r9a3b469 rd54b303 48 48 49 49 50 #ifdef FUTEX_UPGRADABLE 51 52 int _upgrade_futex = 0; 53 static futex_t upg_and_wait_futex = FUTEX_INITIALIZER; 54 55 void futex_upgrade_all_and_wait(void) 56 { 57 _futex_down(&upg_and_wait_futex); 58 59 if (!_upgrade_futex) { 60 rcu_assign(_upgrade_futex, 1); 61 rcu_synchronize(); 62 } 63 64 _futex_up(&upg_and_wait_futex); 65 } 66 67 #endif 68 50 69 /** @} 51 70 */ -
uspace/lib/c/generic/libc.c
r9a3b469 rd54b303 51 51 #include "private/malloc.h" 52 52 #include "private/io.h" 53 #include "private/thread.h" 53 54 #ifdef FUTEX_UPGRADABLE 55 #include <rcu.h> 56 #endif 54 57 55 58 #ifdef CONFIG_RTLD 56 59 #include <rtld/rtld.h> 57 60 #endif 61 58 62 59 63 static bool env_setup = false; … … 63 67 /* Initialize user task run-time environment */ 64 68 __malloc_init(); 65 __async_init();66 69 67 70 fibril_t *fibril = fibril_setup(); … … 74 77 __pcb = (pcb_t *) pcb_ptr; 75 78 76 atomic_inc(&_created_thread_cnt); 79 #ifdef FUTEX_UPGRADABLE 80 rcu_register_fibril(); 81 #endif 82 83 __async_init(); 77 84 78 85 /* The basic run-time environment is setup */ -
uspace/lib/c/generic/malloc.c
r9a3b469 rd54b303 47 47 #include <adt/gcdlcm.h> 48 48 #include "private/malloc.h" 49 #include "private/thread.h"50 49 51 50 /** Magic used in heap headers. */ … … 201 200 do { \ 202 201 if (!(expr)) {\ 203 futex_up(&malloc_futex); \202 _futex_up(&malloc_futex); \ 204 203 assert_abort(#expr, __FILE__, __LINE__); \ 205 204 } \ … … 786 785 void *malloc(const size_t size) 787 786 { 788 /* Do not use futexes for allocations during main thread initialization. */ 789 if (0 == atomic_get(&_created_thread_cnt)) { 790 return malloc_internal(size, BASE_ALIGN); 791 } else { 792 futex_down(&malloc_futex); 793 void *block = malloc_internal(size, BASE_ALIGN); 794 futex_up(&malloc_futex); 795 return block; 796 } 787 _futex_down(&malloc_futex); 788 void *block = malloc_internal(size, BASE_ALIGN); 789 _futex_up(&malloc_futex); 790 791 return block; 797 792 } 798 793 … … 813 808 1 << (fnzb(max(sizeof(void *), align) - 1) + 1); 814 809 815 /* Do not use futexes for allocations during main thread initialization. */ 816 if (0 == atomic_get(&_created_thread_cnt)) { 817 return malloc_internal(size, palign); 818 } else { 819 futex_down(&malloc_futex); 820 void *block = malloc_internal(size, palign); 821 futex_up(&malloc_futex); 822 823 return block; 824 } 810 _futex_down(&malloc_futex); 811 void *block = malloc_internal(size, palign); 812 _futex_up(&malloc_futex); 813 814 return block; 825 815 } 826 816 … … 838 828 return malloc(size); 839 829 840 futex_down(&malloc_futex);830 _futex_down(&malloc_futex); 841 831 842 832 /* Calculate the position of the header. */ … … 895 885 } 896 886 897 futex_up(&malloc_futex);887 _futex_up(&malloc_futex); 898 888 899 889 if (reloc) { … … 918 908 return; 919 909 920 futex_down(&malloc_futex);910 _futex_down(&malloc_futex); 921 911 922 912 /* Calculate the position of the header. */ … … 963 953 heap_shrink(area); 964 954 965 futex_up(&malloc_futex);955 _futex_up(&malloc_futex); 966 956 } 967 957 968 958 void *heap_check(void) 969 959 { 970 futex_down(&malloc_futex);960 _futex_down(&malloc_futex); 971 961 972 962 if (first_heap_area == NULL) { 973 futex_up(&malloc_futex);963 _futex_up(&malloc_futex); 974 964 return (void *) -1; 975 965 } … … 985 975 (((uintptr_t) area->start % PAGE_SIZE) != 0) || 986 976 (((uintptr_t) area->end % PAGE_SIZE) != 0)) { 987 futex_up(&malloc_futex);977 _futex_up(&malloc_futex); 988 978 return (void *) area; 989 979 } … … 996 986 /* Check heap block consistency */ 997 987 if (head->magic != HEAP_BLOCK_HEAD_MAGIC) { 998 futex_up(&malloc_futex);988 _futex_up(&malloc_futex); 999 989 return (void *) head; 1000 990 } … … 1004 994 if ((foot->magic != HEAP_BLOCK_FOOT_MAGIC) || 1005 995 (head->size != foot->size)) { 1006 futex_up(&malloc_futex);996 _futex_up(&malloc_futex); 1007 997 return (void *) foot; 1008 998 } … … 1010 1000 } 1011 1001 1012 futex_up(&malloc_futex);1002 _futex_up(&malloc_futex); 1013 1003 1014 1004 return NULL; -
uspace/lib/c/generic/private/thread.h
r9a3b469 rd54b303 41 41 extern void __thread_main(uspace_arg_t *); 42 42 43 struct atomic;44 extern struct atomic _created_thread_cnt;45 46 43 #endif 47 44 -
uspace/lib/c/generic/thread.c
r9a3b469 rd54b303 46 46 #include "private/thread.h" 47 47 48 /** 49 * The number of threads that have been created and initialized since 50 * the start of the program. 51 */ 52 atomic_t _created_thread_cnt = {0}; 48 #ifdef FUTEX_UPGRADABLE 49 #include <rcu.h> 50 #endif 51 53 52 54 53 /** Main thread function. … … 69 68 __tcb_set(fibril->tcb); 70 69 71 atomic_inc(&_created_thread_cnt); 70 #ifdef FUTEX_UPGRADABLE 71 rcu_register_fibril(); 72 futex_upgrade_all_and_wait(); 73 #endif 72 74 73 75 uarg->uspace_thread_function(uarg->uspace_thread_arg); … … 81 83 /* If there is a manager, destroy it */ 82 84 async_destroy_manager(); 85 86 #ifdef FUTEX_UPGRADABLE 87 rcu_deregister_fibril(); 88 #endif 89 83 90 fibril_teardown(fibril); 84 91 -
uspace/lib/c/include/futex.h
r9a3b469 rd54b303 40 40 #include <libc.h> 41 41 42 #define FUTEX_INITIALIZE(val) {{(val)}}43 #define FUTEX_INITIALIZER FUTEX_INITIALIZE(1)44 45 42 typedef struct futex { 46 43 atomic_t val; 44 #ifdef FUTEX_UPGRADABLE 45 int upgraded; 46 #endif 47 47 } futex_t; 48 48 … … 50 50 extern void futex_initialize(futex_t *futex, int value); 51 51 52 #ifdef FUTEX_UPGRADABLE 53 #include <rcu.h> 54 55 #define FUTEX_INITIALIZE(val) {{(val)}, {0}} 56 57 #define futex_down(fut) \ 58 ({ \ 59 rcu_read_lock(); \ 60 (fut)->upgraded = rcu_access(_upgrade_futexes); \ 61 if ((fut)->upgraded) \ 62 (void) _futex_down((fut)); \ 63 }) 64 65 #define futex_trydown(fut) \ 66 ({ \ 67 rcu_read_lock(); \ 68 int _upgraded = rcu_access(_upgrade_futexes); \ 69 if (_upgraded) { \ 70 int _acquired = _futex_trydown((fut)); \ 71 if (!_acquired) { \ 72 rcu_read_unlock(); \ 73 } else { \ 74 (fut)->upgraded = true; \ 75 } \ 76 _acquired; \ 77 } else { \ 78 (fut)->upgraded = false; \ 79 1; \ 80 } \ 81 }) 82 83 #define futex_up(fut) \ 84 ({ \ 85 if ((fut)->upgraded) \ 86 (void) _futex_up((fut)); \ 87 rcu_read_unlock(); \ 88 }) 89 90 extern int _upgrade_futexes; 91 92 extern void futex_upgrade_all_and_wait(void); 93 94 #else 95 96 #define FUTEX_INITIALIZE(val) {{(val)}} 97 52 98 #define futex_down(fut) (void)_futex_down((fut)) 53 99 #define futex_trydown(fut) _futex_trydown((fut)) 54 100 #define futex_up(fut) (void)_futex_up((fut)) 101 102 #endif 55 103 104 #define FUTEX_INITIALIZER FUTEX_INITIALIZE(1) 56 105 57 106 /** Try to down the futex.
Note:
See TracChangeset
for help on using the changeset viewer.