Ticket #26: ctors.diff
File ctors.diff, 8.2 KB (added by , 15 years ago) |
---|
-
mutek/include/mutek/ctors.h
1 /* 2 This file is part of MutekH. 3 4 MutekH is free software; you can redistribute it and/or modify it 5 under the terms of the GNU General Public License as published by 6 the Free Software Foundation; either version 2 of the License, or 7 (at your option) any later version. 8 9 MutekH is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with MutekH; if not, write to the Free Software Foundation, 16 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 18 Copyright Alexandre Becoulet <alexandre.becoulet@lip6.fr> (c) 2010 19 */ 20 21 #ifndef _MUTEK_CTORS_H_ 22 #define _MUTEK_CTORS_H_ 23 24 #include <hexo/types.h> 25 #include <hexo/error.h> 26 27 #define __MUTEK_AT_START(n) error_t (n)(uint_fast8_t level) 28 typedef __MUTEK_AT_START(mutek_at_start_t); 29 30 #define __MUTEK_AT_END(n) void (n)() 31 typedef __MUTEK_AT_END(mutek_at_end_t); 32 33 #define MUTEK_AT_START(n) __attribute__((constructor,cold)) __MUTEK_AT_START(n) 34 #define MUTEK_AT_END(n) __attribute__((destructor,cold)) __MUTEK_AT_END(n) 35 36 enum mutek_start_level_e 37 { 38 MUTEK_AT_DRVINIT_LEVEL, 39 MUTEK_AT_MUTEK_START_LEVEL, 40 }; 41 42 void execute_at_start(uint_fast8_t level); 43 void execute_at_end(); 44 45 #endif 46 -
mutek/ctors.c
1 /* 2 This file is part of MutekH. 3 4 MutekH is free software; you can redistribute it and/or modify it 5 under the terms of the GNU General Public License as published by 6 the Free Software Foundation; either version 2 of the License, or 7 (at your option) any later version. 8 9 MutekH is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with MutekH; if not, write to the Free Software Foundation, 16 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 18 Copyright Alexandre Becoulet <alexandre.becoulet@lip6.fr> (c) 2010 19 */ 20 21 #include <mutek/ctors.h> 22 23 extern __ldscript_symbol_t ctors_begin, ctors_end; 24 extern __ldscript_symbol_t dtors_begin, dtors_end; 25 26 void execute_at_start(uint_fast8_t level) 27 { 28 mutek_at_start_t **ctors = (mutek_at_start_t**)&ctors_begin; 29 size_t i, count = (mutek_at_start_t**)&ctors_end - ctors; 30 31 for (i = 0; i < count; i++) 32 { 33 if (ctors[i] == NULL) 34 continue; 35 36 if (ctors[i](level)) 37 continue; 38 39 ctors[i] = NULL; 40 } 41 } 42 43 void execute_at_end() 44 { 45 mutek_at_end_t **dtors = (mutek_at_end_t**)&dtors_begin; 46 size_t i, count = (mutek_at_end_t**)&dtors_end - dtors; 47 48 for (i = 0; i < count; i++) 49 dtors[i](); 50 } 51 -
mutek/Makefile
3 3 mutek/printk.h mutek/rwlock.h mutek/scheduler.h mutek/timer.h \ 4 4 mutek/vmem_kalloc.h mutek/semaphore.h mutek/fdt.h mutek/console.h 5 5 6 objs = timer.o printf_arg.o console.o 6 objs = timer.o printf_arg.o console.o ctors.o 7 7 8 8 ifeq ($(CONFIG_MUTEK_MEM_REGION), defined) 9 9 objs += mem_region.o -
arch/emu/arch_init.c
116 116 sched_cpu_init(); 117 117 #endif 118 118 119 execute_at_start(MUTEK_AT_DRVINIT_LEVEL); 120 119 121 arch_hw_init(); 120 122 mem_region_init(); 121 123 … … 123 125 cpu_init_flag = 1; 124 126 #endif 125 127 128 execute_at_start(MUTEK_AT_MUTEK_START_LEVEL); 129 126 130 /* run mutek_start() */ 127 131 mutek_start(0, 0); 128 132 -
arch/emu/ldscript.cpp
17 17 *(.drivers) 18 18 global_driver_registry_end = .; 19 19 20 /* data depending on cpu architecture (fonction pointer variables, ...) */ 21 *(.cpuarchdata*) 20 // constructors and destructors 21 . = ALIGN(4); 22 ctors_begin = .; 23 KEEP(*(.ctors)) 24 ctors_end = .; 25 dtors_begin = .; 26 KEEP(*(.dtors)) 27 dtors_end = .; 22 28 } 23 29 __data_start = LOADADDR(.data); 24 30 __data_end = LOADADDR(.data) + SIZEOF(.data); -
arch/ibmpc/arch_init.c
35 35 #include <mutek/mem_alloc.h> 36 36 #include <mutek/scheduler.h> 37 37 #include <mutek/printk.h> 38 #include <mutek/ctors.h> 38 39 39 40 #ifdef CONFIG_DRIVER_ICU_APIC 40 41 # include <drivers/icu/apic/icu-apic.h> … … 159 160 sched_cpu_init(); 160 161 #endif 161 162 163 execute_at_start(MUTEK_AT_DRVINIT_LEVEL); 164 162 165 #if defined(CONFIG_ARCH_HW_INIT_USER) 163 166 user_hw_init(); 164 167 #elif defined(CONFIG_ARCH_HW_INIT) … … 167 170 # error No supported hardware initialization 168 171 #endif 169 172 173 execute_at_start(MUTEK_AT_MUTEK_START_LEVEL); 170 174 171 175 /* run mutek_start() */ 172 176 mutek_start(0, 0); -
arch/ibmpc/ldscript.cpp
25 25 .sdata : { *(.sdata*) } > mem_ram 26 26 .sbss : { *(.sbss*) } > mem_ram 27 27 .bss : { *(.bss*) } > mem_ram 28 .data : { *(.data*) } > mem_ram 28 .data : { *(.data*) 29 // constructors and destructors 30 . = ALIGN(4); 31 ctors_begin = .; 32 KEEP(*(.ctors)) 33 ctors_end = .; 34 dtors_begin = .; 35 KEEP(*(.dtors)) 36 dtors_end = .; 37 } > mem_ram 29 38 .cpuarchdata : { *(.cpuarchdata*) } > mem_ram 30 39 31 40 __system_heap_start = ADDR(.cpuarchdata) + SIZEOF(.cpuarchdata); -
arch/soclib/arch_init.c
43 43 #include <hexo/cpu.h> 44 44 #include <mutek/printk.h> 45 45 #include <mutek/scheduler.h> 46 #include <mutek/ctors.h> 46 47 47 48 #include <string.h> 48 49 … … 141 142 mmu_cpu_init(); 142 143 #endif 143 144 145 execute_at_start(MUTEK_AT_DRVINIT_LEVEL); 146 144 147 #if defined(CONFIG_ARCH_DEVICE_TREE) 145 148 device_init(&fdt_enum_dev); 146 149 enum_fdt_init(&fdt_enum_dev, device_tree); … … 174 177 sched_cpu_init(); 175 178 #endif 176 179 180 execute_at_start(MUTEK_AT_MUTEK_START_LEVEL); 181 177 182 /* run mutek_start() */ 178 183 mutek_start(0, 0); 179 184 #ifdef CONFIG_ARCH_SMP -
arch/soclib/ldscript.cpp
120 120 __data_start = ABSOLUTE(.); 121 121 *(.sdata*) 122 122 *(.data*) 123 *(.cpuarchdata*) 123 124 // constructors and destructors 125 . = ALIGN(4); 126 ctors_begin = .; 127 KEEP(*(.ctors)) 128 ctors_end = .; 129 dtors_begin = .; 130 KEEP(*(.dtors)) 131 dtors_end = .; 124 132 } > mem_ram __AT_MEM_ROM 125 133 126 134 __data_load_start = LOADADDR(.data); -
examples/hello/hello.c
1 1 2 2 #include <pthread.h> 3 3 #include <mutek/printk.h> 4 #include <mutek/ctors.h> 4 5 5 6 pthread_mutex_t m; 6 7 pthread_t a, b; … … 18 19 19 20 void app_start() 20 21 { 22 return; 21 23 pthread_mutex_init(&m, NULL); 22 24 pthread_create(&a, NULL, f, "Hello world\n"); 23 25 pthread_create(&b, NULL, f, "Hello world\n"); 24 26 } 25 27 28 static MUTEK_AT_START(myinit) 29 { 30 printk("init level %i\n", level); 31 return -EAGAIN; 32 } 33