├── .gitattributes ├── COPYING ├── README.md ├── examples ├── makefile └── pr │ ├── main.c │ └── makefile ├── include ├── pr.h ├── pr_msg.h └── prs │ ├── alloc │ └── stack.h │ ├── assert.h │ ├── clock.h │ ├── config.h │ ├── dllist.h │ ├── error.h │ ├── event.h │ ├── excp.h │ ├── god.h │ ├── gpd.h │ ├── idllist.h │ ├── init.h │ ├── log.h │ ├── mpmcq.h │ ├── mpmcring.h │ ├── mpsciq.h │ ├── mpscq.h │ ├── msg.h │ ├── msgq.h │ ├── name.h │ ├── object.h │ ├── pal │ ├── arch.h │ ├── assert.h │ ├── atomic.h │ ├── bitops.h │ ├── compiler.h │ ├── config.h │ ├── context.h │ ├── crt.h │ ├── cycles.h │ ├── excp.h │ ├── exit.h │ ├── inline.h │ ├── lib.h │ ├── malloc.h │ ├── mem.h │ ├── os.h │ ├── pit.h │ ├── proc.h │ ├── thread.h │ ├── tls.h │ ├── types.h │ └── wls.h │ ├── pd.h │ ├── pool.h │ ├── proc.h │ ├── result.h │ ├── rtc.h │ ├── sched.h │ ├── sched │ ├── swcoop.h │ └── swprio.h │ ├── sem.h │ ├── spinlock.h │ ├── str.h │ ├── svc │ ├── log.h │ ├── proc.h │ └── proc.msg │ ├── systeminfo.h │ ├── task.h │ ├── ticks.h │ ├── timer.h │ ├── types.h │ ├── version.h │ └── worker.h ├── make.bat ├── make ├── makefile.debug ├── makefile.gcc ├── makefile.linux ├── makefile.prapp ├── makefile.release ├── makefile.rules ├── makefile.test ├── makefile.top └── makefile.windows ├── makefile └── prs ├── alloc └── stack.c ├── assert.c ├── clock.c ├── crt └── crt0.c ├── error.c ├── event.c ├── excp.c ├── god.c ├── gpd.c ├── init.c ├── lib ├── ds │ ├── dllist.c │ ├── idllist.c │ ├── mpmcq.c │ ├── mpmcring.c │ ├── mpsciq.c │ ├── mpscq.c │ ├── pd.c │ └── pool.c └── str.c ├── log.c ├── main.c ├── makefile ├── msgq.c ├── name.c ├── pal ├── linux │ ├── os.c │ └── proc.c ├── posix │ ├── assert.c │ ├── context.c │ ├── excp.c │ ├── mem.c │ ├── pit.c │ ├── signal.c │ ├── signal.h │ └── thread.c ├── stdc │ ├── exit.c │ ├── malloc.c │ └── wls.c └── windows │ ├── assert.c │ ├── context.c │ ├── excp.c │ ├── mem.c │ ├── os.c │ ├── pit.c │ ├── proc.c │ ├── thread.c │ └── wls.c ├── pr.c ├── proc.c ├── proc.h ├── rtc.c ├── sched.c ├── sched ├── swcoop.c └── swprio.c ├── sem.c ├── spinlock.c ├── svc ├── log.c └── proc.c ├── systeminfo.c ├── task.c ├── task.h ├── timer.c └── worker.c /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PRS 2 | === 3 | 4 | The Portable Runtime System (PRS) is a software library designed to ease the 5 | process of porting a RTOS application to a general purpose platform. 6 | 7 | Features 8 | ------------ 9 | 10 | * customizable user space preemptive scheduling; 11 | * higher single-core context switch performance than usual operating system 12 | facilities; 13 | * dynamic process loading in a common, flat address space; 14 | * multi-core support through lock-free primitives; 15 | * Windows support; 16 | * Linux support. 17 | 18 | Introduction 19 | ------------ 20 | 21 | By default, PRS initializes as many single-threaded priority schedulers as 22 | there are CPU cores on the system. Each scheduler manages its own list of tasks 23 | to run. New tasks can be created and added to schedulers at run-time. Tasks may 24 | also be removed and destroyed. Each task has its own register context and 25 | stack; tasks in PRS are *light-weight threads*, also known as *fibers*. Fibers 26 | provide faster context switching than regular threads because there is no need 27 | for the operating system to intervene. 28 | 29 | Schedulers work on top of an abstraction which is called a *worker*. A worker 30 | simulates an interruptible bare-metal-like environment where interrupts can 31 | occur any time. It handles context switching between scheduler tasks and also 32 | manages the flow of execution between an interruptible (preemptible) mode and 33 | a non-interruptible mode. In interruptible mode, tasks can be preempted at any 34 | time, just like regular threads. 35 | 36 | Lock-free PRS data structures ensure that even when some workers are preempted 37 | by the OS, other workers can still run freely. 38 | 39 | PRS also features a process loader. The process loader loads executables like 40 | a regular OS, except that the virtual address space is shared with PRS and 41 | other executables. The entry point of the executables is executed through a 42 | PRS task. PRS executables are linked with the PR API which features basic 43 | services such as message queues, semaphores and task management. 44 | 45 | PRS performs OS agnostic operations through its platform abstraction layer 46 | (PAL). The PRS PAL currently features operations for managing threads, context 47 | switching, virtual memory, exceptions, timers and more. 48 | 49 | Tested platforms 50 | ---------------- 51 | 52 | * Windows 7 SP1 with MinGW64, GCC 4.9.3 and 5.3.0 53 | * RHEL 7.2 with GCC 4.8.5 (use `C99=1`) 54 | * Debian 8.3 with GCC 4.9.2 55 | 56 | PRS only supports AMD64 architectures for now. 57 | 58 | Installation 59 | ------------ 60 | 61 | On Windows, first install MinGW-w64. If MinGW binaries are not acessible 62 | through the `PATH` environment variable, you may define the `MINGW_HOME` to 63 | point to them. For example, `set MINGW_HOME=C:\MinGW`. Then, run `make` in the 64 | root PRS directory. 65 | 66 | On Linux, make sure the `gcc` and `g++` tools are installed, then run `make`. 67 | 68 | Test code can be run using the `test` make target. 69 | 70 | If C11 is not supported by your compiler, use the `C99=1` assignation on the 71 | `make` command line. PRS can make use of C11 features such as the `stdatomic.h` 72 | header and the `_Thread_local` specifier. 73 | 74 | To compile in a debug configuration, use the `DEBUG=1` assignation. 75 | 76 | Getting started 77 | --------------- 78 | 79 | The `prs.log` file is generated upon execution of PRS in the current working 80 | directory. 81 | 82 | Examples in the `examples` directory show how to build dynamically loaded PRS 83 | executables. 84 | 85 | The `include/pr.h` file contains the APIs that are used by PRS executables. 86 | 87 | The `prs/init.c` file contains the PRS initialization and exit sequences. 88 | 89 | License 90 | ------- 91 | 92 | This version of PRS is released under the [GNU Affero General Public License] 93 | (https://www.gnu.org/licenses/agpl-3.0.en.html). 94 | 95 | For inquiries about a commercial license, please contact 96 | [portableruntimesystem@gmail.com](mailto:portableruntimesystem@gmail.com) 97 | -------------------------------------------------------------------------------- /examples/makefile: -------------------------------------------------------------------------------- 1 | # Portable Runtime System (PRS) 2 | # Copyright (C) 2016 Alexandre Tremblay 3 | # 4 | # This file is part of PRS. 5 | # 6 | # PRS is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Affero General Public License as 8 | # published by the Free Software Foundation, either version 3 of the 9 | # License, or (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Affero General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Affero General Public License 17 | # along with this program. If not, see . 18 | # 19 | # portableruntimesystem@gmail.com 20 | 21 | # Include the top makefile which will define the build characteristics 22 | MAKEFILE.TOP := $(CURDIR)/../make/makefile.top 23 | include $(MAKEFILE.TOP) 24 | 25 | TARGETS := $(sort $(basename $(wildcard */.))) 26 | 27 | # Define targets for 'all' 28 | ALL_TARGETS := $(addsuffix .all,$(TARGETS)) 29 | 30 | # Define targets for 'clean' 31 | CLEAN_TARGETS := $(addsuffix .clean,$(TARGETS)) 32 | 33 | # Define targets for 'tests' 34 | TEST_TARGETS := $(addsuffix .test,$(TARGETS)) 35 | 36 | # Define phony targets that are not file-related 37 | .PHONY: $(ALL_TARGETS) $(CLEAN_TARGETS) 38 | 39 | # Redirect targets to sub-directories 40 | all: $(ALL_TARGETS) 41 | clean: $(CLEAN_TARGETS) 42 | test: $(TEST_TARGETS) 43 | 44 | $(ALL_TARGETS): 45 | $(V)$(MAKE) -C $(basename $@) all 46 | 47 | $(CLEAN_TARGETS): 48 | $(V)$(MAKE) -C $(basename $@) clean 49 | 50 | $(TEST_TARGETS): 51 | $(V)$(MAKE) -C $(basename $@) test -------------------------------------------------------------------------------- /examples/pr/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | #define PARENT_TASK_NAME "init2" 29 | 30 | union pr_msg { 31 | pr_msg_id_t id; 32 | }; 33 | 34 | void task_entry(void* userdata) 35 | { 36 | const pr_task_id_t parent_id = pr_task_find(PARENT_TASK_NAME); 37 | PR_FATAL_WHEN(!parent_id); 38 | 39 | union pr_msg* msg = pr_msg_alloc(0, sizeof(*msg)); 40 | pr_log("process %u sending message to parent %u", pr_task_get_current(), parent_id); 41 | pr_msg_send(parent_id, msg); 42 | } 43 | 44 | static int main_recursive(int argc, char* argv[]) 45 | { 46 | struct pr_task_create_params params = { 47 | .userdata = 0, 48 | .stack_size = 16384, 49 | .prio = 10, 50 | .entry = task_entry, 51 | .sched_id = pr_sched_get_current() 52 | }; 53 | strcpy(params.name, argv[1]); 54 | const pr_task_id_t task_id = pr_task_create(¶ms); 55 | PR_FATAL_WHEN(!task_id); 56 | 57 | pr_log("%s: created process %u", argv[1], task_id); 58 | 59 | /* Prevent this process from exiting immediately */ 60 | pr_sleep_ms(10000); 61 | 62 | return 0; 63 | } 64 | 65 | static void spawn_proc(const char* path, const char* taskname) 66 | { 67 | const pr_task_id_t proc_task_id = pr_task_find(PRS_PROC_SVC_NAME); 68 | PR_FATAL_WHEN(!proc_task_id); 69 | 70 | struct prs_task_create_params child_task_params = { 71 | .userdata = 0, 72 | .stack_size = 16384, 73 | .prio = 20 74 | }; 75 | 76 | const pr_int_flag_t flag = pr_int_disable(); 77 | strcpy(child_task_params.name, taskname); 78 | char cmdline[PRS_MAX_PATH]; 79 | sprintf(cmdline, "%s %s", path, taskname); 80 | if (flag) { 81 | pr_int_enable(); 82 | } 83 | 84 | const pr_proc_id_t proc_id = prs_proc_send_exec(proc_task_id, path, cmdline, &child_task_params, pr_sched_get_current()); 85 | PR_FATAL_WHEN(!proc_id); 86 | } 87 | 88 | int pr_main(int argc, char* argv[]) 89 | { 90 | if (argc > 1) { 91 | return main_recursive(argc, argv); 92 | } 93 | 94 | for (int i = 0; i < 4; ++i) { 95 | char taskname[128]; 96 | const pr_int_flag_t flag = pr_int_disable(); 97 | sprintf(taskname, "child%d", i); 98 | if (flag) { 99 | pr_int_enable(); 100 | } 101 | spawn_proc(argv[0], taskname); 102 | } 103 | 104 | for (;;) { 105 | union pr_msg* msg = pr_msg_recv_timeout(100); 106 | if (!msg) { 107 | break; 108 | } 109 | pr_log("main: received message from task %u", pr_msg_get_sender(msg)); 110 | pr_msg_free(msg); 111 | } 112 | 113 | pr_log("main: exiting in 1 second"); 114 | 115 | pr_sleep_ms(1000); 116 | 117 | pr_log("main: exiting now"); 118 | 119 | pr_system_exit(0); 120 | 121 | return 0; 122 | } 123 | -------------------------------------------------------------------------------- /examples/pr/makefile: -------------------------------------------------------------------------------- 1 | # Portable Runtime System (PRS) 2 | # Copyright (C) 2016 Alexandre Tremblay 3 | # 4 | # This file is part of PRS. 5 | # 6 | # PRS is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Affero General Public License as 8 | # published by the Free Software Foundation, either version 3 of the 9 | # License, or (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Affero General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Affero General Public License 17 | # along with this program. If not, see . 18 | # 19 | # portableruntimesystem@gmail.com 20 | 21 | # Include the top makefile which will define the build characteristics 22 | MAKEFILE.TOP := $(CURDIR)/../../make/makefile.top 23 | include $(MAKEFILE.TOP) 24 | 25 | # Define the target information 26 | TARGET = my_pr_example 27 | include $(MAKEDIR)/makefile.prapp 28 | 29 | # Define the generic source files to build 30 | SOURCES += main.c 31 | 32 | # Define the include paths 33 | INCLUDEDIRS = \ 34 | ../../include 35 | 36 | # Define additional compiler flags 37 | CFLAGS += 38 | 39 | # Include rules 40 | include $(MAKEDIR)/makefile.rules 41 | 42 | # Include test rules 43 | include $(MAKEDIR)/makefile.test 44 | -------------------------------------------------------------------------------- /include/pr_msg.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PR API message ID declarations. 27 | * 28 | * The following declarations allow PRS services to each use unique message IDs for their communication with tasks. 29 | */ 30 | 31 | #ifndef _PR_MSG_H 32 | #define _PR_MSG_H 33 | 34 | #include 35 | 36 | /** \brief Class of the message. */ 37 | enum pr_id_class { 38 | /** \brief Use this class for custom messages. */ 39 | PR_ID_CLASS_USER = 0, 40 | /** \brief This class is used for the PR API messages. */ 41 | PR_ID_CLASS_PR = 176 42 | }; 43 | 44 | /** 45 | * \brief 46 | * This macro is used to generate the class-level message ID bits. 47 | */ 48 | #define PR_ID_CLASS(class) ((pr_msg_id_t)(class) << 24) 49 | 50 | /** \brief Service of the message. */ 51 | enum pr_id_svc { 52 | /** \brief Process service. */ 53 | PR_ID_SVC_PROC = 1, 54 | /** \brief Test service. */ 55 | PR_ID_SVC_TEST = 2 56 | }; 57 | 58 | /** 59 | * \brief 60 | * This macro is used to generate the service-level message ID bits. 61 | */ 62 | #define PR_ID_SVC(svc) ((pr_msg_id_t)(svc) << 16) 63 | 64 | /** 65 | * \brief 66 | * This macro is used to generate the message IDs. 67 | */ 68 | #define PR_ID_CREATE(class, svc, id) (PR_ID_CLASS(class) | PR_ID_SVC(svc) | (pr_msg_id_t)(id)) 69 | 70 | #endif /* _PR_MSG_H */ -------------------------------------------------------------------------------- /include/prs/alloc/stack.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the stack allocator declarations. 27 | */ 28 | 29 | #ifndef _PRS_ALLOC_STACK_H 30 | #define _PRS_ALLOC_STACK_H 31 | 32 | #include 33 | 34 | void* prs_stack_create(prs_size_t size, prs_size_t* available_size); 35 | void prs_stack_destroy(void* stack); 36 | 37 | prs_bool_t prs_stack_grow(void* stack, prs_size_t old_size, void* failed_ptr, prs_size_t* new_size); 38 | prs_bool_t prs_stack_address_in_range(void* stack, void* address); 39 | 40 | #endif /* _PRS_ALLOC_STACK_H */ 41 | -------------------------------------------------------------------------------- /include/prs/assert.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the assertion declarations. 27 | */ 28 | 29 | #ifndef _PRS_ASSERT_H 30 | #define _PRS_ASSERT_H 31 | 32 | #include 33 | #include 34 | 35 | /** 36 | * \brief 37 | * If \p expr is \p false, raises an assertion. 38 | * \param expr 39 | * Expression that is asserted. 40 | * \note 41 | * Assertions are only evaluated when compiling in a \p debug (\p DEBUG=1) configuration. They have no effect in a 42 | * normal (\p release) configuration. 43 | * \see 44 | * PRS_ASSERT 45 | * PRS_PRECONDITION 46 | * PRS_POSTCONDITION 47 | */ 48 | #if defined(PRS_ASSERTIONS) 49 | #define PRS_ASSERT(expr) do { if (!(expr)) { prs_assert(#expr, __FILE__, __LINE__); } } while (0); 50 | #else /* PRS_ASSERTIONS */ 51 | #define PRS_ASSERT(expr) do { (void)sizeof(expr); } while (0); 52 | #endif /* PRS_ASSERTIONS */ 53 | 54 | /** 55 | * \copydoc PRS_ASSERT 56 | */ 57 | #define PRS_PRECONDITION(expr) PRS_ASSERT(expr) 58 | 59 | /** 60 | * \copydoc PRS_ASSERT 61 | */ 62 | #define PRS_POSTCONDITION(expr) PRS_ASSERT(expr) 63 | 64 | /** 65 | * \brief 66 | * If \p expr is \p false, generates an error for the current compilation unit. 67 | * \param expr 68 | * Expression that is asserted. Must be resolvable at compile-time. 69 | */ 70 | #if __STDC_VERSION__ >= 201112L 71 | #include 72 | #define PRS_STATIC_ASSERT(expr) static_assert(expr, #expr) 73 | #else /* __STDC_VERSION__ >= 201112L */ 74 | #define PRS_STATIC_ASSERT(expr) do { enum { prs_static_assert = 1 / (expr) }; } while (0); 75 | #endif /* __STDC_VERSION__ >= 201112L */ 76 | 77 | void prs_assert(const char* expr, const char* file, int line); 78 | 79 | #endif /* _PRS_ASSERT_H */ 80 | -------------------------------------------------------------------------------- /include/prs/clock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the clock module declarations. 27 | */ 28 | 29 | #ifndef _PRS_CLOCK_H 30 | #define _PRS_CLOCK_H 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | /** 37 | * \brief 38 | * This structure provides the clock module's parameters that should be passed to \ref prs_clock_init. 39 | */ 40 | struct prs_clock_init_params { 41 | /** 42 | * \brief 43 | * If the clock module should use the current thread to use interrupts. This has a varying effect depending on the 44 | * underlying operating system. 45 | */ 46 | prs_bool_t use_current_thread; 47 | /** \brief Affinity of the clock module. This is only used if \ref use_current_thread is set to \p true. */ 48 | prs_core_mask_t affinity; 49 | /** \brief Priority of the clock module. This is only used if \ref use_current_thread is set to \p true. */ 50 | enum prs_pal_thread_prio prio; 51 | }; 52 | 53 | prs_result_t prs_clock_init(struct prs_clock_init_params* params); 54 | void prs_clock_uninit(void); 55 | 56 | struct prs_timer* prs_clock_timer(void); 57 | prs_ticks_t prs_clock_get(void); 58 | 59 | #endif /* _PRS_CLOCK_H */ 60 | -------------------------------------------------------------------------------- /include/prs/dllist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the declarations for a doubly linked list. 27 | */ 28 | 29 | #ifndef _PRS_DLLIST_H 30 | #define _PRS_DLLIST_H 31 | 32 | #include 33 | #include 34 | 35 | struct prs_dllist; 36 | struct prs_dllist_node; 37 | 38 | struct prs_dllist* prs_dllist_create(void); 39 | void prs_dllist_destroy(struct prs_dllist* dllist); 40 | 41 | prs_result_t prs_dllist_insert_before(struct prs_dllist* dllist, struct prs_dllist_node* before, void* data); 42 | void prs_dllist_remove(struct prs_dllist* dllist, struct prs_dllist_node* node); 43 | 44 | struct prs_dllist_node* prs_dllist_begin(struct prs_dllist* dllist); 45 | struct prs_dllist_node* prs_dllist_end(struct prs_dllist* dllist); 46 | struct prs_dllist_node* prs_dllist_next(struct prs_dllist* dllist, struct prs_dllist_node* node); 47 | void* prs_dllist_get_data(struct prs_dllist* dllist, struct prs_dllist_node* node); 48 | 49 | prs_bool_t prs_dllist_empty(struct prs_dllist* dllist); 50 | prs_size_t prs_dllist_size(struct prs_dllist* dllist); 51 | 52 | #define prs_dllist_foreach(dllist, node) \ 53 | for(struct prs_dllist_node* node = prs_dllist_begin(dllist); \ 54 | node != prs_dllist_end(dllist); \ 55 | node = prs_dllist_next(dllist, node)) 56 | 57 | #endif /* _PRS_DLLIST_H */ 58 | -------------------------------------------------------------------------------- /include/prs/error.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the declarations for the error module. 27 | */ 28 | 29 | #ifndef _PRS_ERROR_H 30 | #define _PRS_ERROR_H 31 | 32 | /** 33 | * \brief 34 | * From the current location, reports an error in the log. 35 | * \param desc 36 | * Description of the error. 37 | */ 38 | #define PRS_ERROR(desc) prs_error(PRS_ERROR_TYPE_CONTINUE, desc, __FILE__, __LINE__) 39 | 40 | /** 41 | * \brief 42 | * From the current location, reports an error in the log if \p cond is \p true. Must be used like an \p if statement. 43 | * \param cond 44 | * Condition that is verified. 45 | */ 46 | #define PRS_ERROR_IF(cond) if ((cond) && (prs_error(PRS_ERROR_TYPE_CONTINUE, #cond, __FILE__, __LINE__), 1)) 47 | 48 | /** 49 | * \brief 50 | * From the current location, reports an error in the log when \p cond is \p true. 51 | * \param cond 52 | * Condition that is verified. 53 | */ 54 | #define PRS_ERROR_WHEN(cond) do { if (cond) { prs_error(PRS_ERROR_TYPE_CONTINUE, #cond, __FILE__, __LINE__); } } while (0); 55 | 56 | /** 57 | * \brief 58 | * From the current location, reports and error and kills the current task. 59 | * \param desc 60 | * Description of the error. 61 | */ 62 | #define PRS_KILL_TASK(desc) prs_error(PRS_ERROR_TYPE_KILL_TASK, desc, __FILE__, __LINE__) 63 | 64 | /** 65 | * \brief 66 | * From the current location, reports and error and kills the current task when \p cond is \p true. 67 | * \param cond 68 | * Condition that is verified. 69 | */ 70 | #define PRS_KILL_TASK_WHEN(cond) do { if (cond) { prs_error(PRS_ERROR_TYPE_KILL_TASK, #cond, __FILE__, __LINE__); } } while (0); 71 | 72 | /** 73 | * \brief 74 | * From the current location, reports and error and exits PRS. 75 | * \param desc 76 | * Description of the error. 77 | */ 78 | #define PRS_FATAL(desc) prs_error(PRS_ERROR_TYPE_FATAL, desc, __FILE__, __LINE__) 79 | 80 | /** 81 | * \brief 82 | * From the current location, reports and error and exits PRS when \p cond is \p true. 83 | * \param cond 84 | * Condition that is verified. 85 | */ 86 | #define PRS_FATAL_WHEN(cond) do { if (cond) { prs_error(PRS_ERROR_TYPE_FATAL, #cond, __FILE__, __LINE__); } } while (0); 87 | 88 | enum prs_error_type { 89 | PRS_ERROR_TYPE_CONTINUE, 90 | PRS_ERROR_TYPE_KILL_TASK, 91 | PRS_ERROR_TYPE_FATAL 92 | }; 93 | 94 | void prs_error(enum prs_error_type error, const char* expr, const char* file, int line); 95 | 96 | #endif /* _PRS_ERROR_H */ 97 | -------------------------------------------------------------------------------- /include/prs/event.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the declarations for a task blocking event object. 27 | */ 28 | 29 | #ifndef _PRS_EVENT_H 30 | #define _PRS_EVENT_H 31 | 32 | #include 33 | #include 34 | 35 | struct prs_event; 36 | 37 | /** \brief Type containing the source of an event. */ 38 | typedef prs_uint8_t prs_event_type_t; 39 | 40 | /** \brief Indicates that the event object was already signaled by another source. */ 41 | #define PRS_EVENT_STATE_SIGNALED 0x1 42 | /** \brief Indicates that the event object was freed by the returning call. */ 43 | #define PRS_EVENT_STATE_FREED 0x2 44 | 45 | /** \brief Type containing the state of an event object. */ 46 | typedef prs_uint_t prs_event_state_t; 47 | 48 | struct prs_event* prs_event_create(struct prs_task* task, prs_uint_t refcnt); 49 | 50 | prs_event_state_t prs_event_signal(struct prs_event* event, prs_event_type_t type); 51 | prs_event_state_t prs_event_unref(struct prs_event* event); 52 | void prs_event_cancel(struct prs_event* event); 53 | 54 | #endif /* _PRS_EVENT_H */ 55 | -------------------------------------------------------------------------------- /include/prs/excp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the exception module declarations. 27 | */ 28 | 29 | #ifndef _PRS_EXCP_H 30 | #define _PRS_EXCP_H 31 | 32 | #include 33 | 34 | struct prs_pal_context; 35 | struct prs_task; 36 | struct prs_worker; 37 | 38 | /** \brief The possible exception types. */ 39 | enum prs_excp_type { 40 | PRS_EXCP_TYPE_UNKNOWN, 41 | PRS_EXCP_TYPE_ASSERT, 42 | PRS_EXCP_TYPE_USER, 43 | PRS_EXCP_TYPE_PRS, 44 | PRS_EXCP_TYPE_OS, 45 | PRS_EXCP_TYPE_STACK_OVERFLOW, 46 | PRS_EXCP_TYPE_SEGMENTATION_FAULT, 47 | PRS_EXCP_TYPE_ILLEGAL_INSTRUCTION, 48 | PRS_EXCP_TYPE_INTEGER, 49 | PRS_EXCP_TYPE_FLOATING_POINT, 50 | PRS_EXCP_TYPE_BUS, 51 | PRS_EXCP_TYPE_USER_INTERRUPT 52 | }; 53 | 54 | /** \brief The exception handler result codes. */ 55 | enum prs_excp_result { 56 | /** \brief The exception is fatal and PRS should immediately exit. */ 57 | PRS_EXCP_RESULT_EXIT, 58 | /** \brief The exception is fatal for the currently running task and it should be destroyed. */ 59 | PRS_EXCP_RESULT_KILL_TASK, 60 | /** \brief The exception is unimportant or corrected; the currently running task can continue its execution. */ 61 | PRS_EXCP_RESULT_CONTINUE, 62 | /** \brief The exception was not handled; forward the exception to the next exception handler. */ 63 | PRS_EXCP_RESULT_FORWARD 64 | }; 65 | 66 | /** 67 | * \brief 68 | * This structure adds information to a manually launched exception through \ref prs_excp_raise. 69 | * 70 | * The \p extra parameter of \ref prs_excp_raise should refer to this structure when the \p excp parameter is 71 | * \ref PRS_EXCP_TYPE_ASSERT, \ref PRS_EXCP_TYPE_USER, \ref PRS_EXCP_TYPE_PRS or \ref PRS_EXCP_TYPE_OS. 72 | */ 73 | struct prs_excp_raise_info { 74 | /** \brief Expression that is printed into the log. */ 75 | const char* expr; 76 | /** \brief File where the error occurred. */ 77 | const char* file; 78 | /** \brief Line in \p file where the error occurred. */ 79 | int line; 80 | /** \brief The desired action to be taken by the exception handler. */ 81 | enum prs_excp_result behavior; 82 | }; 83 | 84 | /** 85 | * \brief 86 | * Type representing an exception handler. 87 | */ 88 | typedef enum prs_excp_result (*prs_excp_handler_t)(enum prs_excp_type excp, void* extra, struct prs_worker* worker, 89 | struct prs_task* task, struct prs_pal_context* context); 90 | 91 | void prs_excp_raise(enum prs_excp_type excp, void* extra, struct prs_worker* worker, struct prs_pal_context* context); 92 | 93 | prs_result_t prs_excp_register_handler(prs_excp_handler_t handler); 94 | 95 | void prs_excp_init(void); 96 | 97 | #endif /* _PRS_EXCP_H */ 98 | -------------------------------------------------------------------------------- /include/prs/god.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the global object directory declarations. 27 | */ 28 | 29 | #ifndef _PRS_GOD_H 30 | #define _PRS_GOD_H 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | /** 37 | * \brief 38 | * Global object directory creation parameters. 39 | */ 40 | struct prs_god_create_params { 41 | /** \brief Maximum simultaneous number of entries in the global object directory. */ 42 | prs_size_t max_entries; 43 | }; 44 | 45 | prs_result_t prs_god_create(struct prs_god_create_params* params); 46 | void prs_god_destroy(void); 47 | 48 | prs_object_id_t prs_god_alloc_and_lock(void* object, struct prs_object_ops* ops); 49 | 50 | void* prs_god_find(prs_object_id_t id); 51 | 52 | void* prs_god_lock(prs_object_id_t id); 53 | void prs_god_unlock(prs_object_id_t id); 54 | prs_result_t prs_god_try_unlock_final(prs_object_id_t id); 55 | 56 | prs_result_t prs_god_object_destroy(prs_object_id_t id); 57 | 58 | void prs_god_print(void* userdata, void (*fct)(void*, const char*, ...)); 59 | 60 | #endif /* _PRS_GOD_H */ 61 | -------------------------------------------------------------------------------- /include/prs/gpd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the global pointer directory declarations. 27 | */ 28 | 29 | #ifndef _PRS_GPD_H 30 | #define _PRS_GPD_H 31 | 32 | #include 33 | 34 | void prs_gpd_init(void); 35 | void prs_gpd_uninit(void); 36 | 37 | struct prs_pd* prs_gpd_get(void); 38 | 39 | #endif /* _PRS_GPD_H */ 40 | -------------------------------------------------------------------------------- /include/prs/idllist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the declarations for an intrusive doubly linked list. 27 | */ 28 | 29 | #ifndef _PRS_IDLLIST_H 30 | #define _PRS_IDLLIST_H 31 | 32 | #include 33 | #include 34 | 35 | struct prs_idllist; 36 | 37 | struct prs_idllist_node { 38 | struct prs_idllist_node* next; 39 | struct prs_idllist_node* prev; 40 | 41 | #if defined(DEBUG) 42 | struct prs_idllist* list; 43 | #endif /* DEBUG */ 44 | }; 45 | 46 | struct prs_idllist_create_params { 47 | prs_size_t node_offset; 48 | }; 49 | 50 | struct prs_idllist* prs_idllist_create(struct prs_idllist_create_params* params); 51 | void prs_idllist_destroy(struct prs_idllist* idllist); 52 | 53 | void prs_idllist_insert_before(struct prs_idllist* idllist, struct prs_idllist_node* before, struct prs_idllist_node* node); 54 | void prs_idllist_remove(struct prs_idllist* idllist, struct prs_idllist_node* node); 55 | 56 | struct prs_idllist_node* prs_idllist_begin(struct prs_idllist* idllist); 57 | struct prs_idllist_node* prs_idllist_end(struct prs_idllist* idllist); 58 | struct prs_idllist_node* prs_idllist_next(struct prs_idllist* idllist, struct prs_idllist_node* node); 59 | void* prs_idllist_get_data(struct prs_idllist* idllist, struct prs_idllist_node* node); 60 | prs_bool_t prs_idllist_is_inserted(struct prs_idllist* idllist, struct prs_idllist_node* node); 61 | 62 | prs_bool_t prs_idllist_empty(struct prs_idllist* idllist); 63 | prs_size_t prs_idllist_size(struct prs_idllist* idllist); 64 | 65 | #define prs_idllist_foreach(idllist, node) \ 66 | for(struct prs_idllist_node* node = prs_idllist_begin(idllist); \ 67 | node != prs_idllist_end(idllist); \ 68 | node = prs_idllist_next(idllist, node)) 69 | 70 | #endif /* _PRS_IDLLIST_H */ 71 | -------------------------------------------------------------------------------- /include/prs/init.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PRS initialization and uninitialization declarations. 27 | */ 28 | 29 | #ifndef _PRS_INIT_H 30 | #define _PRS_INIT_H 31 | 32 | #include 33 | 34 | /** 35 | * \brief 36 | * PRS initialization parameters. 37 | */ 38 | struct prs_init_params { 39 | /** \brief If PRS should use the currently executing thread as a worker. */ 40 | prs_bool_t use_current_thread; 41 | /** \brief The CPU cores that PRS should spawn a worker and scheduler on. */ 42 | prs_core_mask_t core_mask; 43 | }; 44 | 45 | int prs_init(struct prs_init_params* params); 46 | 47 | void prs_fast_exit(int status); 48 | void prs_exit(int status); 49 | void prs_exit_from_excp(int status); 50 | 51 | #endif /* _PRS_INIT_H */ 52 | -------------------------------------------------------------------------------- /include/prs/log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the log module declarations. 27 | */ 28 | 29 | #ifndef _PRS_LOG_H 30 | #define _PRS_LOG_H 31 | 32 | #include 33 | 34 | #include 35 | #include 36 | 37 | /** 38 | * \def PRS_FTRACE 39 | * \brief 40 | * Prints a trace in the log with the function name as a prefix. Does not print the trace when 41 | * \ref PRS_FUNCTION_TRACES is not defined. 42 | */ 43 | #if defined(PRS_FUNCTION_TRACES) 44 | #define PRS_FTRACE(...) prs_log_ftrace(__FILE__, __LINE__, __func__, __VA_ARGS__) 45 | #else 46 | #define PRS_FTRACE(...) 47 | #endif 48 | 49 | void prs_log_init(void); 50 | void prs_log_uninit(void); 51 | 52 | void prs_log_vprint(const char* file, int line, const char* function, const char* fmt, va_list va); 53 | 54 | void prs_log_print(const char* fmt, ...); 55 | 56 | void prs_log_ftrace(const char* file, int line, const char* function, const char* fmt, ...); 57 | 58 | prs_int_t prs_log_flush(void); 59 | 60 | #endif /* _PRS_LOG_H */ 61 | -------------------------------------------------------------------------------- /include/prs/mpmcq.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the declarations for a multi-producer multi-consumer queue. 27 | */ 28 | 29 | #ifndef _PRS_MPMCQ_H 30 | #define _PRS_MPMCQ_H 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | struct prs_mpmcq; 37 | struct prs_mpmcq_node; 38 | 39 | struct prs_mpmcq_create_params { 40 | void* area; 41 | struct prs_pd* pd; 42 | struct prs_pd_create_params* pd_params; 43 | }; 44 | 45 | prs_size_t prs_mpmcq_struct_size(void); 46 | 47 | struct prs_mpmcq* prs_mpmcq_create(struct prs_mpmcq_create_params* params); 48 | void prs_mpmcq_destroy(struct prs_mpmcq* mpmcq); 49 | 50 | prs_result_t prs_mpmcq_push(struct prs_mpmcq* mpmcq, void* data); 51 | prs_result_t prs_mpmcq_pop(struct prs_mpmcq* mpmcq, void* data_ptr); 52 | 53 | #endif /* _PRS_MPMCQ_H */ 54 | -------------------------------------------------------------------------------- /include/prs/mpmcring.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the declarations for a multi-producer multi-consumer ring. 27 | */ 28 | 29 | #ifndef _PRS_MPMCRING_H 30 | #define _PRS_MPMCRING_H 31 | 32 | #include 33 | #include 34 | 35 | struct prs_mpmcring; 36 | 37 | struct prs_mpmcring_create_params { 38 | prs_size_t data_size; 39 | prs_size_t max_entries; 40 | }; 41 | 42 | struct prs_mpmcring* prs_mpmcring_create(struct prs_mpmcring_create_params* params); 43 | void prs_mpmcring_destroy(struct prs_mpmcring* mpmcring); 44 | 45 | void* prs_mpmcring_alloc(struct prs_mpmcring* mpmcring); 46 | void prs_mpmcring_free(struct prs_mpmcring* mpmcring, void* data); 47 | 48 | void prs_mpmcring_push(struct prs_mpmcring* mpmcring, void* data); 49 | void* prs_mpmcring_pop(struct prs_mpmcring* mpmcring); 50 | 51 | #endif /* _PRS_MPMCRING_H */ 52 | -------------------------------------------------------------------------------- /include/prs/mpsciq.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the declarations for a multi-producer single-consumer intrusive queue. 27 | */ 28 | 29 | #ifndef _PRS_MPSCIQ_H 30 | #define _PRS_MPSCIQ_H 31 | 32 | #include 33 | #include 34 | 35 | struct prs_mpsciq; 36 | 37 | struct prs_mpsciq_node { 38 | #if defined(DEBUG) 39 | struct prs_mpsciq* mpsciq; 40 | #endif /* defined(DEBUG) */ 41 | struct prs_mpsciq_node* next; 42 | struct prs_mpsciq_node* prev; 43 | }; 44 | 45 | struct prs_mpsciq_create_params { 46 | prs_size_t node_offset; 47 | }; 48 | 49 | struct prs_mpsciq* prs_mpsciq_create(struct prs_mpsciq_create_params* params); 50 | void prs_mpsciq_destroy(struct prs_mpsciq* mpsciq); 51 | 52 | void prs_mpsciq_push(struct prs_mpsciq* mpsciq, struct prs_mpsciq_node* node); 53 | void prs_mpsciq_remove(struct prs_mpsciq* mpsciq, struct prs_mpsciq_node* node); 54 | 55 | struct prs_mpsciq_node* prs_mpsciq_begin(struct prs_mpsciq* mpsciq); 56 | struct prs_mpsciq_node* prs_mpsciq_rbegin(struct prs_mpsciq* mpsciq); 57 | struct prs_mpsciq_node* prs_mpsciq_end(struct prs_mpsciq* mpsciq); 58 | struct prs_mpsciq_node* prs_mpsciq_next(struct prs_mpsciq* mpsciq, struct prs_mpsciq_node* node); 59 | struct prs_mpsciq_node* prs_mpsciq_prev(struct prs_mpsciq* mpscq, struct prs_mpsciq_node* node); 60 | void* prs_mpsciq_get_data(struct prs_mpsciq* mpsciq, struct prs_mpsciq_node* node); 61 | prs_bool_t prs_mpsciq_is_inserted(struct prs_mpsciq* mpsciq, struct prs_mpsciq_node* node); 62 | 63 | #define prs_mpsciq_foreach(mpsciq, node) \ 64 | for(struct prs_mpsciq_node* node = prs_mpsciq_begin(mpsciq); \ 65 | node != prs_mpsciq_end(mpsciq); \ 66 | node = prs_mpsciq_next(mpsciq, node)) 67 | 68 | #define prs_mpsciq_rforeach(mpsciq, node) \ 69 | for(struct prs_mpsciq_node* node = prs_mpsciq_rbegin(mpsciq); \ 70 | node != prs_mpsciq_end(mpsciq); \ 71 | node = prs_mpsciq_prev(mpsciq, node)) 72 | 73 | #endif /* _PRS_MPSCIQ_H */ 74 | -------------------------------------------------------------------------------- /include/prs/mpscq.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the declarations for a multi-producer single-consumer queue. 27 | */ 28 | 29 | #ifndef _PRS_MPSCQ_H 30 | #define _PRS_MPSCQ_H 31 | 32 | #include 33 | #include 34 | 35 | struct prs_mpscq; 36 | struct prs_mpscq_node; 37 | 38 | struct prs_mpscq* prs_mpscq_create(void); 39 | void prs_mpscq_destroy(struct prs_mpscq* mpscq); 40 | 41 | prs_result_t prs_mpscq_push(struct prs_mpscq* mpscq, void* data); 42 | void prs_mpscq_remove(struct prs_mpscq* mpscq, struct prs_mpscq_node* node); 43 | 44 | struct prs_mpscq_node* prs_mpscq_begin(struct prs_mpscq* mpscq); 45 | struct prs_mpscq_node* prs_mpscq_rbegin(struct prs_mpscq* mpscq); 46 | struct prs_mpscq_node* prs_mpscq_end(struct prs_mpscq* mpscq); 47 | struct prs_mpscq_node* prs_mpscq_next(struct prs_mpscq* mpscq, struct prs_mpscq_node* node); 48 | struct prs_mpscq_node* prs_mpscq_prev(struct prs_mpscq* mpscq, struct prs_mpscq_node* node); 49 | void* prs_mpscq_get_data(struct prs_mpscq* mpscq, struct prs_mpscq_node* node); 50 | 51 | #define prs_mpscq_foreach(mpscq, node) \ 52 | for(struct prs_mpscq_node* node = prs_mpscq_begin(mpscq); \ 53 | node != prs_mpscq_end(mpscq); \ 54 | node = prs_mpscq_next(mpscq, node)) 55 | 56 | #define prs_mpscq_rforeach(mpscq, node) \ 57 | for(struct prs_mpscq_node* node = prs_mpscq_rbegin(mpscq); \ 58 | node != prs_mpscq_end(mpscq); \ 59 | node = prs_mpscq_prev(mpscq, node)) 60 | 61 | #endif /* _PRS_MPSCQ_H */ 62 | -------------------------------------------------------------------------------- /include/prs/msg.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the message declarations. 27 | */ 28 | 29 | #ifndef _PRS_MSG_H 30 | #define _PRS_MSG_H 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | /** 37 | * \brief 38 | * This structure contains the header fields for all PRS messages exchanged between tasks. 39 | * \note 40 | * This structure should only be used directly by PRS internal functions. 41 | */ 42 | struct prs_msg { 43 | /** \brief Node used when the message is in a message queue. */ 44 | struct prs_mpsciq_node node; 45 | /** \brief Owner of the message. */ 46 | prs_task_id_t owner; 47 | /** \brief Last sender of the message. */ 48 | prs_task_id_t sender; 49 | /** \brief Data (payload) of the message. The data may extend beyond this field. */ 50 | prs_uint8_t data[PRS_PAL_POINTER_SIZE]; 51 | }; 52 | 53 | /** \brief Size of the message header. */ 54 | #define PRS_MSG_OVERHEAD (offsetof(struct prs_msg, data)) 55 | /** \brief Returns the message header from the message payload. */ 56 | #define PRS_MSG_FROM_DATA(data) ((struct prs_msg*)((prs_uint8_t*)data - PRS_MSG_OVERHEAD)) 57 | 58 | #endif /* _PRS_MSG_H */ 59 | -------------------------------------------------------------------------------- /include/prs/msgq.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the message queue declarations. 27 | */ 28 | 29 | #ifndef _PRS_MSGQ_H 30 | #define _PRS_MSGQ_H 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | struct prs_msgq_filter; 38 | struct prs_msgq; 39 | struct prs_msg; 40 | 41 | /** 42 | * \brief 43 | * Message queue creation parameters. 44 | */ 45 | struct prs_msgq_create_params { 46 | /** 47 | * \brief 48 | * Pointer directory to use for assigning filters to the message queue. When \p null, the global pointer directory 49 | * (GPD) is used. 50 | */ 51 | struct prs_pd* pd; 52 | }; 53 | 54 | /** 55 | * \brief 56 | * Defines a message queue filter function. The filter function returns \p true when the specified message matches 57 | * the filter. 58 | */ 59 | typedef prs_bool_t (*prs_msgq_filter_function_t)(void* userdata, struct prs_msg* msg); 60 | 61 | struct prs_msgq* prs_msgq_create(struct prs_msgq_create_params* params); 62 | void prs_msgq_destroy(struct prs_msgq* msgq); 63 | 64 | void prs_msgq_send(struct prs_msgq* msgq, struct prs_msg* msg); 65 | 66 | struct prs_msg* prs_msgq_recv(struct prs_msgq* msgq); 67 | struct prs_msg* prs_msgq_recv_timeout(struct prs_msgq* msgq, prs_ticks_t timeout); 68 | struct prs_msg* prs_msgq_recv_filter(struct prs_msgq* msgq, void* userdata, prs_size_t userdata_size, 69 | prs_msgq_filter_function_t function); 70 | struct prs_msg* prs_msgq_recv_filter_timeout(struct prs_msgq* msgq, void* userdata, prs_size_t userdata_size, 71 | prs_msgq_filter_function_t function, prs_ticks_t timeout); 72 | 73 | #endif /* _PRS_MSGQ_H */ 74 | -------------------------------------------------------------------------------- /include/prs/name.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the name resolver declarations. 27 | */ 28 | 29 | #ifndef _PRS_NAME_H 30 | #define _PRS_NAME_H 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | struct prs_name; 37 | 38 | /** 39 | * \brief 40 | * Name resolver creation parameters. 41 | */ 42 | struct prs_name_create_params { 43 | /** \brief Maximum number of entries in the name resolver's hash table. */ 44 | prs_size_t max_entries; 45 | /** \brief Offset of the name of the object relative to its object data structure. */ 46 | prs_size_t string_offset; 47 | }; 48 | 49 | struct prs_name* prs_name_create(struct prs_name_create_params* params); 50 | void prs_name_destroy(struct prs_name* name); 51 | 52 | prs_result_t prs_name_alloc(struct prs_name* name, prs_object_id_t id); 53 | prs_result_t prs_name_free(struct prs_name* name, prs_object_id_t id); 54 | prs_object_id_t prs_name_find(struct prs_name* name, const char* key); 55 | prs_object_id_t prs_name_find_and_lock(struct prs_name* name, const char* key); 56 | 57 | #endif /* _PRS_NAME_H */ 58 | -------------------------------------------------------------------------------- /include/prs/object.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PRS object declarations. 27 | */ 28 | 29 | #ifndef _PRS_OBJECT_H 30 | #define _PRS_OBJECT_H 31 | 32 | #include 33 | 34 | /** 35 | * \brief 36 | * Invalid object ID. 37 | */ 38 | #define PRS_OBJECT_ID_INVALID ((prs_object_id_t)0) 39 | 40 | /** 41 | * \brief 42 | * Standard PRS object operations. These operations are used by the global object directory. 43 | */ 44 | struct prs_object_ops { 45 | /** \brief Initiates the destruction of the object. */ 46 | void (*destroy)(void* object); 47 | /** \brief Frees the object's data structures when it is no longer referenced. */ 48 | void (*free)(void* object); 49 | /** \brief Prints brief information about the object. */ 50 | void (*print)(void* object, void* userdata, void (*fct)(void*, const char*, ...)); 51 | }; 52 | 53 | /** \brief Process object ID type. */ 54 | typedef prs_object_id_t prs_proc_id_t; 55 | /** \brief Task object ID type. */ 56 | typedef prs_object_id_t prs_task_id_t; 57 | /** \brief Scheduler object ID type. */ 58 | typedef prs_object_id_t prs_sched_id_t; 59 | /** \brief Worker object ID type. */ 60 | typedef prs_object_id_t prs_worker_id_t; 61 | /** \brief Semaphore object ID type. */ 62 | typedef prs_object_id_t prs_sem_id_t; 63 | 64 | #endif /* _PRS_OBJECT_H */ 65 | -------------------------------------------------------------------------------- /include/prs/pal/arch.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL architecture preprocessor definitions. 27 | */ 28 | 29 | #ifndef _PRS_PAL_ARCH_H 30 | #define _PRS_PAL_ARCH_H 31 | 32 | #include 33 | 34 | /** \brief x86 architecture */ 35 | #define PRS_PAL_ARCH_X86 0 36 | /** \brief AMD64 architecture */ 37 | #define PRS_PAL_ARCH_AMD64 1 38 | /** \brief ARM architecture */ 39 | #define PRS_PAL_ARCH_ARM 2 40 | 41 | #if PRS_PAL_COMPILER == PRS_PAL_COMPILER_GCC 42 | #define PRS_PAL_POINTER_SIZE __SIZEOF_POINTER__ 43 | #if defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) 44 | #define PRS_PAL_REGISTER_SIZE 4 45 | #define PRS_PAL_ARCH PRS_PAL_ARCH_X86 46 | #elif defined(__amd64__) 47 | #define PRS_PAL_REGISTER_SIZE 8 48 | #define PRS_PAL_ARCH PRS_PAL_ARCH_AMD64 49 | #elif defined(__arm__) 50 | #define PRS_PAL_REGISTER_SIZE 4 51 | #define PRS_PAL_ARCH PRS_PAL_ARCH_ARM 52 | #endif 53 | #elif PRS_PAL_COMPILER == PRS_PAL_COMPILER_MSVC 54 | #if defined(_M_IX86) 55 | #define PRS_PAL_POINTER_SIZE 4 56 | #define PRS_PAL_REGISTER_SIZE 4 57 | #define PRS_PAL_ARCH PRS_PAL_ARCH_X86 58 | #elif defined(_M_AMD64) 59 | #define PRS_PAL_POINTER_SIZE 8 60 | #define PRS_PAL_REGISTER_SIZE 8 61 | #define PRS_PAL_ARCH PRS_PAL_ARCH_AMD64 62 | #endif 63 | #endif 64 | 65 | #if PRS_PAL_ARCH == PRS_PAL_ARCH_X86 66 | #define PRS_PAL_ARCH_NAME "x86" 67 | #elif PRS_PAL_ARCH == PRS_PAL_ARCH_AMD64 68 | #define PRS_PAL_ARCH_NAME "amd64" 69 | #elif PRS_PAL_ARCH == PRS_PAL_ARCH_ARM 70 | #define PRS_PAL_ARCH_NAME "arm" 71 | #endif 72 | 73 | /** 74 | * \def PRS_PAL_POINTER_SIZE 75 | * \brief 76 | * Defines the size of a pointer in the current architecture. 77 | */ 78 | #if !defined(PRS_PAL_POINTER_SIZE) 79 | #error PRS_PAL_POINTER_SIZE is not defined 80 | #define PRS_PAL_POINTER_SIZE /* doxygen */ 81 | #endif 82 | 83 | /** 84 | * \def PRS_PAL_REGISTER_SIZE 85 | * \brief 86 | * Defines the size of a register in the current architecture. 87 | */ 88 | #if !defined(PRS_PAL_REGISTER_SIZE) 89 | #error PRS_PAL_REGISTER_SIZE is not defined 90 | #define PRS_PAL_REGISTER_SIZE /* doxygen */ 91 | #endif 92 | 93 | /** 94 | * \def PRS_PAL_ARCH 95 | * \brief 96 | * Defines the current architecture. 97 | * 98 | * It is one of the following: 99 | * - \ref PRS_PAL_ARCH_X86 100 | * - \ref PRS_PAL_ARCH_AMD64 101 | * - \ref PRS_PAL_ARCH_ARM 102 | */ 103 | #if !defined(PRS_PAL_ARCH) 104 | #error PRS_PAL_ARCH is not defined 105 | #define PRS_PAL_ARCH /* doxygen */ 106 | #endif 107 | 108 | /** 109 | * \def PRS_PAL_ARCH_NAME 110 | * \brief 111 | * Defines the current architecture name in a string literal format. 112 | */ 113 | #if !defined(PRS_PAL_ARCH_NAME) 114 | #error PRS_PAL_ARCH_NAME is not defined 115 | #define PRS_PAL_ARCH_NAME /* doxygen */ 116 | #endif 117 | 118 | #endif /* _PRS_PAL_ARCH_H */ 119 | -------------------------------------------------------------------------------- /include/prs/pal/assert.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL assertion declarations. 27 | */ 28 | 29 | #ifndef _PRS_PAL_ASSERT_H 30 | #define _PRS_PAL_ASSERT_H 31 | 32 | /** 33 | * \brief 34 | * This function is called by PRS to generate an assertion failure. 35 | * \param expr 36 | * Expression that caused the assertion failure. 37 | * \param file 38 | * File where the assertion occurred. 39 | * \param line 40 | * Line in \p file where the assertion occurred. 41 | */ 42 | void prs_pal_assert(const char* expr, const char* file, int line); 43 | 44 | #endif /* _PRS_PAL_ASSERT_H */ -------------------------------------------------------------------------------- /include/prs/pal/compiler.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL compiler preprocessor definitions. 27 | */ 28 | 29 | #ifndef _PRS_PAL_COMPILER_H 30 | #define _PRS_PAL_COMPILER_H 31 | 32 | /** \brief GCC compiler. */ 33 | #define PRS_PAL_COMPILER_GCC 0 34 | /** \brief MSVC compiler. */ 35 | #define PRS_PAL_COMPILER_MSVC 1 36 | 37 | #if defined(__GNUC__) 38 | #define PRS_PAL_COMPILER PRS_PAL_COMPILER_GCC 39 | #define PRS_PAL_COMPILER_NAME "gcc" 40 | #define __PRS_PAL_COMPILER_STR(x) #x 41 | #define _PRS_PAL_COMPILER_STR(x) __PRS_PAL_COMPILER_STR(x) 42 | #define PRS_PAL_COMPILER_VERSION _PRS_PAL_COMPILER_STR(__GNUC__) \ 43 | "."_PRS_PAL_COMPILER_STR(__GNUC_MINOR__) \ 44 | "."_PRS_PAL_COMPILER_STR(__GNUC_PATCHLEVEL__) 45 | #elif defined(_MSC_VER) 46 | #define PRS_PAL_COMPILER PRS_PAL_COMPILER_MSVC 47 | #define PRS_PAL_COMPILER_NAME "msvc" 48 | #define __PRS_PAL_COMPILER_STR(x) #x 49 | #define _PRS_PAL_COMPILER_STR(x) __PRS_PAL_COMPILER_STR(x) 50 | #define PRS_PAL_COMPILER_VERSION _PRS_PAL_COMPILER_STR(_MSC_FULL_VER) 51 | #endif 52 | 53 | /** 54 | * \def PRS_PAL_COMPILER 55 | * \brief 56 | * Defines the current compiler. 57 | * 58 | * It is one of the following: 59 | * - \ref PRS_PAL_COMPILER_GCC 60 | * - \ref PRS_PAL_COMPILER_MSVC 61 | */ 62 | #if !defined(PRS_PAL_COMPILER) 63 | #error PRS_PAL_COMPILER is not defined 64 | #define PRS_PAL_COMPILER /* doxygen */ 65 | #endif 66 | 67 | /** 68 | * \def PRS_PAL_COMPILER_NAME 69 | * \brief 70 | * Defines the current compiler name in a string literal format. 71 | */ 72 | #if !defined(PRS_PAL_COMPILER_NAME) 73 | #error PRS_PAL_COMPILER_NAME is not defined 74 | #define PRS_PAL_COMPILER_NAME /* doxygen */ 75 | #endif 76 | 77 | /** 78 | * \def PRS_PAL_COMPILER_VERSION 79 | * \brief 80 | * Defines the current compiler version in a string literal format. 81 | */ 82 | #if !defined(PRS_PAL_COMPILER_VERSION) 83 | #error PRS_PAL_COMPILER_VERSION is not defined 84 | #define PRS_PAL_COMPILER_VERSION /* doxygen */ 85 | #endif 86 | 87 | #endif /* _PRS_PAL_COMPILER_H */ 88 | -------------------------------------------------------------------------------- /include/prs/pal/config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL configuration preprocessor definitions. 27 | */ 28 | 29 | #ifndef _PRS_PAL_CONFIG_H 30 | #define _PRS_PAL_CONFIG_H 31 | 32 | /** \brief Debug configuration. */ 33 | #define PRS_PAL_CONFIG_DEBUG 0 34 | /** \brief Release configuration. */ 35 | #define PRS_PAL_CONFIG_RELEASE 1 36 | 37 | #if defined(DEBUG) 38 | #define PRS_PAL_CONFIG PRS_PAL_CONFIG_DEBUG 39 | #define PRS_PAL_CONFIG_NAME "debug" 40 | #else /* DEBUG */ 41 | #define PRS_PAL_CONFIG PRS_PAL_CONFIG_RELEASE 42 | #define PRS_PAL_CONFIG_NAME "release" 43 | #endif /* DEBUG */ 44 | 45 | /** 46 | * \def PRS_PAL_CONFIG 47 | * \brief 48 | * Defines the current configuration. 49 | * 50 | * It is one of the following: 51 | * - \ref PRS_PAL_CONFIG_DEBUG 52 | * - \ref PRS_PAL_CONFIG_RELEASE 53 | */ 54 | #if !defined(PRS_PAL_CONFIG) 55 | #error PRS_PAL_CONFIG is not defined 56 | #define PRS_PAL_CONFIG /* doxygen */ 57 | #endif 58 | 59 | /** 60 | * \def PRS_PAL_CONFIG_NAME 61 | * \brief 62 | * Defines the current configuration name in a string literal format. 63 | */ 64 | #if !defined(PRS_PAL_CONFIG_NAME) 65 | #error PRS_PAL_CONFIG_NAME is not defined 66 | #define PRS_PAL_CONFIG /* doxygen */ 67 | #endif 68 | 69 | #endif /* _PRS_PAL_CONFIG_H */ -------------------------------------------------------------------------------- /include/prs/pal/context.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL register context declarations. 27 | */ 28 | 29 | #ifndef _PRS_PAL_CONTEXT_H 30 | #define _PRS_PAL_CONTEXT_H 31 | 32 | #include 33 | 34 | struct prs_pal_context; 35 | 36 | /** 37 | * \brief 38 | * Allocates a context. 39 | */ 40 | struct prs_pal_context* prs_pal_context_alloc(void); 41 | 42 | /** 43 | * \brief 44 | * Frees a context allocated by \ref prs_pal_context_alloc. 45 | */ 46 | void prs_pal_context_free(struct prs_pal_context* context); 47 | 48 | /** 49 | * \brief 50 | * Copies a context. 51 | * \param dst 52 | * Destination context. 53 | * \param src 54 | * Source context. 55 | */ 56 | void prs_pal_context_copy(struct prs_pal_context* dst, struct prs_pal_context* src); 57 | 58 | /** 59 | * \brief 60 | * Fills the context with the provided stack and function entry point. 61 | * \param context 62 | * Context to write to. 63 | * \param stack 64 | * Stack where the stack frame will be written to. 65 | * \param function 66 | * Function that will be called once the context is restored. 67 | * \param argc 68 | * Number of arguments of \p function. 69 | * \param ... 70 | * Variable arguments. 71 | */ 72 | void prs_pal_context_make(struct prs_pal_context* context, void* stack, void (*function)(), int argc, ...); 73 | 74 | /** 75 | * \brief 76 | * Pushes a new stack frame on top of the stack in \p context. The stack frame is a call to \p function. When 77 | * \p function returns, the stack and the register context will be identical as they were before this call. 78 | * \param context 79 | * Context to write to. 80 | * \param function 81 | * Function that will be called once the context is restored. 82 | * \param argc 83 | * Number of arguments of \p function. 84 | * \param ... 85 | * Variable arguments. 86 | */ 87 | void prs_pal_context_add(struct prs_pal_context* context, void (*function)(), int argc, ...); 88 | 89 | /** 90 | * \brief 91 | * Save the current register context in \p save and use the context in \p restore as the current register context. 92 | * \param save 93 | * Context to save to. 94 | * \param restore 95 | * Context to restore from. 96 | */ 97 | void prs_pal_context_swap(struct prs_pal_context* save, struct prs_pal_context* restore); 98 | 99 | /** 100 | * \brief 101 | * Returns if the current stack pointer in \p context is using the specified \p stack. 102 | * \param context 103 | * Context to verify. 104 | * \param stack 105 | * Top of the stack to verify. 106 | * \param stack_size 107 | * Size of the specified \p stack. 108 | */ 109 | prs_bool_t prs_pal_context_is_using_stack(struct prs_pal_context* context, void* stack, prs_size_t stack_size); 110 | 111 | /** 112 | * \brief 113 | * Returns the value of the intruction pointer in \p context. 114 | * \param context 115 | * Context to get the instruction pointer from. 116 | */ 117 | void* prs_pal_context_get_ip(struct prs_pal_context* context); 118 | 119 | #endif /* _PRS_PAL_CONTEXT_H */ 120 | -------------------------------------------------------------------------------- /include/prs/pal/crt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL CRT definitions. 27 | */ 28 | 29 | #ifndef _PRS_PAL_CRT_H 30 | #define _PRS_PAL_CRT_H 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | /** 37 | * \fn _prs_pal_crt_ctors 38 | * \brief 39 | * This function is called when a dynamically loaded PRS executable starts. It must initialize all the C constructs 40 | * necessary for the executable's function. 41 | */ 42 | 43 | /** 44 | * \fn _prs_pal_crt_dtors 45 | * \brief 46 | * This function is called when a dynamically loaded PRS executable is destroyed. It must uninitialize all the C 47 | * constructs that were initialized in \ref _prs_pal_crt_ctors. 48 | */ 49 | 50 | #if PRS_PAL_COMPILER == PRS_PAL_COMPILER_GCC 51 | 52 | typedef void (*_prs_pal_crt_func_ptr_t)(void); 53 | 54 | #if PRS_PAL_OS == PRS_PAL_OS_WINDOWS 55 | 56 | /* MinGW still defines __CTOR_LIST__ and __DTOR_LIST__ in the default linker script */ 57 | extern _prs_pal_crt_func_ptr_t __CTOR_LIST__[]; 58 | extern _prs_pal_crt_func_ptr_t __DTOR_LIST__[]; 59 | 60 | static PRS_INLINE void _prs_pal_crt_ctors(void) 61 | { 62 | unsigned long nptrs = (unsigned long)(uintptr_t)__CTOR_LIST__[0]; 63 | unsigned long i; 64 | 65 | if (nptrs == (unsigned long)-1) { 66 | for (nptrs = 0; __CTOR_LIST__[nptrs + 1]; nptrs++) { 67 | } 68 | } 69 | 70 | for (i = nptrs; i >= 1; i--) { 71 | __CTOR_LIST__[i](); 72 | } 73 | } 74 | 75 | static void _prs_pal_crt_dtors(void) 76 | { 77 | static _prs_pal_crt_func_ptr_t *p = __DTOR_LIST__ + 1; 78 | 79 | while (*p) { 80 | (*p)(); 81 | ++p; 82 | } 83 | } 84 | 85 | #else /* PRS_PAL_OS == PRS_PAL_OS_WINDOWS */ 86 | 87 | /* On Linux, the .init_array section is used with the following symbols */ 88 | extern _prs_pal_crt_func_ptr_t __preinit_array_start[]; 89 | extern _prs_pal_crt_func_ptr_t __preinit_array_end[]; 90 | extern _prs_pal_crt_func_ptr_t __init_array_start[]; 91 | extern _prs_pal_crt_func_ptr_t __init_array_end[]; 92 | extern _prs_pal_crt_func_ptr_t __fini_array_start[]; 93 | extern _prs_pal_crt_func_ptr_t __fini_array_end[]; 94 | 95 | static PRS_INLINE void _prs_pal_crt_ctors(void) 96 | { 97 | const unsigned long preinit_array_count = __preinit_array_end - __preinit_array_start; 98 | const unsigned long init_array_count = __init_array_end - __init_array_start; 99 | 100 | for (unsigned long i = 0; i < preinit_array_count; ++i) { 101 | __preinit_array_start[i](); 102 | } 103 | 104 | for (unsigned long i = 0; i < init_array_count; ++i) { 105 | __init_array_start[i](); 106 | } 107 | } 108 | 109 | static void _prs_pal_crt_dtors(void) 110 | { 111 | const unsigned long fini_array_count = __fini_array_end - __fini_array_start; 112 | 113 | unsigned long i = fini_array_count; 114 | while (i-- > 0) { 115 | __fini_array_start[i](); 116 | } 117 | } 118 | 119 | #endif /* PRS_PAL_OS != PRS_PAL_OS_WINDOWS */ 120 | 121 | #endif /* PRS_PAL_COMPILER == PRS_PAL_COMPILER_GCC */ 122 | 123 | #endif /* _PRS_PAL_CRT_H */ 124 | -------------------------------------------------------------------------------- /include/prs/pal/cycles.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL cycle counter definitions. 27 | */ 28 | 29 | #ifndef _PRS_PAL_CYCLES_H 30 | #define _PRS_PAL_CYCLES_H 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | /** 38 | * \typedef prs_cycles_t 39 | * \brief 40 | * Cycle counter type. 41 | */ 42 | 43 | /** 44 | * \fn prs_cycles_now 45 | * \brief 46 | * This function returns the value of a cycle counter. Ideally, the cycle counter should ignore hardware sleep states 47 | * and be consistent across CPU cores on the same system. 48 | */ 49 | 50 | #if PRS_PAL_ARCH == PRS_PAL_ARCH_X86 || PRS_PAL_ARCH == PRS_PAL_ARCH_AMD64 51 | typedef prs_uint64_t prs_cycles_t; 52 | 53 | #if PRS_PAL_COMPILER == PRS_PAL_COMPILER_GCC 54 | 55 | static PRS_INLINE prs_cycles_t prs_cycles_now(void) 56 | { 57 | prs_uint32_t hi; 58 | prs_uint32_t lo; 59 | 60 | /* 61 | * For this to work properly, the CPU must have the Invariant TSC feature so that the cycle count increments 62 | * symmetrically on multiple cores. 63 | * On Linux, the feature can be observed through the "nonstop_tsc" flag in /proc/cpuinfo. 64 | */ 65 | __asm__ __volatile__ ( 66 | "rdtsc" 67 | : "=a"(lo), 68 | "=d"(hi) 69 | ); 70 | 71 | return (prs_uint64_t)lo | ((prs_uint64_t)hi << 32); 72 | } 73 | 74 | #endif /* PRS_PAL_COMPILER == PRS_PAL_COMPILER_GCC */ 75 | 76 | #endif /* PRS_PAL_ARCH == PRS_PAL_ARCH_X86 || PRS_PAL_ARCH == PRS_PAL_ARCH_AMD64 */ 77 | 78 | #endif /* _PRS_PAL_CYCLES_H */ 79 | -------------------------------------------------------------------------------- /include/prs/pal/excp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL exception declarations. 27 | */ 28 | 29 | #ifndef _PRS_PAL_EXCP_H 30 | #define _PRS_PAL_EXCP_H 31 | 32 | #include 33 | #include 34 | 35 | /** 36 | * \brief 37 | * This function is called when a worker is initialized. It should set up an exception handler for the worker. 38 | */ 39 | prs_result_t prs_pal_excp_init_worker(struct prs_worker* worker); 40 | 41 | /** 42 | * \brief 43 | * This function is called when a worker is about to be stopped. It should clear the exception handler for the worker. 44 | */ 45 | void prs_pal_excp_uninit_worker(struct prs_worker* worker); 46 | 47 | #endif /* _PRS_PAL_EXCP_H */ 48 | -------------------------------------------------------------------------------- /include/prs/pal/exit.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL exit declarations. 27 | */ 28 | 29 | #ifndef _PRS_PAL_EXIT_H 30 | #define _PRS_PAL_EXIT_H 31 | 32 | /** 33 | * \brief 34 | * This function is called by PRS when it needs to stop the current operating system process. 35 | * \param status 36 | * Exit status code. 37 | */ 38 | void prs_pal_exit(int status); 39 | 40 | #endif /* _PRS_PAL_EXIT_H */ 41 | -------------------------------------------------------------------------------- /include/prs/pal/inline.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL inline preprocessor definitions. 27 | */ 28 | 29 | #ifndef _PRS_PAL_INLINE_H 30 | #define _PRS_PAL_INLINE_H 31 | 32 | #include 33 | 34 | #if PRS_PAL_COMPILER == PRS_PAL_COMPILER_GCC 35 | #define PRS_INLINE __inline 36 | #elif PRS_PAL_COMPILER == PRS_PAL_COMPILER_MSVC 37 | #define PRS_INLINE __inline 38 | #endif 39 | 40 | /** 41 | * \def PRS_INLINE 42 | * \brief 43 | * Defines the inline compiler directive. 44 | */ 45 | #if !defined(PRS_INLINE) 46 | #error PRS_INLINE is not defined 47 | #define PRS_INLINE /* doxygen */ 48 | #endif 49 | 50 | #endif /* _PRS_PAL_INLINE_H */ -------------------------------------------------------------------------------- /include/prs/pal/lib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL dynamic library preprocessor definitions. 27 | */ 28 | 29 | #ifndef _PRS_PAL_LIB 30 | #define _PRS_PAL_LIB 31 | 32 | #include 33 | #include 34 | 35 | #if PRS_PAL_COMPILER == PRS_PAL_COMPILER_GCC 36 | #if PRS_PAL_OS == PRS_PAL_OS_WINDOWS 37 | #define PRS_PAL_LIB_IMPORT __attribute__((dllimport)) 38 | #define PRS_PAL_LIB_EXPORT __attribute__((dllexport)) 39 | #elif PRS_PAL_OS == PRS_PAL_OS_LINUX 40 | /* Builder is responsible for setting -fvisibility=hidden in exported code */ 41 | #define PRS_PAL_LIB_IMPORT 42 | #define PRS_PAL_LIB_EXPORT __attribute__((visibility("default"))) 43 | #endif 44 | #elif PRS_PAL_COMPILER == PRS_PAL_COMPILER_MSVC 45 | #define PRS_PAL_LIB_IMPORT __declspec(dllimport) 46 | #define PRS_PAL_LIB_EXPORT __declspec(dllexport) 47 | #endif 48 | 49 | /** 50 | * \def PRS_PAL_LIB_IMPORT 51 | * \brief 52 | * Defines the symbol import compiler directive. 53 | */ 54 | #if !defined(PRS_PAL_LIB_IMPORT) 55 | #error PRS_PAL_LIB_IMPORT is not defined 56 | #define PRS_PAL_LIB_IMPORT /* doxygen */ 57 | #endif 58 | 59 | /** 60 | * \def PRS_PAL_LIB_EXPORT 61 | * \brief 62 | * Defines the symbol export compiler directive. 63 | */ 64 | #if !defined(PRS_PAL_LIB_EXPORT) 65 | #error PRS_PAL_LIB_EXPORT is not defined 66 | #define PRS_PAL_LIB_EXPORT /* doxygen */ 67 | #endif 68 | 69 | #endif /* _PRS_PAL_LIB */ -------------------------------------------------------------------------------- /include/prs/pal/malloc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL memory allocation declarations. 27 | */ 28 | 29 | #ifndef _PRS_PAL_MALLOC_H 30 | #define _PRS_PAL_MALLOC_H 31 | 32 | #include 33 | #include 34 | 35 | /** 36 | * \brief 37 | * This function is called by PRS to allocate memory from the operating system's heap. 38 | * \param size 39 | * Number of bytes to allocate. 40 | * \return 41 | * A pointer to the allocated memory area. 42 | */ 43 | void* prs_pal_malloc(prs_size_t size); 44 | 45 | /** 46 | * \brief 47 | * This function is called by PRS to allocate zeroed memory from the operating system's heap. 48 | * \param size 49 | * Number of bytes to allocate. 50 | * \return 51 | * A pointer to the allocated memory area. 52 | */ 53 | void* prs_pal_malloc_zero(prs_size_t size); 54 | 55 | /** 56 | * \brief 57 | * This function is called by PRS to free memory to the operating system's heap. 58 | * \param data 59 | * Memory area to free. It has to have been allocated by \ref prs_pal_malloc or \ref prs_pal_malloc_zero firsthand. 60 | */ 61 | void prs_pal_free(void* data); 62 | 63 | #endif /* _PRS_PAL_MALLOC_H */ 64 | -------------------------------------------------------------------------------- /include/prs/pal/os.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL operating system preprocessor definitions and function declarations. 27 | */ 28 | 29 | #ifndef _PRS_PAL_OS_H 30 | #define _PRS_PAL_OS_H 31 | 32 | #include 33 | #include 34 | 35 | /** \brief Linux operating system. */ 36 | #define PRS_PAL_OS_LINUX 0 37 | /** \brief Windows operating system. */ 38 | #define PRS_PAL_OS_WINDOWS 1 39 | 40 | #if PRS_PAL_COMPILER == PRS_PAL_COMPILER_GCC 41 | #if defined(__linux__) 42 | #define PRS_PAL_OS PRS_PAL_OS_LINUX 43 | #elif defined(_WIN32) 44 | #define PRS_PAL_OS PRS_PAL_OS_WINDOWS 45 | #endif 46 | #elif PRS_PAL_COMPILER == PRS_PAL_COMPILER_MSVC 47 | #if defined(_WIN32) 48 | #define PRS_PAL_OS PRS_PAL_OS_WINDOWS 49 | #endif 50 | #endif 51 | 52 | #if PRS_PAL_OS == PRS_PAL_OS_LINUX 53 | #define PRS_PAL_OS_NAME "linux" 54 | #elif PRS_PAL_OS == PRS_PAL_OS_WINDOWS 55 | #define PRS_PAL_OS_NAME "windows" 56 | #endif 57 | 58 | /** 59 | * \def PRS_PAL_OS 60 | * \brief 61 | * Defines the current operating system. 62 | * 63 | * It is one of the following: 64 | * - \ref PRS_PAL_OS_LINUX 65 | * - \ref PRS_PAL_OS_WINDOWS 66 | */ 67 | #if !defined(PRS_PAL_OS) 68 | #error PRS_PAL_OS is not defined 69 | #define PRS_PAL_OS /* doxygen */ 70 | #endif 71 | 72 | /** 73 | * \def PRS_PAL_OS_NAME 74 | * \brief 75 | * Defines the current operating system name in a string literal format. 76 | */ 77 | #if !defined(PRS_PAL_OS_NAME) 78 | #error PRS_PAL_OS_NAME is not defined 79 | #define PRS_PAL_OS_NAME /* doxygen */ 80 | #endif 81 | 82 | /** 83 | * \brief 84 | * Initializes OS facilities used by PRS. 85 | */ 86 | void prs_pal_os_init(void); 87 | 88 | /** 89 | * \brief 90 | * Uninitializes OS facilities used by PRS. 91 | */ 92 | void prs_pal_os_uninit(void); 93 | 94 | /** 95 | * \brief 96 | * Returns the number of CPU cores in the system. 97 | */ 98 | prs_size_t prs_pal_os_get_core_count(void); 99 | 100 | /** 101 | * \brief 102 | * Returns the page size of the system. 103 | */ 104 | prs_size_t prs_pal_os_get_page_size(void); 105 | 106 | /** 107 | * \brief 108 | * Returns the huge page size of the system. 109 | */ 110 | prs_size_t prs_pal_os_get_huge_page_size(void); 111 | 112 | /** 113 | * \brief 114 | * Returns the version of the operating system. 115 | */ 116 | const char* prs_pal_os_get_version(void); 117 | 118 | /** 119 | * \brief 120 | * Returns the system's name. 121 | */ 122 | const char* prs_pal_os_get_computer(void); 123 | 124 | #endif /* _PRS_PAL_OS_H */ 125 | -------------------------------------------------------------------------------- /include/prs/pal/pit.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL programmable interrupt timer declarations. 27 | * 28 | * The programmable interrupt timer (PIT) is an operating system facility that is used to generate periodic function 29 | * calls at precise intervals. 30 | */ 31 | 32 | #ifndef _PRS_PAL_PIT_H 33 | #define _PRS_PAL_PIT_H 34 | 35 | #include 36 | #include 37 | 38 | struct prs_pal_pit; 39 | 40 | /** 41 | * \brief 42 | * This structure provides the PAL PIT's parameters that should be passed to \ref prs_pal_pit_create. 43 | */ 44 | struct prs_pal_pit_create_params { 45 | /** \brief Period, in ticks, at which the call to \p callback should be made. */ 46 | prs_ticks_t period; 47 | 48 | /** 49 | * \brief 50 | * If the clock module should use the current thread to use interrupts. This has a varying effect depending on the 51 | * underlying operating system. 52 | */ 53 | prs_bool_t use_current_thread; 54 | /** \brief Affinity of the clock module. This is only used if \ref use_current_thread is set to \p true. */ 55 | prs_core_mask_t affinity; 56 | /** \brief Priority of the clock module. This is only used if \ref use_current_thread is set to \p true. */ 57 | enum prs_pal_thread_prio prio; 58 | 59 | /** \brief Data that should be passed to \p callback when the period expires. */ 60 | void* userdata; 61 | /** \brief Callback to use when the period expires. */ 62 | void (*callback)(void* userdata); 63 | }; 64 | 65 | /** 66 | * \brief 67 | * Creates and starts a programmable interrupt timer. 68 | */ 69 | struct prs_pal_pit* prs_pal_pit_create(struct prs_pal_pit_create_params* params); 70 | 71 | /** 72 | * \brief 73 | * Stops and destroys a programmable interrupt timer. 74 | */ 75 | void prs_pal_pit_destroy(struct prs_pal_pit* pal_pit); 76 | 77 | #endif /* _PRS_PAL_PIT_H */ 78 | -------------------------------------------------------------------------------- /include/prs/pal/proc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL process declarations. 27 | * 28 | * The PAL process module handles process loading and process management related to the operating system. 29 | */ 30 | 31 | #ifndef _PRS_PAL_PROC_H 32 | #define _PRS_PAL_PROC_H 33 | 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | struct prs_pal_proc; 41 | 42 | /** 43 | * \brief 44 | * This structure provides the PAL process loading parameters that should be passed to \ref prs_pal_proc_load. 45 | */ 46 | struct prs_pal_proc_load_params { 47 | /** \brief Path of the process that should be loaded. */ 48 | char filename[PRS_MAX_PATH]; 49 | }; 50 | 51 | /** 52 | * \brief 53 | * Create the PAL process object for the main PRS process. This does not load an actual process, it only gathers 54 | * information about the current process. 55 | */ 56 | struct prs_pal_proc* prs_pal_proc_create_main(char* filename, char* cmdline); 57 | 58 | /** 59 | * \brief 60 | * Loads the specified process into the current process' virtual address space and load dynamic library dependencies. 61 | * \param params 62 | * Load parameters. 63 | */ 64 | struct prs_pal_proc* prs_pal_proc_load(struct prs_pal_proc_load_params* params); 65 | 66 | /** 67 | * \brief 68 | * Removes the specified process from current process' virtual address space. 69 | * \param pal_proc 70 | * Process to remove. 71 | */ 72 | void prs_pal_proc_destroy(struct prs_pal_proc* pal_proc); 73 | 74 | /** 75 | * \brief 76 | * Returns the specified process' entry point address. 77 | * \param pal_proc 78 | * Process to get the entry point from. 79 | */ 80 | void* prs_pal_proc_get_entry_point(struct prs_pal_proc* pal_proc); 81 | 82 | /** 83 | * \brief 84 | * Returns the specified process' base address in virtual memory. 85 | * \param pal_proc 86 | * Process to get the base address from. 87 | */ 88 | void* prs_pal_proc_get_base(struct prs_pal_proc* pal_proc); 89 | 90 | /** 91 | * \brief 92 | * Returns the specified process' allocated virtual memory size. 93 | * \param pal_proc 94 | * Process to get the size from. 95 | */ 96 | prs_size_t prs_pal_proc_get_size(struct prs_pal_proc* pal_proc); 97 | 98 | #endif /* _PRS_PAL_PROC_H */ 99 | -------------------------------------------------------------------------------- /include/prs/pal/tls.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL thread local storage (TLS) preprocessor definitions. 27 | */ 28 | 29 | #ifndef _PRS_PAL_TLS_H 30 | #define _PRS_PAL_TLS_H 31 | 32 | #include 33 | 34 | #if PRS_PAL_COMPILER == PRS_PAL_COMPILER_GCC 35 | 36 | #if (__STDC_VERSION__ >= 201112L) 37 | #define PRS_TLS _Thread_local 38 | #else /* (__STDC_VERSION__ >= 201112L) */ 39 | #define PRS_TLS __thread 40 | #endif /* (__STDC_VERSION__ >= 201112L) */ 41 | 42 | #endif /* PRS_PAL_COMPILER == PRS_PAL_COMPILER_GCC */ 43 | 44 | /** 45 | * \def PRS_TLS 46 | * \brief 47 | * Defines the thread local storage specifier compiler directive. 48 | */ 49 | #if !defined(PRS_TLS) 50 | #error PRS_TLS is not defined 51 | #define PRS_TLS /* doxygen */ 52 | #endif 53 | 54 | #endif /* _PRS_PAL_TLS_H */ 55 | -------------------------------------------------------------------------------- /include/prs/pal/wls.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PAL worker local storage definitions. 27 | * 28 | * Worker local storage (WLS) is the same as thread local storage, except that is optimized for faster worker pointer 29 | * access. 30 | */ 31 | 32 | #ifndef _PRS_PAL_WLS_H 33 | #define _PRS_PAL_WLS_H 34 | 35 | #include 36 | #include 37 | 38 | struct prs_worker; 39 | 40 | /** 41 | * \brief 42 | * Initializes the worker local storage module. 43 | */ 44 | prs_result_t prs_wls_init(void); 45 | 46 | /** 47 | * \brief 48 | * Uninitializes the worker local storage module. 49 | */ 50 | void prs_wls_uninit(void); 51 | 52 | /** 53 | * \brief 54 | * Initializes the worker local storage module for the specified worker. 55 | */ 56 | prs_result_t prs_wls_worker_init(struct prs_worker* worker); 57 | 58 | /** 59 | * \brief 60 | * Uninitializes the worker local storage module for the specified worker. 61 | */ 62 | void prs_wls_worker_uninit(struct prs_worker* worker); 63 | 64 | /** 65 | * \brief 66 | * Sets the worker local storage value for the current thread. 67 | */ 68 | void prs_wls_set(void* data); 69 | 70 | /** 71 | * \brief 72 | * Gets the worker local storage value for the current thread. 73 | */ 74 | void* prs_wls_get(void); 75 | 76 | #endif /* _PRS_PAL_WLS_H */ 77 | -------------------------------------------------------------------------------- /include/prs/pd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the declarations for a pointer directory. 27 | */ 28 | 29 | #ifndef _PRS_PD_H 30 | #define _PRS_PD_H 31 | 32 | #include 33 | #include 34 | 35 | #define PRS_PD_ID_INVALID ((prs_pd_id_t)-1) 36 | 37 | typedef prs_uint_t prs_pd_id_t; 38 | 39 | struct prs_pd_create_params { 40 | prs_size_t max_entries; 41 | void* area; 42 | }; 43 | 44 | prs_size_t prs_pd_struct_size(struct prs_pd_create_params* params); 45 | 46 | struct prs_pd* prs_pd_create(struct prs_pd_create_params* params); 47 | void prs_pd_destroy(struct prs_pd* pd); 48 | 49 | prs_result_t prs_pd_alloc_and_lock(struct prs_pd* pd, void* ptr, prs_pd_id_t* id); 50 | 51 | void* prs_pd_lock(struct prs_pd* pd, prs_pd_id_t id); 52 | prs_bool_t prs_pd_unlock(struct prs_pd* pd, prs_pd_id_t id); 53 | 54 | #endif /* _PRS_PD_H */ 55 | -------------------------------------------------------------------------------- /include/prs/pool.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the pool declarations. 27 | */ 28 | 29 | #ifndef _PRS_POOL_H 30 | #define _PRS_POOL_H 31 | 32 | #include 33 | #include 34 | 35 | /** 36 | * \brief 37 | * Invalid pool element ID. 38 | */ 39 | #define PRS_POOL_ID_INVALID ((prs_pool_id_t)-1) 40 | 41 | /** \brief Pool element ID type. */ 42 | typedef prs_uint_t prs_pool_id_t; 43 | 44 | /** 45 | * \brief 46 | * Pool creation parameters. 47 | */ 48 | struct prs_pool_create_params { 49 | /** \brief Maximum number of elements in the pool. */ 50 | prs_size_t max_entries; 51 | /** \brief Size of an element. */ 52 | prs_size_t data_size; 53 | /** 54 | * \brief 55 | * Pre-allocated memory area that the pool can use. It must be as large as \ref prs_pool_struct_size returns. 56 | */ 57 | void* area; 58 | }; 59 | 60 | prs_size_t prs_pool_struct_size(struct prs_pool_create_params* params); 61 | 62 | struct prs_pool* prs_pool_create(struct prs_pool_create_params* params); 63 | void prs_pool_destroy(struct prs_pool* pool); 64 | 65 | void* prs_pool_alloc(struct prs_pool* pool, prs_pool_id_t* id); 66 | void prs_pool_free(struct prs_pool* pool, prs_pool_id_t id); 67 | 68 | void prs_pool_lock_first(struct prs_pool* pool, prs_pool_id_t id); 69 | 70 | void* prs_pool_alloc_and_lock(struct prs_pool* pool, void* data, prs_pool_id_t* id); 71 | void* prs_pool_lock(struct prs_pool* pool, prs_pool_id_t id); 72 | 73 | prs_bool_t prs_pool_unlock(struct prs_pool* pool, prs_pool_id_t id); 74 | prs_bool_t prs_pool_try_unlock_final(struct prs_pool* pool, prs_pool_id_t id); 75 | prs_bool_t prs_pool_unlock_dest(struct prs_pool* pool, prs_pool_id_t id, void (*destructor)(void* userdata, void* data), void* userdata); 76 | prs_bool_t prs_pool_try_unlock_final_dest(struct prs_pool* pool, prs_pool_id_t id, void (*destructor)(void* userdata, void* data), void* userdata); 77 | 78 | prs_pool_id_t prs_pool_get_id(struct prs_pool* pool, void* data); 79 | 80 | #endif /* _PRS_POOL_H */ 81 | -------------------------------------------------------------------------------- /include/prs/proc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the process module declarations. 27 | */ 28 | 29 | #ifndef _PRS_PROC_H 30 | #define _PRS_PROC_H 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | struct prs_proc; 39 | 40 | /** 41 | * \brief 42 | * Main entry point arguments. 43 | */ 44 | struct prs_proc_main_params { 45 | /** \brief Number of arguments. */ 46 | int argc; 47 | /** \brief Array of pointers to the arguments. */ 48 | char** argv; 49 | }; 50 | 51 | /** 52 | * \brief 53 | * Process execution parameters. 54 | */ 55 | struct prs_proc_exec_params { 56 | /** \brief Path of the PRS executable to load. */ 57 | char filename[PRS_MAX_PATH]; 58 | /** \brief Full command line (including the executable path). */ 59 | char cmdline[PRS_MAX_CMDLINE]; 60 | /** \brief Main task creation parameters. */ 61 | struct prs_task_create_params main_task_params; 62 | /** \brief Scheduler object ID on which to add the main task. */ 63 | prs_sched_id_t sched_id; 64 | }; 65 | 66 | void prs_proc_init(void); 67 | void prs_proc_uninit(void); 68 | 69 | struct prs_proc* prs_proc_exec(struct prs_proc_exec_params* params); 70 | void prs_proc_destroy(struct prs_proc* proc); 71 | void prs_proc_register_object(struct prs_proc* proc, prs_object_id_t object_id); 72 | prs_result_t prs_proc_unregister_object(struct prs_proc* proc, prs_object_id_t object_id); 73 | 74 | void prs_proc_atexit(struct prs_proc* proc, void (*function)(void)); 75 | 76 | void prs_proc_gc(void); 77 | 78 | int prs_proc_get_argc(void); 79 | const char* prs_proc_get_argv(int arg); 80 | 81 | prs_bool_t prs_proc_is_user_text(void* instr_ptr); 82 | 83 | #endif /* _PRS_PROC_H */ 84 | -------------------------------------------------------------------------------- /include/prs/result.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the common PRS result values. 27 | */ 28 | 29 | #ifndef _PRS_RESULT_H 30 | #define _PRS_RESULT_H 31 | 32 | #include 33 | 34 | /** 35 | * \brief 36 | * Defines common result codes used in PRS. 37 | */ 38 | typedef enum prs_result { 39 | PRS_OK = 0, 40 | PRS_UNKNOWN, 41 | PRS_NOT_IMPLEMENTED, 42 | PRS_OUT_OF_MEMORY, 43 | PRS_PLATFORM_ERROR, 44 | PRS_INVALID_STATE, 45 | PRS_NOT_FOUND, 46 | PRS_ALREADY_EXISTS, 47 | PRS_EMPTY, 48 | PRS_LOCKED, 49 | PRS_TIMEOUT 50 | } prs_result_t; 51 | 52 | #endif /* _PRS_RESULT_H */ 53 | -------------------------------------------------------------------------------- /include/prs/rtc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the run-time check declarations. 27 | */ 28 | 29 | #ifndef _PRS_RTC_H 30 | #define _PRS_RTC_H 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | /** 37 | * \def PRS_RTC_IF 38 | * \brief 39 | * Does a run-time check on the specified condition. 40 | * 41 | * When \ref PRS_RUN_TIME_CHECKING is defined, the condition is verified. In addition, when \ref PRS_ASSERTIONS is 42 | * defined, the condition will be asserted first. 43 | * 44 | * The \ref PRS_RTC_IF macro should be used as a normal \p if C statement as it does not stop PRS nor the currently 45 | * running task. It is up to the user of the macro to define behavior when the check fails. 46 | * \param cond 47 | * Condition to check. 48 | * \note 49 | * It is advised not to use following \p else statements as it could cause unintended side effects if the 50 | * \ref PRS_RTC_IF implementation changes in the future. 51 | */ 52 | #if defined(PRS_RUN_TIME_CHECKING) 53 | #if defined(PRS_ASSERTIONS) 54 | #define PRS_RTC_IF(cond) if ((cond) && (prs_assert("!("#cond")", __FILE__, __LINE__), \ 55 | prs_rtc(#cond, __FILE__, __LINE__), 1)) 56 | #else 57 | #define PRS_RTC_IF(cond) if ((cond) && (prs_rtc(#cond, __FILE__, __LINE__), 1)) 58 | #endif 59 | #else 60 | #if defined(PRS_ASSERTIONS) 61 | #define PRS_RTC_IF(cond) if ((cond) && (prs_assert("!("#cond")", __FILE__, __LINE__), 0)) 62 | #else 63 | #define PRS_RTC_IF(cond) if (0) 64 | #endif 65 | #endif 66 | 67 | void prs_rtc(const char* expr, const char* file, int line); 68 | 69 | #endif /* _PRS_RTC_H */ 70 | -------------------------------------------------------------------------------- /include/prs/sched/swcoop.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the declarations for a single worker cooperative scheduler. 27 | */ 28 | 29 | #ifndef _PRS_SCHED_SWCOOP_H 30 | #define _PRS_SCHED_SWCOOP_H 31 | 32 | #include 33 | 34 | /** 35 | * \brief 36 | * Returns the single worker cooperative scheduler operations. 37 | */ 38 | struct prs_sched_ops* prs_sched_swcoop_ops(void); 39 | 40 | #endif /* _PRS_SCHED_SWCOOP_H */ 41 | -------------------------------------------------------------------------------- /include/prs/sched/swprio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the declarations for a single worker priority scheduler. 27 | */ 28 | 29 | #ifndef _PRS_SCHED_SWPRIO_H 30 | #define _PRS_SCHED_SWPRIO_H 31 | 32 | #include 33 | 34 | /** 35 | * \brief 36 | * Returns the single worker priority scheduler operations. 37 | */ 38 | struct prs_sched_ops* prs_sched_swprio_ops(void); 39 | 40 | #endif /* _PRS_SCHED_SWPRIO_H */ 41 | -------------------------------------------------------------------------------- /include/prs/sem.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains semaphore declarations. 27 | */ 28 | 29 | #ifndef _PRS_SEM_H 30 | #define _PRS_SEM_H 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | struct prs_sem; 38 | 39 | /** 40 | * \brief 41 | * Semaphore creation parameters. 42 | */ 43 | struct prs_sem_create_params { 44 | /** \brief Maximum count of the semaphore. */ 45 | prs_int_t max_count; 46 | /** \brief Initial count of the semaphore. */ 47 | prs_int_t initial_count; 48 | }; 49 | 50 | struct prs_sem* prs_sem_create(struct prs_sem_create_params* params); 51 | void prs_sem_destroy(struct prs_sem* sem); 52 | 53 | void prs_sem_wait(struct prs_sem* sem); 54 | prs_result_t prs_sem_wait_timeout(struct prs_sem* sem, prs_ticks_t timeout); 55 | void prs_sem_signal(struct prs_sem* sem); 56 | 57 | 58 | #endif /* _PRS_SEM_H */ 59 | -------------------------------------------------------------------------------- /include/prs/spinlock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains spinlock definitions. 27 | */ 28 | 29 | #ifndef _PRS_SPINLOCK_H 30 | #define _PRS_SPINLOCK_H 31 | 32 | #include 33 | 34 | struct prs_spinlock; 35 | 36 | struct prs_spinlock* prs_spinlock_create(void); 37 | void prs_spinlock_destroy(struct prs_spinlock* spinlock); 38 | 39 | void prs_spinlock_lock(struct prs_spinlock* spinlock); 40 | prs_bool_t prs_spinlock_try_lock(struct prs_spinlock* spinlock); 41 | 42 | void prs_spinlock_unlock(struct prs_spinlock* spinlock); 43 | 44 | #endif /* _PRS_SPINLOCK_H */ -------------------------------------------------------------------------------- /include/prs/str.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains string manipulation declarations. 27 | */ 28 | 29 | #ifndef _PRS_STR_H 30 | #define _PRS_STR_H 31 | 32 | #include 33 | 34 | int prs_str_copy(char* dst, const char* src, int max); 35 | int prs_str_append(char* dst, const char* src, int offset, int max); 36 | int prs_str_printf(char* dst, int max, const char* fmt, ...); 37 | int prs_str_append_printf(char* dst, int offset, int max, const char* fmt, ...); 38 | int prs_str_vprintf(char* dst, int max, const char* fmt, va_list va); 39 | int prs_str_append_vprintf(char* dst, int offset, int max, const char* fmt, va_list va); 40 | 41 | #endif /* _PRS_STR_H */ 42 | -------------------------------------------------------------------------------- /include/prs/svc/log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the declarations for the log service. 27 | */ 28 | 29 | #ifndef _PRS_SVC_LOG_H 30 | #define _PRS_SVC_LOG_H 31 | 32 | #include 33 | #include 34 | 35 | void prs_svc_log_init(prs_sched_id_t sched_id, prs_task_prio_t prio); 36 | 37 | #endif /* _PRS_SVC_LOG_H */ 38 | -------------------------------------------------------------------------------- /include/prs/svc/proc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the declarations for the process service. 27 | */ 28 | 29 | #ifndef _PRS_SVC_PROC_H 30 | #define _PRS_SVC_PROC_H 31 | 32 | #include 33 | #include 34 | 35 | void prs_svc_proc_init(prs_sched_id_t sched_id, prs_task_prio_t prio); 36 | 37 | #endif /* _PRS_SVC_PROC_H */ 38 | -------------------------------------------------------------------------------- /include/prs/systeminfo.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains system information declarations. 27 | */ 28 | 29 | #ifndef _PRS_SYSTEMINFO_H 30 | #define _PRS_SYSTEMINFO_H 31 | 32 | #include 33 | 34 | /** 35 | * \brief 36 | * This structure contains fields that describe the system. 37 | */ 38 | struct prs_systeminfo { 39 | /** \brief Operating system identifier obtained through \ref PRS_PAL_OS. */ 40 | prs_int_t os_id; 41 | /** \brief Operating system name obtained through \ref PRS_PAL_OS_NAME. */ 42 | const char* os; 43 | /** \brief Operating system version obtained through \ref prs_pal_os_get_version. */ 44 | const char* os_version; 45 | /** \brief Architecture identifier obtained through \ref PRS_PAL_ARCH. */ 46 | prs_int_t arch_id; 47 | /** \brief Architecture name obtained through \ref PRS_PAL_ARCH_NAME. */ 48 | const char* arch; 49 | /** \brief Architecture pointer size obtained through \ref PRS_PAL_POINTER_SIZE. */ 50 | prs_int_t arch_bits; 51 | /** \brief Microarchitecture name. \note Not implemented. */ 52 | const char* march; 53 | /** \brief Compiler identifier obtained through \ref PRS_PAL_COMPILER. */ 54 | prs_int_t compiler_id; 55 | /** \brief Compiler name obtained through \ref PRS_PAL_COMPILER_NAME. */ 56 | const char* compiler; 57 | /** \brief Compiler version obtained through \ref PRS_PAL_COMPILER_VERSION. */ 58 | const char* compiler_version; 59 | /** \brief System name obtained through \ref prs_pal_os_get_computer. */ 60 | const char* computer; 61 | /** \brief Configuration identifier obtained through \ref PRS_PAL_CONFIG. */ 62 | prs_int_t config_id; 63 | /** \brief Configuration name obtained through \ref PRS_PAL_CONFIG_NAME. */ 64 | const char* config; 65 | /** \brief Version number. \note Not implemented. */ 66 | prs_int_t version_number; 67 | /** \brief Version name. */ 68 | const char* version; 69 | /** \brief Number of cores in the system. */ 70 | prs_int_t core_count; 71 | }; 72 | 73 | struct prs_systeminfo* prs_systeminfo_get(void); 74 | 75 | #endif /* _PRS_SYSTEMINFO_H */ 76 | -------------------------------------------------------------------------------- /include/prs/task.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains task declarations. 27 | */ 28 | 29 | #ifndef _PRS_TASK_H 30 | #define _PRS_TASK_H 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | /** \brief Task priority type. */ 39 | typedef PRS_TASK_PRIO_TYPE prs_task_prio_t; 40 | 41 | struct prs_task; 42 | 43 | /** 44 | * \brief 45 | * Task creation parameters. 46 | */ 47 | struct prs_task_create_params { 48 | /** \brief Name of the task which can then be found through \ref prs_task_find. */ 49 | char name[PRS_MAX_TASK_NAME]; 50 | /** \brief Userdata that will be passed as a parameter to the entry point. */ 51 | void* userdata; 52 | /** \brief Initial stack size of the stack. The stack may be extended by an exception handler. */ 53 | prs_size_t stack_size; 54 | /** \brief Priority of the task. May be unused if the scheduler of the task does not support it. */ 55 | prs_task_prio_t prio; 56 | /** \brief Entry point of the task. */ 57 | void (*entry)(void* userdata); 58 | }; 59 | 60 | struct prs_task* prs_task_create(struct prs_task_create_params* params); 61 | void prs_task_destroy(struct prs_task* task); 62 | prs_result_t prs_task_start(struct prs_task* task); 63 | prs_result_t prs_task_stop(struct prs_task* task); 64 | 65 | prs_result_t prs_task_set_userdata(struct prs_task* task, void* userdata); 66 | prs_result_t prs_task_get_userdata(struct prs_task* task, void** userdata); 67 | 68 | prs_task_prio_t prs_task_get_prio(struct prs_task* task); 69 | void prs_task_set_prio(struct prs_task* task, prs_task_prio_t prio); 70 | 71 | prs_task_id_t prs_task_get_id(struct prs_task* task); 72 | struct prs_task* prs_task_current(void); 73 | 74 | prs_object_id_t prs_task_find(const char* name); 75 | struct prs_task* prs_task_find_and_lock(const char* name); 76 | 77 | #endif /* _PRS_TASK_H */ 78 | -------------------------------------------------------------------------------- /include/prs/ticks.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the system tick declarations. 27 | */ 28 | 29 | #ifndef _PRS_TICKS_H 30 | #define _PRS_TICKS_H 31 | 32 | #include 33 | 34 | /** \brief System ticks type. */ 35 | typedef PRS_TICKS_TYPE prs_ticks_t; 36 | 37 | /** \brief Converts seconds to ticks. */ 38 | #define PRS_TICKS_FROM_SECS(secs) ((prs_ticks_t)secs * (PRS_HZ)) 39 | /** \brief Converts milliseconds to ticks. */ 40 | #define PRS_TICKS_FROM_MS(ms) ((prs_ticks_t)ms * (PRS_HZ) / 1000) 41 | /** \brief Converts microseconds to ticks. */ 42 | #define PRS_TICKS_FROM_US(us) ((prs_ticks_t)us * (PRS_HZ) / 1000000) 43 | 44 | /** \brief Converts ticks to seconds. */ 45 | #define PRS_TICKS_TO_SECS(ticks) (ticks / (PRS_HZ)) 46 | /** \brief Converts ticks to milliseconds. */ 47 | #define PRS_TICKS_TO_MS(ticks) (ticks * 1000 / (PRS_HZ)) 48 | /** \brief Converts ticks to microseconds. */ 49 | #define PRS_TICKS_TO_US(ticks) (ticks * 1000000 / (PRS_HZ)) 50 | 51 | #endif /* _PRS_TICKS_H */ 52 | -------------------------------------------------------------------------------- /include/prs/timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the timer module declarations. 27 | */ 28 | 29 | #ifndef _PRS_TIMER_H 30 | #define _PRS_TIMER_H 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | struct prs_timer; 37 | struct prs_timer_entry; 38 | 39 | struct prs_timer* prs_timer_create(void); 40 | void prs_timer_destroy(struct prs_timer* timer); 41 | 42 | struct prs_timer_entry* prs_timer_queue(struct prs_timer* timer, struct prs_event* event, prs_event_type_t event_type, 43 | prs_ticks_t timeout); 44 | prs_ticks_t prs_timer_get_start(struct prs_timer_entry* entry); 45 | void prs_timer_cancel(struct prs_timer* timer, struct prs_timer_entry* entry); 46 | 47 | void prs_timer_tick(struct prs_timer* timer); 48 | 49 | #endif /* _PRS_TIMER_H */ 50 | -------------------------------------------------------------------------------- /include/prs/types.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the basic types declarations. 27 | */ 28 | 29 | #ifndef _PRS_TYPES_H 30 | #define _PRS_TYPES_H 31 | 32 | #include 33 | #include 34 | 35 | /** \brief Converts an expression to a boolean expression. */ 36 | #define PRS_BOOL(expr) ((expr) ? PRS_TRUE : PRS_FALSE) 37 | 38 | /** \brief CPU core mask type. */ 39 | typedef PRS_CPU_TYPE prs_core_mask_t; 40 | 41 | /** \brief PRS object ID type. */ 42 | typedef prs_uint32_t prs_object_id_t; 43 | 44 | #endif /* _PRS_TYPES_H */ 45 | -------------------------------------------------------------------------------- /include/prs/version.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PRS version preprocessor directives. 27 | */ 28 | 29 | #ifndef _PRS_VERSION_H 30 | #define _PRS_VERSION_H 31 | 32 | /** \brief PRS version number. */ 33 | #define PRS_VERSION_VER 1 34 | 35 | /** \brief PRS major revision number. */ 36 | #define PRS_VERSION_MAJ 0 37 | 38 | /** \brief PRS minor revision number. */ 39 | #define PRS_VERSION_MIN 0 40 | 41 | /** \brief PRS version in a string literal format. */ 42 | #define PRS_VERSION_NAME "1.0.0" 43 | 44 | #endif /* _PRS_VERSION_H */ 45 | -------------------------------------------------------------------------------- /include/prs/worker.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the worker declarations. 27 | */ 28 | 29 | #ifndef _PRS_WORKER_H 30 | #define _PRS_WORKER_H 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | struct prs_worker; 37 | 38 | /** 39 | * \brief 40 | * Worker implementation operations. These operations must be implemented by the user of the worker. 41 | */ 42 | struct prs_worker_ops { 43 | /** 44 | * \brief 45 | * This function is called by the worker to get the next task to execute. 46 | * 47 | * This function has the same characteristics as \ref prs_sched_ops::get_next. 48 | * \see 49 | * \ref prs_sched_ops::get_next 50 | */ 51 | prs_bool_t (*get_next)(void* userdata, struct prs_task* current_task, 52 | struct prs_task** next_task); 53 | }; 54 | 55 | /** 56 | * \brief 57 | * Worker creation parameters. 58 | */ 59 | struct prs_worker_create_params { 60 | /** \brief PAL thread to use for this worker. */ 61 | struct prs_pal_thread* pal_thread; 62 | /** \brief Userdata passed to the functions of the worker implementation (\ref prs_worker_ops). */ 63 | void* userdata; 64 | /** \brief Worker implementation operations. */ 65 | struct prs_worker_ops ops; 66 | }; 67 | 68 | prs_result_t prs_worker_create(struct prs_worker_create_params* params, prs_worker_id_t* id); 69 | prs_result_t prs_worker_destroy(struct prs_worker* worker); 70 | prs_result_t prs_worker_start(struct prs_worker* worker); 71 | prs_result_t prs_worker_stop(struct prs_worker* worker); 72 | prs_result_t prs_worker_join(struct prs_worker* worker); 73 | prs_result_t prs_worker_interrupt(struct prs_worker* worker); 74 | prs_result_t prs_worker_signal(struct prs_worker* worker); 75 | 76 | prs_bool_t prs_worker_int_disable(struct prs_worker* worker); 77 | void prs_worker_int_enable(struct prs_worker* worker); 78 | prs_bool_t prs_worker_int_enabled(struct prs_worker* worker); 79 | 80 | void prs_worker_schedule(struct prs_worker* worker); 81 | 82 | struct prs_worker* prs_worker_current(void); 83 | struct prs_task* prs_worker_get_current_task(struct prs_worker* worker); 84 | prs_task_id_t prs_worker_get_current_task_id(struct prs_worker* worker); 85 | void* prs_worker_get_userdata(struct prs_worker* worker); 86 | 87 | void prs_worker_restore_context(struct prs_worker* worker, struct prs_pal_context* context); 88 | 89 | #endif /* _PRS_WORKER_H */ 90 | -------------------------------------------------------------------------------- /make.bat: -------------------------------------------------------------------------------- 1 | @REM This is a batch file to use "make" from the Windows command line. It will 2 | @REM search for "mingw32-make.exe" using %PATH% first, then try to use 3 | @REM %MINGW_HOME%, %MINGWDIR% and "C:\MinGW" to locate the MinGW directory. 4 | 5 | @SET _MINGW_MAKE=mingw32-make.exe 6 | @WHERE /Q %_MINGW_MAKE% 7 | @IF %ERRORLEVEL% EQU 0 GOTO GO 8 | 9 | @SET _MINGW_MAKE=%MINGW_HOME%\bin\%_MINGW_MAKE% 10 | @IF EXIST %_MINGW_MAKE% GOTO GO 11 | 12 | @SET _MINGW_MAKE=%MINGWDIR%\bin\%_MINGW_MAKE% 13 | @IF EXIST %_MINGW_MAKE% GOTO GO 14 | 15 | @SET _MINGW_MAKE=C:\MinGW\bin\%_MINGW_MAKE% 16 | @IF EXIST %_MINGW_MAKE% GOTO GO 17 | 18 | @REM You may add other custom path substitutions here. 19 | 20 | @ECHO Couldn't locate mingw32-make.exe. Make sure one of the following 21 | @ECHO conditions is true so that the executable is found: 22 | @ECHO - the MinGW path is in %%PATH%%; 23 | @ECHO - the MinGW path is in %%MINGW_HOME%%; 24 | @ECHO - the MinGW path is in %%MINGWDIR%%; 25 | @ECHO - the MinGW path is C:\MinGW. 26 | @GOTO END 27 | 28 | :GO 29 | 30 | @REM The following properly calls the "make" application with the arguments 31 | @REM provided. Otherwise, parameters containing the equal sign ("=") would get 32 | @REM splitted. 33 | @FOR /F "tokens=1*" %%x IN ("skip %*") DO @%_MINGW_MAKE% %%y 34 | 35 | :END 36 | 37 | @EXIT /B %ERRORLEVEL% -------------------------------------------------------------------------------- /make/makefile.debug: -------------------------------------------------------------------------------- 1 | # Portable Runtime System (PRS) 2 | # Copyright (C) 2016 Alexandre Tremblay 3 | # 4 | # This file is part of PRS. 5 | # 6 | # PRS is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Affero General Public License as 8 | # published by the Free Software Foundation, either version 3 of the 9 | # License, or (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Affero General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Affero General Public License 17 | # along with this program. If not, see . 18 | # 19 | # portableruntimesystem@gmail.com 20 | 21 | CONFIG := debug -------------------------------------------------------------------------------- /make/makefile.gcc: -------------------------------------------------------------------------------- 1 | # Portable Runtime System (PRS) 2 | # Copyright (C) 2016 Alexandre Tremblay 3 | # 4 | # This file is part of PRS. 5 | # 6 | # PRS is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Affero General Public License as 8 | # published by the Free Software Foundation, either version 3 of the 9 | # License, or (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Affero General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Affero General Public License 17 | # along with this program. If not, see . 18 | # 19 | # portableruntimesystem@gmail.com 20 | 21 | COMPILER := gcc 22 | 23 | # Test for Windows use (MinGW) 24 | ifeq ($(OS), windows) 25 | ifdef MINGW_HOME 26 | GCCDIR := $(MINGW_HOME)/bin/ 27 | else 28 | ifdef MINGWDIR 29 | GCCDIR := $(MINGWDIR)/bin/ 30 | else 31 | ifneq (,$(wildcard C:/MinGW)) 32 | GCCDIR := C:/MinGW/bin/ 33 | endif 34 | endif 35 | endif 36 | else 37 | GCCDIR := 38 | _CFLAGS += -mno-red-zone # Disable red zone on Linux, as it prevents the signal handler from adding a stack frame 39 | endif 40 | 41 | # Define some tools 42 | CC := $(call native_path,$(GCCDIR)gcc$(EXE_EXT)) 43 | CPP := $(call native_path,$(GCCDIR)g++$(EXE_EXT)) 44 | AS := $(call native_path,$(GCCDIR)as$(EXE_EXT)) 45 | LD := $(call native_path,$(GCCDIR)g++$(EXE_EXT)) 46 | 47 | # Set the platform according to GCC's dumpmachine 48 | PLATFORM := $(shell $(CC) -dumpmachine) 49 | 50 | # Add some GCC flags according to the chosen configuration 51 | ifeq ($(CONFIG), debug) 52 | DEFINES += DEBUG=1 53 | _CFLAGS += -g 54 | else 55 | _CFLAGS += -O3 56 | ifdef SYMBOLS 57 | _CFLAGS += -g 58 | endif 59 | endif 60 | 61 | ifdef GENERATE_ASM 62 | _CFLAGS += -S 63 | endif 64 | 65 | # Add prefixes to well-known variables 66 | _INCLUDEDIRS = $(addprefix -I,$(INCLUDEDIRS)) 67 | _DEFINES = $(addprefix -D,$(DEFINES)) 68 | 69 | # Define flags 70 | CCFLAGS += $(_CFLAGS) $(CFLAGS) $(_DEFINES) $(_INCLUDEDIRS) 71 | CPPFLAGS += $(_CFLAGS) $(CFLAGS) $(_DEFINES) $(_INCLUDEDIRS) 72 | 73 | # Define library flags 74 | _LIBS = $(addprefix -l,$(LIBS)) 75 | _LIBDIRS = $(addprefix -L,$(LIBDIRS)) 76 | LDFLAGS += $(_LIBDIRS) $(_LIBS) 77 | 78 | # Define shared library flags 79 | SLFLAGS += -shared 80 | 81 | # Define macros for compiling objects 82 | cc = $(V)$(CC) $(CCFLAGS) -c -o $(call native_path,$(2)) $(call native_path,$(1)) 83 | cpp = $(V)$(CPP) $(CPPFLAGS) -c -o $(call native_path,$(2)) $(call native_path,$(1)) 84 | as = $(V)$(AS) $(ASFLAGS) -o $(call native_path,$(2)) $(call native_path,$(1)) 85 | ld = $(V)$(LD) -o $(call native_path,$(2)) $(call native_path,$(1)) $(LDFLAGS) 86 | objcopy = $(V)$(OBJCOPY) $(call native_path,$(1)) $(call native_path,$(2)) 87 | -------------------------------------------------------------------------------- /make/makefile.linux: -------------------------------------------------------------------------------- 1 | # Portable Runtime System (PRS) 2 | # Copyright (C) 2016 Alexandre Tremblay 3 | # 4 | # This file is part of PRS. 5 | # 6 | # PRS is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Affero General Public License as 8 | # published by the Free Software Foundation, either version 3 of the 9 | # License, or (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Affero General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Affero General Public License 17 | # along with this program. If not, see . 18 | # 19 | # portableruntimesystem@gmail.com 20 | 21 | CMDSEP := ; 22 | EXE_EXT := 23 | SL_PRE := lib 24 | SL_EXT := .so 25 | ECHO := @echo "***" 26 | V := $(if $(VERBOSE),,@) 27 | CP := cp 28 | MKDIR := mkdir -p 29 | RMDIR := rm -rf 30 | -------------------------------------------------------------------------------- /make/makefile.prapp: -------------------------------------------------------------------------------- 1 | # Portable Runtime System (PRS) 2 | # Copyright (C) 2016 Alexandre Tremblay 3 | # 4 | # This file is part of PRS. 5 | # 6 | # PRS is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Affero General Public License as 8 | # published by the Free Software Foundation, either version 3 of the 9 | # License, or (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Affero General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Affero General Public License 17 | # along with this program. If not, see . 18 | # 19 | # portableruntimesystem@gmail.com 20 | 21 | # Abolute PRS path 22 | PRSDIR = $(realpath $(MAKEDIR)/../prs) 23 | 24 | # PRS uses its own C runtime code 25 | SOURCES += $(PRSDIR)/crt/crt0.c 26 | 27 | # Use the latest C version 28 | ifdef C99 29 | CFLAGS += --std=c99 30 | else 31 | CFLAGS += --std=c11 32 | endif 33 | 34 | # Enable all warnings 35 | CFLAGS += -Wall 36 | 37 | # Note: -ffreestanding prevents printf from working with floating point 38 | LDFLAGS += -nostartfiles -Wl,-e_pr_entrypoint 39 | 40 | # Define PR_APP to import pr symbols 41 | DEFINES += PR_APP 42 | 43 | ifeq ($(OS), windows) 44 | # Windows relocation 45 | LDFLAGS += -Wl,--dynamicbase,--export-all-symbols 46 | else # windows 47 | # Position independent code for Linux 48 | CFLAGS += -fPIC 49 | endif # windows 50 | 51 | # Import symbols from the prs executable 52 | PRS_BINDIR = $(PRSDIR)/$(BINDIR) 53 | LIBDIRS += $(PRS_BINDIR) 54 | LIBS += prs 55 | -------------------------------------------------------------------------------- /make/makefile.release: -------------------------------------------------------------------------------- 1 | # Portable Runtime System (PRS) 2 | # Copyright (C) 2016 Alexandre Tremblay 3 | # 4 | # This file is part of PRS. 5 | # 6 | # PRS is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Affero General Public License as 8 | # published by the Free Software Foundation, either version 3 of the 9 | # License, or (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Affero General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Affero General Public License 17 | # along with this program. If not, see . 18 | # 19 | # portableruntimesystem@gmail.com 20 | 21 | CONFIG := release -------------------------------------------------------------------------------- /make/makefile.rules: -------------------------------------------------------------------------------- 1 | # Portable Runtime System (PRS) 2 | # Copyright (C) 2016 Alexandre Tremblay 3 | # 4 | # This file is part of PRS. 5 | # 6 | # PRS is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Affero General Public License as 8 | # published by the Free Software Foundation, either version 3 of the 9 | # License, or (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Affero General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Affero General Public License 17 | # along with this program. If not, see . 18 | # 19 | # portableruntimesystem@gmail.com 20 | 21 | 22 | .PHONY: all clean run env 23 | 24 | # Define the target executable's complete path 25 | EXETARGET := $(BINDIR)/$(TARGET)$(EXE_EXT) 26 | 27 | # Add the target name into the defines 28 | DEFINES += MAKE_TARGET=\"$(TARGET)\" 29 | 30 | # Simple rules to create the obj and bin directories 31 | $(OBJDIR): 32 | $(E)$(V)$(MKDIR) $(call native_path,$@) 33 | $(BINDIR): 34 | $(E)$(V)$(MKDIR) $(call native_path,$@) 35 | 36 | # Collect all the source files directories so that we can generate rules for 37 | # each of them 38 | uniq = $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1))) 39 | SOURCEDIRS = $(call uniq,$(dir $(SOURCES))) 40 | 41 | __OBJS = $(patsubst %.c,%.o,$(SOURCES)) 42 | _OBJS = $(call native_underpath,$(__OBJS)) 43 | OBJS += $(addprefix $(OBJDIR)/,$(subst /,_,$(_OBJS))) 44 | 45 | define define_cc_rule 46 | $(OBJDIR)/$(2)%.o: $(1)%.c 47 | $$(ECHO) Compiling C $$<... 48 | $$(call cc, $$<, $$@) 49 | endef 50 | 51 | $(eval $(call define_cc_rule,,)) 52 | $(foreach sourcedir,$(SOURCEDIRS),$(eval $(call define_cc_rule,$(sourcedir),$(call native_underpath,$(subst /,_,$(sourcedir)))))) 53 | 54 | # Executable linking 55 | $(EXETARGET): $(OBJS) 56 | $(ECHO) Linking $@... 57 | $(call ld,$^,$@) 58 | 59 | # Environment display 60 | VARIABLES = \ 61 | AS \ 62 | BINDIR \ 63 | CC \ 64 | CCFLAGS \ 65 | CPP \ 66 | CPPFLAGS \ 67 | COMPILER \ 68 | CONFIG \ 69 | DEBUG \ 70 | EXETARGET \ 71 | EXE_EXT \ 72 | GCCDIR \ 73 | LD \ 74 | LDFLAGS \ 75 | LIBDIRS \ 76 | LIBS \ 77 | MAKEDIR \ 78 | MAKEFILE_LIST \ 79 | MAKEFILE.TOP \ 80 | OBJDIR \ 81 | OBJS \ 82 | PLATFORM \ 83 | SOURCEDIRS \ 84 | SOURCES \ 85 | V \ 86 | VERBOSE 87 | 88 | env: 89 | $(foreach var,$(VARIABLES),$(ECHO) $(var)=$($(var))$(CMDSEP)) 90 | 91 | run: 92 | $(ECHO) Running $(EXETARGET)... 93 | $(V)$(EXETARGET) 94 | 95 | all: $(OBJDIR) 96 | all: $(BINDIR) 97 | all: $(EXETARGET) 98 | 99 | clean: 100 | $(ECHO) Cleaning $(call native_path,$(BINDIR)) 101 | $(E)$(V)$(RMDIR) $(call native_path,$(BINDIR)) 102 | $(ECHO) Cleaning $(call native_path,$(OBJDIR)) 103 | $(E)$(V)$(RMDIR) $(call native_path,$(OBJDIR)) 104 | -------------------------------------------------------------------------------- /make/makefile.test: -------------------------------------------------------------------------------- 1 | # Portable Runtime System (PRS) 2 | # Copyright (C) 2016 Alexandre Tremblay 3 | # 4 | # This file is part of PRS. 5 | # 6 | # PRS is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Affero General Public License as 8 | # published by the Free Software Foundation, either version 3 of the 9 | # License, or (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Affero General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Affero General Public License 17 | # along with this program. If not, see . 18 | # 19 | # portableruntimesystem@gmail.com 20 | 21 | test: all 22 | $(ECHO) Running test $(TARGET)... 23 | $(V)$(PRS_BINDIR)/prs$(EXE_EXT) $(EXETARGET) 24 | -------------------------------------------------------------------------------- /make/makefile.top: -------------------------------------------------------------------------------- 1 | # Portable Runtime System (PRS) 2 | # Copyright (C) 2016 Alexandre Tremblay 3 | # 4 | # This file is part of PRS. 5 | # 6 | # PRS is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Affero General Public License as 8 | # published by the Free Software Foundation, either version 3 of the 9 | # License, or (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Affero General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Affero General Public License 17 | # along with this program. If not, see . 18 | # 19 | # portableruntimesystem@gmail.com 20 | 21 | # Get the path of the "make" directory 22 | MAKEFILE.TOP := $(realpath $(lastword $(MAKEFILE_LIST))) 23 | MAKEDIR := $(patsubst %/,%,$(dir $(MAKEFILE.TOP))) 24 | 25 | # Get the build configuration 26 | ifdef DEBUG 27 | include $(MAKEDIR)/makefile.debug 28 | else # DEBUG 29 | include $(MAKEDIR)/makefile.release 30 | endif # DEBUG 31 | 32 | # Define a function to handle path conversions on Windows 33 | # It will have no effect on other platforms 34 | native_path = $1 35 | native_underpath = $1 36 | 37 | # Makefile symbol to put at the beginning of commands to ignore errors 38 | E := - 39 | 40 | # Auto-detect the operating system 41 | ifdef ComSpec 42 | OS := windows 43 | else # ComSpec 44 | UNAME := $(shell uname) 45 | ifeq ($(UNAME), Linux) 46 | OS := linux 47 | else # Linux 48 | ifeq ($(UNAME), FreeBSD) 49 | OS := freebsd 50 | else # FreeBSD 51 | $(error Unknown OS: $(UNAME)) 52 | endif # FreeBSD 53 | endif # Linux 54 | endif # ComSpec 55 | include $(MAKEDIR)/makefile.$(OS) 56 | 57 | # Define some directories 58 | OBJDIR = obj/$(CONFIG)/$(PLATFORM) 59 | BINDIR = bin/$(CONFIG)/$(PLATFORM) 60 | 61 | # We only support GCC for now 62 | include $(MAKEDIR)/makefile.gcc 63 | -------------------------------------------------------------------------------- /make/makefile.windows: -------------------------------------------------------------------------------- 1 | # Portable Runtime System (PRS) 2 | # Copyright (C) 2016 Alexandre Tremblay 3 | # 4 | # This file is part of PRS. 5 | # 6 | # PRS is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Affero General Public License as 8 | # published by the Free Software Foundation, either version 3 of the 9 | # License, or (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Affero General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Affero General Public License 17 | # along with this program. If not, see . 18 | # 19 | # portableruntimesystem@gmail.com 20 | 21 | CMDSEP := & 22 | EXE_EXT := .exe 23 | SL_PRE := 24 | SL_EXT := .dll 25 | ECHO := @ECHO *** 26 | V := $(if $(VERBOSE),,@) 27 | CP := COPY 28 | MKDIR := MKDIR 29 | RMDIR := RMDIR /S /Q 30 | 31 | # Convert a Unix'ed path with slashes to a Windows path with backslashes as 32 | # some command line tools do not support slashes at all 33 | native_path = $(subst /,\,$1) 34 | 35 | # Windows paths do not support semicolons 36 | native_underpath = $(subst :,_,$1) 37 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | # Portable Runtime System (PRS) 2 | # Copyright (C) 2016 Alexandre Tremblay 3 | # 4 | # This file is part of PRS. 5 | # 6 | # PRS is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU Affero General Public License as 8 | # published by the Free Software Foundation, either version 3 of the 9 | # License, or (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU Affero General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Affero General Public License 17 | # along with this program. If not, see . 18 | # 19 | # portableruntimesystem@gmail.com 20 | 21 | # Include the top makefile which will define the build characteristics 22 | MAKEFILE.TOP := $(CURDIR)/make/makefile.top 23 | include $(MAKEFILE.TOP) 24 | 25 | TARGETS = prs 26 | ifneq ($(wildcard test/*),) 27 | TARGETS += test 28 | endif 29 | ifneq ($(wildcard examples/*),) 30 | TARGETS += examples 31 | endif 32 | 33 | # Define targets for 'all' 34 | ALL_TARGETS := $(addsuffix .all,$(TARGETS)) 35 | 36 | # Define targets for 'clean' 37 | CLEAN_TARGETS := $(addsuffix .clean,$(TARGETS)) 38 | 39 | # Define phony targets that are not file-related 40 | .PHONY: $(ALL_TARGETS) $(CLEAN_TARGETS) test 41 | 42 | # Redirect targets to sub-directories 43 | all: $(ALL_TARGETS) 44 | clean: $(CLEAN_TARGETS) 45 | test: 46 | ifneq ($(wildcard test/*),) 47 | $(V)$(MAKE) -C test test 48 | endif 49 | ifneq ($(wildcard examples/*),) 50 | $(V)$(MAKE) -C examples test 51 | endif 52 | 53 | $(ALL_TARGETS): 54 | $(V)$(MAKE) -C $(basename $@) all 55 | 56 | $(CLEAN_TARGETS): 57 | $(V)$(MAKE) -C $(basename $@) clean 58 | -------------------------------------------------------------------------------- /prs/assert.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the assertion definitions. 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include "task.h" 36 | 37 | /** 38 | * \brief 39 | * Raises an assertion failure. In addition to printing an error message to the log, this will terminate PRS. 40 | * \param expr 41 | * Expression that is printed into the log. 42 | * \param file 43 | * File where the assertion occurred. 44 | * \param line 45 | * Line in \p file where the assertion occurred. 46 | * \note 47 | * This should never be called directly by code. Use \ref PRS_ASSERT, \ref PRS_PRECONDITION or \ref PRS_POSTCONDITION to 48 | * raise an assertion. 49 | */ 50 | void prs_assert(const char* expr, const char* file, int line) 51 | { 52 | #if defined(PRS_CLEAN) 53 | prs_log_print("Assertion failed: %s (%s:%d)", expr, file, line); 54 | struct prs_task* task = prs_task_current(); 55 | if (task) { 56 | prs_log_print(" in task %s (%u)", task->name, task->id); 57 | } 58 | prs_exit_from_excp(-1); 59 | #endif /* PRS_CLEAN */ 60 | prs_pal_assert(expr, file, line); 61 | } 62 | -------------------------------------------------------------------------------- /prs/crt/crt0.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the PRS CRT definitions. 27 | * 28 | * When an executable is loaded by PRS, it will spawn a task that will call the executable's entry point. The default 29 | * entry point for PRS executables is \ref PR_ENTRYPOINT. The entry point runs C constructors and registers its 30 | * destructors using \ref pr_atexit. The \ref pr_main call is then made so that the user code can run. If the user 31 | * code did not already call \ref pr_exit to exit the process, it is called automatically when \ref pr_main returns. 32 | * 33 | * The entry point defined in this file is intentionally different from the usual one, as it would call native CRT 34 | * initializers which were already run. 35 | */ 36 | 37 | #include 38 | #include 39 | #include 40 | 41 | /** 42 | * \brief 43 | * Entry point of a dynamically loaded PRS executable. 44 | * \param userdata 45 | * Process parameters. 46 | */ 47 | void PR_ENTRYPOINT(void* userdata) 48 | { 49 | struct prs_proc_main_params* params = userdata; 50 | 51 | _prs_pal_crt_ctors(); 52 | pr_atexit(_prs_pal_crt_dtors); 53 | 54 | extern int pr_main(int argc, char* argv[]); 55 | const int ret = pr_main(params->argc, params->argv); 56 | 57 | pr_exit(ret); 58 | } 59 | -------------------------------------------------------------------------------- /prs/error.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the error module definitions. 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | static enum prs_excp_result prs_error_type_to_excp_behavior(enum prs_error_type error) 36 | { 37 | switch (error) { 38 | case PRS_ERROR_TYPE_CONTINUE: 39 | return PRS_EXCP_RESULT_CONTINUE; 40 | case PRS_ERROR_TYPE_KILL_TASK: 41 | return PRS_EXCP_RESULT_KILL_TASK; 42 | case PRS_ERROR_TYPE_FATAL: 43 | return PRS_EXCP_RESULT_EXIT; 44 | default: 45 | return PRS_EXCP_RESULT_EXIT; 46 | } 47 | } 48 | 49 | /** 50 | * \brief 51 | * Reports an error and executes the action provided by the \p error parameter. 52 | * \param error 53 | * Behavior of the error. 54 | * \param expr 55 | * Expression that is printed into the log. 56 | * \param file 57 | * File where the error occurred. 58 | * \param line 59 | * Line in \p file where the error occurred. 60 | * \note 61 | * This should never be called directly by code. Use \ref PRS_ERROR, \ref PRS_ERROR_IF, \ref PRS_ERROR_WHEN, 62 | * \ref PRS_KILL_TASK, \ref PRS_KILL_TASK_WHEN, \ref PRS_FATAL or \ref PRS_FATAL_WHEN to raise an error. 63 | */ 64 | void prs_error(enum prs_error_type error, const char* expr, const char* file, int line) 65 | { 66 | struct prs_worker* worker = prs_worker_current(); 67 | struct prs_excp_raise_info info = { 68 | .expr = expr, 69 | .file = file, 70 | .line = line, 71 | .behavior = prs_error_type_to_excp_behavior(error) 72 | }; 73 | prs_excp_raise(PRS_EXCP_TYPE_PRS, &info, worker, 0); 74 | } 75 | -------------------------------------------------------------------------------- /prs/gpd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the global pointer directory definitions. 27 | * 28 | * There can only be one global pointer directory (GPD) at a time. It is used to dereference pointers simultaneously 29 | * across multiple workers using a unique ID. It can be accessed through the \ref prs_pd implementation and is 30 | * obtained through \ref prs_gpd_get. 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | 37 | static struct prs_pd* s_prs_gpd = 0; 38 | 39 | /** 40 | * \brief 41 | * Initializes the global pointer directory. 42 | */ 43 | void prs_gpd_init(void) 44 | { 45 | struct prs_pd_create_params params = { 46 | .max_entries = PRS_MAX_GPD_ENTRIES, 47 | .area = 0 48 | }; 49 | s_prs_gpd = prs_pd_create(¶ms); 50 | PRS_FATAL_WHEN(!s_prs_gpd); 51 | } 52 | 53 | /** 54 | * \brief 55 | * Uninitializes the global pointer directory. 56 | */ 57 | void prs_gpd_uninit(void) 58 | { 59 | prs_pd_destroy(s_prs_gpd); 60 | } 61 | 62 | /** 63 | * \brief 64 | * Returns a pointer to the global pointer directory. 65 | */ 66 | struct prs_pd* prs_gpd_get(void) 67 | { 68 | return s_prs_gpd; 69 | } 70 | -------------------------------------------------------------------------------- /prs/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the main PRS application definitions. 27 | * 28 | * This is the PRS application (main) entry point. If PRS is to be used as a library within another application, this 29 | * file shall be excluded from the application and \ref prs_init shall be called from somewhere else. 30 | */ 31 | 32 | #include 33 | 34 | int main(int argc, char* argv[]) 35 | { 36 | struct prs_init_params init_params = { 37 | .use_current_thread = PRS_TRUE, 38 | .core_mask = 0xFFFFFFFF 39 | }; 40 | prs_init(&init_params); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /prs/pal/linux/os.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the Linux operating system definitions. 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #include 35 | #include 36 | #include 37 | 38 | #include "../posix/signal.h" 39 | 40 | static struct utsname s_prs_pal_linux_uname; 41 | static prs_size_t s_prs_pal_linux_core_count; 42 | static prs_size_t s_prs_pal_linux_page_size; 43 | static prs_size_t s_prs_pal_linux_huge_page_size; 44 | 45 | static void prs_pal_os_user_interrupt(int signo, siginfo_t* sinfo, void* ucontext) 46 | { 47 | prs_excp_raise(PRS_EXCP_TYPE_USER_INTERRUPT, 0, 0, (struct prs_pal_context*)ucontext); 48 | } 49 | 50 | void prs_pal_os_init(void) 51 | { 52 | const int error = uname(&s_prs_pal_linux_uname); 53 | if (error) { 54 | fprintf(stderr, "uname() failed\n"); 55 | exit(-1); 56 | } 57 | 58 | s_prs_pal_linux_core_count = sysconf(_SC_NPROCESSORS_ONLN); 59 | s_prs_pal_linux_page_size = sysconf(_SC_PAGESIZE); 60 | s_prs_pal_linux_huge_page_size = 0; 61 | 62 | prs_result_t result = prs_pal_signal_action(SIGINT, prs_pal_os_user_interrupt, 0); 63 | if (result != PRS_OK) { 64 | fprintf(stderr, "Couldn't register SIGINT\n"); 65 | exit(-1); 66 | } 67 | 68 | result = prs_pal_signal_unblock(SIGINT); 69 | if (result != PRS_OK) { 70 | fprintf(stderr, "Couldn't unblock SIGINT\n"); 71 | exit(-1); 72 | } 73 | } 74 | 75 | void prs_pal_os_uninit(void) 76 | { 77 | 78 | } 79 | 80 | prs_size_t prs_pal_os_get_core_count(void) 81 | { 82 | return s_prs_pal_linux_core_count; 83 | } 84 | 85 | prs_size_t prs_pal_os_get_page_size(void) 86 | { 87 | return s_prs_pal_linux_page_size; 88 | } 89 | 90 | prs_size_t prs_pal_os_get_huge_page_size(void) 91 | { 92 | return s_prs_pal_linux_huge_page_size; 93 | } 94 | 95 | const char* prs_pal_os_get_version(void) 96 | { 97 | return s_prs_pal_linux_uname.release; 98 | } 99 | 100 | const char* prs_pal_os_get_computer(void) 101 | { 102 | return s_prs_pal_linux_uname.nodename; 103 | } 104 | -------------------------------------------------------------------------------- /prs/pal/posix/assert.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the POSIX assertion definitions. 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | 34 | void prs_pal_assert(const char* expr, const char* file, int line) 35 | { 36 | fprintf(stderr, "Assertion failed: %s in %s, line %d.\nAborting.\n", expr, file, line); 37 | fflush(stderr); 38 | abort(); 39 | } 40 | -------------------------------------------------------------------------------- /prs/pal/posix/excp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the POSIX exception definitions. 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | #include "signal.h" 41 | #include "../../task.h" 42 | 43 | static const int prs_pal_excp_handled_signals[] = { 44 | SIGFPE, 45 | SIGILL, 46 | SIGSEGV, 47 | SIGBUS, 48 | SIGSYS 49 | }; 50 | static const int prs_pal_excp_handled_signal_count = 51 | sizeof(prs_pal_excp_handled_signals) / sizeof(prs_pal_excp_handled_signals[0]); 52 | 53 | static PRS_ATOMIC prs_bool_t s_prs_pal_excp_sigaction_done = PRS_FALSE; 54 | static struct sigaction s_prs_pal_excp_prev_sigaction; 55 | 56 | static void prs_pal_excp_sighandler(int signo, siginfo_t* sinfo, void* ucontext) 57 | { 58 | PRS_PRECONDITION(ucontext); 59 | 60 | struct prs_worker* worker = prs_worker_current(); 61 | if (!worker) { 62 | /* No worker for this thread. Do not process this exception here. */ 63 | prs_pal_signal_chain(signo, sinfo, ucontext, &s_prs_pal_excp_prev_sigaction); 64 | return; 65 | } 66 | 67 | void* extra = sinfo->si_addr; 68 | 69 | PRS_FTRACE("#%d, extra=%p", signo, extra); 70 | 71 | enum prs_excp_type type = PRS_EXCP_TYPE_UNKNOWN; 72 | switch (signo) { 73 | case SIGFPE: 74 | /* Could also be an integer error */ 75 | type = PRS_EXCP_TYPE_FLOATING_POINT; 76 | break; 77 | case SIGBUS: 78 | type = PRS_EXCP_TYPE_BUS; 79 | break; 80 | case SIGILL: 81 | type = PRS_EXCP_TYPE_ILLEGAL_INSTRUCTION; 82 | break; 83 | case SIGSEGV: { 84 | struct prs_task* task = prs_worker_get_current_task(worker); 85 | if (task && prs_stack_address_in_range(task->stack, extra)) { 86 | type = PRS_EXCP_TYPE_STACK_OVERFLOW; 87 | } else { 88 | type = PRS_EXCP_TYPE_SEGMENTATION_FAULT; 89 | } 90 | break; 91 | } 92 | case SIGSYS: 93 | type = PRS_EXCP_TYPE_OS; 94 | break; 95 | } 96 | 97 | prs_excp_raise(type, extra, worker, (struct prs_pal_context*)ucontext); 98 | } 99 | 100 | prs_result_t prs_pal_excp_init_worker(struct prs_worker* worker) 101 | { 102 | prs_result_t result = PRS_OK; 103 | 104 | if (!prs_pal_atomic_exchange(&s_prs_pal_excp_sigaction_done, PRS_TRUE)) { 105 | for (int i = 0; i < prs_pal_excp_handled_signal_count; ++i) { 106 | const int signum = prs_pal_excp_handled_signals[i]; 107 | const prs_result_t result = prs_pal_signal_action(signum, prs_pal_excp_sighandler, 108 | &s_prs_pal_excp_prev_sigaction); 109 | if (result != PRS_OK) { 110 | return result; 111 | } 112 | } 113 | } 114 | 115 | for (int i = 0; i < prs_pal_excp_handled_signal_count; ++i) { 116 | const int signum = prs_pal_excp_handled_signals[i]; 117 | const prs_result_t result = prs_pal_signal_unblock(signum); 118 | if (result != PRS_OK) { 119 | return result; 120 | } 121 | } 122 | 123 | return result; 124 | } 125 | 126 | void prs_pal_excp_uninit_worker(struct prs_worker* worker) 127 | { 128 | for (int i = 0; i < prs_pal_excp_handled_signal_count; ++i) { 129 | const int signum = prs_pal_excp_handled_signals[i]; 130 | prs_pal_signal_block(signum); 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /prs/pal/posix/mem.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the POSIX virtual memory definitions. 27 | */ 28 | 29 | #include 30 | #if PRS_PAL_OS == PRS_PAL_OS_LINUX 31 | /* Required on some distributions for MAP_HUGE_SHIFT */ 32 | #include 33 | #endif 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | static int prs_pal_mem_protect_flags(prs_pal_mem_flags_t flags) 42 | { 43 | int prot = PROT_NONE; 44 | 45 | if (flags & PRS_PAL_MEM_FLAG_READ) { 46 | prot |= PROT_READ; 47 | } 48 | if (flags & PRS_PAL_MEM_FLAG_WRITE) { 49 | prot |= PROT_WRITE; 50 | } 51 | if (flags & PRS_PAL_MEM_FLAG_EXECUTE) { 52 | prot |= PROT_EXEC; 53 | } 54 | 55 | return prot; 56 | } 57 | 58 | void* prs_pal_mem_map(prs_size_t size, prs_pal_mem_flags_t flags) 59 | { 60 | PRS_ASSERT(!(flags & PRS_PAL_MEM_FLAG_ALL_PROTECTION) || (flags & PRS_PAL_MEM_FLAG_COMMIT)); 61 | PRS_ASSERT(!(flags & PRS_PAL_MEM_FLAG_LARGE_PAGE) || (flags & PRS_PAL_MEM_FLAG_COMMIT)); 62 | 63 | int prot = PROT_NONE; 64 | int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS; 65 | 66 | #if defined(PRS_ASSERTIONS) 67 | const prs_size_t page_size = (flags & PRS_PAL_MEM_FLAG_LARGE_PAGE) ? 68 | prs_pal_os_get_huge_page_size() : 69 | prs_pal_os_get_page_size(); 70 | PRS_ASSERT((size % page_size) == 0); 71 | #endif 72 | 73 | if (flags & PRS_PAL_MEM_FLAG_COMMIT) { 74 | if (flags & PRS_PAL_MEM_FLAG_ALL_PROTECTION) { 75 | prot = prs_pal_mem_protect_flags(flags); 76 | } 77 | } 78 | 79 | if (flags & PRS_PAL_MEM_FLAG_LARGE_PAGE) { 80 | mmap_flags |= MAP_HUGETLB; 81 | mmap_flags |= prs_bitops_hsb_uint(prs_pal_os_get_huge_page_size()) << MAP_HUGE_SHIFT; 82 | } 83 | 84 | if (flags & PRS_PAL_MEM_FLAG_LOCK) { 85 | mmap_flags |= MAP_LOCKED; 86 | } 87 | 88 | void* result = mmap(0, size, prot, mmap_flags, 0, 0); 89 | PRS_ERROR_WHEN(!result); 90 | return result; 91 | } 92 | 93 | void prs_pal_mem_unmap(void* ptr, prs_size_t size) 94 | { 95 | const int error = munmap(ptr, size); 96 | PRS_ERROR_WHEN(error); 97 | } 98 | 99 | void prs_pal_mem_commit(void* ptr, prs_size_t size, prs_pal_mem_flags_t flags) 100 | { 101 | PRS_ASSERT(ptr); 102 | const int prot = prs_pal_mem_protect_flags(flags); 103 | const int error = mprotect(ptr, size, prot); 104 | PRS_ERROR_WHEN(error); 105 | } 106 | 107 | void prs_pal_mem_uncommit(void* ptr, prs_size_t size) 108 | { 109 | PRS_ASSERT(ptr); 110 | const int error = mprotect(ptr, size, PROT_NONE); 111 | PRS_ERROR_WHEN(error); 112 | } 113 | 114 | void prs_pal_mem_lock(void* ptr, prs_size_t size) 115 | { 116 | PRS_ASSERT(ptr); 117 | const int error = mlock(ptr, size); 118 | PRS_ERROR_WHEN(error); 119 | } 120 | 121 | void prs_pal_mem_unlock(void* ptr, prs_size_t size) 122 | { 123 | PRS_ASSERT(ptr); 124 | const int error = munlock(ptr, size); 125 | PRS_ERROR_WHEN(error); 126 | } 127 | 128 | void prs_pal_mem_protect(void* ptr, prs_size_t size, prs_pal_mem_flags_t flags) 129 | { 130 | PRS_ASSERT(ptr); 131 | const int prot = prs_pal_mem_protect_flags(flags); 132 | const int error = mprotect(ptr, size, prot); 133 | PRS_ERROR_WHEN(error); 134 | } 135 | -------------------------------------------------------------------------------- /prs/pal/posix/signal.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the POSIX signal definitions. 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | #include "signal.h" 39 | 40 | static PRS_TLS prs_bool_t s_prs_pal_signal_initialized = PRS_FALSE; 41 | static PRS_TLS void* s_prs_pal_signal_stack = 0; 42 | 43 | static prs_result_t prs_pal_signal_init(void) 44 | { 45 | if (!s_prs_pal_signal_initialized) { 46 | /* Set up the alternate stack for this thread */ 47 | int error; 48 | prs_size_t stack_size; 49 | void* stack_top = prs_stack_create(SIGSTKSZ, &stack_size); 50 | PRS_FATAL_WHEN(!stack_top); 51 | s_prs_pal_signal_stack = (void*)((prs_uintptr_t)stack_top - stack_size); 52 | stack_t altstack = { 53 | .ss_sp = s_prs_pal_signal_stack, 54 | .ss_flags = 0, 55 | .ss_size = stack_size 56 | }; 57 | error = sigaltstack(&altstack, 0); 58 | if (error) { 59 | return PRS_PLATFORM_ERROR; 60 | } 61 | 62 | /* Block all signals by default */ 63 | sigset_t sigset; 64 | error = sigfillset(&sigset); 65 | if (error) { 66 | return PRS_PLATFORM_ERROR; 67 | } 68 | error = pthread_sigmask(SIG_BLOCK, &sigset, 0); 69 | if (error) { 70 | return PRS_PLATFORM_ERROR; 71 | } 72 | s_prs_pal_signal_initialized = PRS_TRUE; 73 | } 74 | return PRS_OK; 75 | } 76 | 77 | static prs_result_t prs_pal_signal_set(int signum, int action) 78 | { 79 | prs_result_t result; 80 | result = prs_pal_signal_init(); 81 | if (result != PRS_OK) { 82 | return result; 83 | } 84 | 85 | int error; 86 | sigset_t sigset; 87 | error = sigemptyset(&sigset); 88 | if (error) { 89 | return PRS_PLATFORM_ERROR; 90 | } 91 | error = sigaddset(&sigset, signum); 92 | if (error) { 93 | return PRS_PLATFORM_ERROR; 94 | } 95 | error = pthread_sigmask(action, &sigset, 0); 96 | if (error) { 97 | return PRS_PLATFORM_ERROR; 98 | } 99 | return PRS_OK; 100 | } 101 | 102 | prs_result_t prs_pal_signal_block(int signum) 103 | { 104 | return prs_pal_signal_set(signum, SIG_BLOCK); 105 | } 106 | 107 | prs_result_t prs_pal_signal_unblock(int signum) 108 | { 109 | return prs_pal_signal_set(signum, SIG_UNBLOCK); 110 | } 111 | 112 | prs_result_t prs_pal_signal_action(int signum, void (*handler)(int, siginfo_t*, void*), struct sigaction* prev) 113 | { 114 | int error; 115 | struct sigaction sa; 116 | memset(&sa, 0, sizeof(sa)); 117 | sa.sa_sigaction = handler; 118 | error = sigfillset(&sa.sa_mask); /* No nested signals */ 119 | if (error) { 120 | return PRS_PLATFORM_ERROR; 121 | } 122 | sa.sa_flags = SA_ONSTACK | SA_SIGINFO; 123 | 124 | error = sigaction(signum, &sa, prev); 125 | if (error) { 126 | return PRS_PLATFORM_ERROR; 127 | } 128 | 129 | return PRS_OK; 130 | } 131 | 132 | void prs_pal_signal_chain(int signum, siginfo_t* sinfo, void* context, struct sigaction* action) 133 | { 134 | if (action) { 135 | if (action->sa_flags & SA_SIGINFO) { 136 | if (action->sa_sigaction) { 137 | action->sa_sigaction(signum, sinfo, context); 138 | } 139 | } else { 140 | if (action->sa_handler) { 141 | action->sa_handler(signum); 142 | } 143 | } 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /prs/pal/posix/signal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the POSIX signal declarations. 27 | */ 28 | 29 | #ifndef _PRS_PAL_SIGNAL_H 30 | #define _PRS_PAL_SIGNAL_H 31 | 32 | #include 33 | #if PRS_PAL_OS != PRS_PAL_OS_LINUX 34 | #error signal.h only supported on Linux 35 | #endif 36 | 37 | #include 38 | 39 | #include 40 | 41 | prs_result_t prs_pal_signal_block(int signum); 42 | prs_result_t prs_pal_signal_unblock(int signum); 43 | 44 | prs_result_t prs_pal_signal_action(int signum, void (*handler)(int, siginfo_t*, void*), struct sigaction* prev); 45 | void prs_pal_signal_chain(int signum, siginfo_t* sinfo, void* context, struct sigaction* action); 46 | 47 | #endif /* _PRS_PAL_SIGNAL_H */ 48 | -------------------------------------------------------------------------------- /prs/pal/stdc/exit.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the standard C exit definitions. 27 | */ 28 | 29 | #include 30 | 31 | #include 32 | 33 | void prs_pal_exit(int status) 34 | { 35 | /* 36 | * We use _exit() here instead of exit() because this function could be called from a signal handler and the exit() 37 | * function is not async-safe. 38 | */ 39 | _exit(status); 40 | } 41 | -------------------------------------------------------------------------------- /prs/pal/stdc/malloc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the standard C memory allocation definitions. 27 | */ 28 | 29 | #include 30 | 31 | #include 32 | 33 | void* prs_pal_malloc(prs_size_t size) 34 | { 35 | return malloc(size); 36 | } 37 | 38 | void* prs_pal_malloc_zero(prs_size_t size) 39 | { 40 | return calloc(1, size); 41 | } 42 | 43 | void prs_pal_free(void* data) 44 | { 45 | free(data); 46 | } 47 | -------------------------------------------------------------------------------- /prs/pal/stdc/wls.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the standard C worker local storage definitions. 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | /* 33 | * Note: if you see a crash in this file while debugging with GDB, this is possibly due to a bug in the build 34 | * toolchain generating bad DWARF debugging information. See: 35 | * http://stackoverflow.com/questions/33429912/program-compiled-with-fpic-crashes-while-stepping-over-thread-local-variable-in 36 | */ 37 | 38 | static PRS_TLS void* s_prs_thread_local_data; 39 | 40 | prs_result_t prs_wls_init(void) 41 | { 42 | return PRS_OK; 43 | } 44 | 45 | void prs_wls_uninit(void) 46 | { 47 | } 48 | 49 | prs_result_t prs_wls_worker_init(struct prs_worker* worker) 50 | { 51 | s_prs_thread_local_data = 0; 52 | return PRS_OK; 53 | } 54 | 55 | void prs_wls_worker_uninit(struct prs_worker* worker) 56 | { 57 | } 58 | 59 | void prs_wls_set(void* data) 60 | { 61 | s_prs_thread_local_data = data; 62 | } 63 | 64 | void* prs_wls_get(void) 65 | { 66 | return s_prs_thread_local_data; 67 | } 68 | -------------------------------------------------------------------------------- /prs/pal/windows/assert.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the Windows assertion definitions. 27 | */ 28 | 29 | #include 30 | 31 | #include 32 | 33 | void prs_pal_assert(const char* expr, const char* file, int line) 34 | { 35 | _assert(expr, file, line); 36 | } 37 | -------------------------------------------------------------------------------- /prs/pal/windows/excp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the Windows exception definitions. 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | static prs_bool_t s_prs_pal_excp_registered = PRS_FALSE; 42 | 43 | static LONG prs_pal_excp_windows_vectored_handler(PEXCEPTION_POINTERS ExceptionInfo) 44 | { 45 | struct prs_worker* worker = prs_worker_current(); 46 | if (!worker) { 47 | /* No worker for this thread. Do not process this exception here. */ 48 | PRS_FTRACE("no worker"); 49 | return EXCEPTION_CONTINUE_SEARCH; 50 | } 51 | 52 | PEXCEPTION_RECORD record = ExceptionInfo->ExceptionRecord; 53 | PCONTEXT wincontext = ExceptionInfo->ContextRecord; 54 | 55 | PRS_FTRACE("code 0x%08X", record->ExceptionCode); 56 | 57 | enum prs_excp_type type = PRS_EXCP_TYPE_UNKNOWN; 58 | void* extra = 0; 59 | switch (record->ExceptionCode) { 60 | case STATUS_ACCESS_VIOLATION: 61 | type = PRS_EXCP_TYPE_SEGMENTATION_FAULT; 62 | extra = (void *)record->ExceptionInformation[1]; 63 | break; 64 | 65 | case STATUS_GUARD_PAGE_VIOLATION: 66 | type = PRS_EXCP_TYPE_STACK_OVERFLOW; 67 | extra = (void *)record->ExceptionInformation[1]; 68 | break; 69 | 70 | case STATUS_INVALID_HANDLE: 71 | case STATUS_INVALID_PARAMETER: 72 | case STATUS_ASSERTION_FAILURE: 73 | type = PRS_EXCP_TYPE_OS; 74 | break; 75 | 76 | case STATUS_ILLEGAL_INSTRUCTION: 77 | case STATUS_PRIVILEGED_INSTRUCTION: 78 | type = PRS_EXCP_TYPE_ILLEGAL_INSTRUCTION; 79 | break; 80 | 81 | case STATUS_CONTROL_C_EXIT: 82 | /* This isn't going to work most of the time. We use SetConsoleCtrlHandler() in the PAL OS module. */ 83 | type = PRS_EXCP_TYPE_USER_INTERRUPT; 84 | break; 85 | 86 | case STATUS_FLOAT_MULTIPLE_FAULTS: 87 | case STATUS_FLOAT_MULTIPLE_TRAPS: 88 | case STATUS_FLOAT_DENORMAL_OPERAND: 89 | case STATUS_FLOAT_DIVIDE_BY_ZERO: 90 | case STATUS_FLOAT_INEXACT_RESULT: 91 | case STATUS_FLOAT_INVALID_OPERATION: 92 | case STATUS_FLOAT_OVERFLOW: 93 | case STATUS_FLOAT_STACK_CHECK: 94 | case STATUS_FLOAT_UNDERFLOW: 95 | type = PRS_EXCP_TYPE_FLOATING_POINT; 96 | break; 97 | 98 | case STATUS_INTEGER_DIVIDE_BY_ZERO: 99 | case STATUS_INTEGER_OVERFLOW: 100 | type = PRS_EXCP_TYPE_INTEGER; 101 | break; 102 | } 103 | 104 | prs_excp_raise(type, extra, worker, (struct prs_pal_context*)wincontext); 105 | 106 | return EXCEPTION_CONTINUE_EXECUTION; 107 | } 108 | 109 | prs_result_t prs_pal_excp_init_worker(struct prs_worker* worker) 110 | { 111 | prs_result_t result = PRS_OK; 112 | 113 | if (!s_prs_pal_excp_registered) { 114 | s_prs_pal_excp_registered = PRS_TRUE; 115 | const PVOID add_result = AddVectoredExceptionHandler(1, prs_pal_excp_windows_vectored_handler); 116 | if (!add_result) { 117 | result = PRS_PLATFORM_ERROR; 118 | } 119 | } 120 | 121 | return result; 122 | } 123 | 124 | void prs_pal_excp_uninit_worker(struct prs_worker* worker) 125 | { 126 | if (s_prs_pal_excp_registered) { 127 | s_prs_pal_excp_registered = PRS_FALSE; 128 | RemoveVectoredExceptionHandler(prs_pal_excp_windows_vectored_handler); 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /prs/pal/windows/wls.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the Windows worker local storage definitions. 27 | */ 28 | 29 | #include 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | /* https://en.wikipedia.org/wiki/Win32_Thread_Information_Block */ 37 | typedef struct _TEB { 38 | BYTE Reserved1[1952]; 39 | PVOID Reserved2[412]; 40 | PVOID TlsSlots[64]; 41 | BYTE Reserved3[8]; 42 | PVOID Reserved4[26]; 43 | PVOID ReservedForOle; 44 | PVOID Reserved5[4]; 45 | PVOID TlsExpansionSlots; 46 | } TEB; 47 | typedef TEB *PTEB; 48 | 49 | static prs_int_t s_prs_wls_tls_slot = 0; 50 | 51 | static PRS_INLINE PTEB prs_wls_get_teb(void) 52 | { 53 | /* http://www.nynaeve.net/?p=185 */ 54 | /* 55 | * On x86, the TEB is located at fs:0x18. 56 | * On AMD64, the TEB is located at gs:0x30 (0x30 is 0x18 multiplied by 2 because of the larger pointer size). 57 | */ 58 | PTEB teb = 0; 59 | __asm__( 60 | "movq %%gs:0x30, %%rcx\n" 61 | "movq %%rcx, %0\n" 62 | : "=r" (teb) 63 | : 64 | : "rcx" 65 | ); 66 | return teb; 67 | } 68 | 69 | prs_result_t prs_wls_init(void) 70 | { 71 | s_prs_wls_tls_slot = (prs_int_t)TlsAlloc(); 72 | return PRS_OK; 73 | } 74 | 75 | void prs_wls_uninit(void) 76 | { 77 | } 78 | 79 | prs_result_t prs_wls_worker_init(struct prs_worker* worker) 80 | { 81 | return PRS_OK; 82 | } 83 | 84 | void prs_wls_worker_uninit(struct prs_worker* worker) 85 | { 86 | } 87 | 88 | void prs_wls_set(void* data) 89 | { 90 | /* TlsSetValue(s_prs_wls_tls_slot, data); */ 91 | PTEB teb = prs_wls_get_teb(); 92 | teb->TlsSlots[s_prs_wls_tls_slot] = data; 93 | } 94 | 95 | void* prs_wls_get(void) 96 | { 97 | void* result; 98 | 99 | __asm__( 100 | "movq %%gs:0x30, %%rcx\n" 101 | "movq %[index], %%rdx\n" 102 | "movq %c[offset](%%rcx, %%rdx, %c[size]), %[result]\n" 103 | : [result] "=r" (result) 104 | : [offset] "e" (offsetof(TEB, TlsSlots)), 105 | [index] "r" (s_prs_wls_tls_slot), 106 | [size] "e" (PRS_PAL_POINTER_SIZE) 107 | : "rcx", "rdx" 108 | ); 109 | 110 | return result; 111 | 112 | /* return TlsGetValue(s_prs_wls_tls_slot); */ 113 | /* 114 | PTEB teb = prs_wls_get_teb(); 115 | return teb->TlsSlots[s_prs_wls_tls_slot]; 116 | */ 117 | } 118 | 119 | -------------------------------------------------------------------------------- /prs/proc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the private declarations for a process control block (\ref prs_proc). 27 | */ 28 | 29 | #ifndef _PRSP_PROC_H 30 | #define _PRSP_PROC_H 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | typedef prs_int_t prs_proc_range_table_index_t; 38 | 39 | struct prs_proc { 40 | struct prs_idllist_node node; 41 | 42 | prs_proc_id_t id; 43 | char filename[PRS_MAX_PATH]; 44 | struct prs_proc_main_params main_params; 45 | struct prs_pal_proc* pal_proc; 46 | char* cmdline_buffer; 47 | struct prs_task* main_task; 48 | 49 | prs_proc_range_table_index_t range_table_index; 50 | 51 | struct prs_mpsciq* objects; 52 | struct prs_mpscq* atexit_callbacks; 53 | 54 | prs_bool_t destroyed; 55 | }; 56 | 57 | #endif /* _PRSP_PROC_H */ 58 | -------------------------------------------------------------------------------- /prs/rtc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the run-time check definitions. 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | /** 33 | * \brief 34 | * Prints a run-time check failure to the log. 35 | * \param expr 36 | * Expression that is printed into the log. 37 | * \param file 38 | * File where the failure occurred. 39 | * \param line 40 | * Line in \p file where the failure occurred. 41 | * \note 42 | * This should never be called directly by code. Use \ref PRS_RTC_IF instead. 43 | */ 44 | void prs_rtc(const char* expr, const char* file, int line) 45 | { 46 | prs_log_print("RTC: \"%s\" %s:%d\n", expr, file, line); 47 | } 48 | -------------------------------------------------------------------------------- /prs/spinlock.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains spinlock definitions. 27 | * 28 | * The spinlock is implemented using a simple exchange operation. The spinlock waits indefinitely for the resource to 29 | * be released. There is no queue for the workers that are waiting for the lock. This lock is meant to be used in 30 | * scenarios where mutually exclusion is critical but cannot be scheduled later. 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | struct prs_spinlock { 39 | PRS_ATOMIC prs_bool_t lock; 40 | }; 41 | 42 | /** 43 | * \brief 44 | * Creates a spinlock. 45 | */ 46 | struct prs_spinlock* prs_spinlock_create(void) 47 | { 48 | struct prs_spinlock* spinlock = prs_pal_malloc(sizeof(*spinlock)); 49 | PRS_FATAL_WHEN(!spinlock); 50 | 51 | prs_pal_atomic_store(&spinlock->lock, PRS_FALSE); 52 | 53 | return spinlock; 54 | } 55 | 56 | /** 57 | * \brief 58 | * Destroys a spinlock. 59 | * \param spinlock 60 | * Spinlock to destroy. 61 | */ 62 | void prs_spinlock_destroy(struct prs_spinlock* spinlock) 63 | { 64 | prs_pal_free(spinlock); 65 | } 66 | 67 | /** 68 | * \brief 69 | * Acquire the lock. If it is not available, wait for it. 70 | * \param spinlock 71 | * Spinlock to wait for. 72 | */ 73 | void prs_spinlock_lock(struct prs_spinlock* spinlock) 74 | { 75 | while (!prs_spinlock_try_lock(spinlock)) 76 | { 77 | } 78 | } 79 | 80 | /** 81 | * \brief 82 | * Tries to acquire the lock. 83 | * \param spinlock 84 | * Spinlock to lock. 85 | * \return 86 | * \ref PRS_TRUE if the spinlock was effectively locked. 87 | * \ref PRS_FALSE if the spinlock was not locked. 88 | */ 89 | prs_bool_t prs_spinlock_try_lock(struct prs_spinlock* spinlock) 90 | { 91 | return PRS_BOOL(!prs_pal_atomic_exchange(&spinlock->lock, PRS_TRUE)); 92 | } 93 | 94 | /** 95 | * \brief 96 | * Releases a spinlock. 97 | * \param spinlock 98 | * Spinlock to release. 99 | */ 100 | void prs_spinlock_unlock(struct prs_spinlock* spinlock) 101 | { 102 | prs_pal_atomic_store(&spinlock->lock, 0); 103 | } 104 | -------------------------------------------------------------------------------- /prs/svc/log.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the definitions for the log service. 27 | * 28 | * The log service is a single task that periodically flushes the log. The period at which the log is flushed is 29 | * determined by the rate of log entries that are generated. 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | #include "../task.h" 42 | 43 | static void prs_svc_log_task(void* userdata) 44 | { 45 | struct prs_worker* worker = prs_worker_current(); 46 | PRS_ASSERT(worker); 47 | 48 | const prs_ticks_t max_ticks_to_wait = PRS_TICKS_FROM_MS(1000); 49 | const prs_ticks_t min_ticks_to_wait = PRS_TICKS_FROM_MS(100); 50 | 51 | prs_ticks_t ticks_to_wait = min_ticks_to_wait; 52 | for (;;) { 53 | prs_worker_int_disable(worker); 54 | prs_sched_sleep(ticks_to_wait); 55 | prs_worker_int_enable(worker); 56 | 57 | prs_worker_int_disable(worker); 58 | const prs_int_t entries_written = prs_log_flush(); 59 | prs_worker_int_enable(worker); 60 | const prs_int_t entries_per_second = (1000 * entries_written) / PRS_TICKS_TO_MS(ticks_to_wait); 61 | 62 | ticks_to_wait = PRS_TICKS_FROM_MS(1000 / (entries_per_second ? entries_per_second : 1)); 63 | if (ticks_to_wait > max_ticks_to_wait) { 64 | ticks_to_wait = max_ticks_to_wait; 65 | } 66 | if (ticks_to_wait < min_ticks_to_wait) { 67 | ticks_to_wait = min_ticks_to_wait; 68 | } 69 | } 70 | } 71 | 72 | /** 73 | * \brief 74 | * Initializes the log service task on the specified scheduler, at the specified priority. 75 | * \param sched_id 76 | * Scheduler object ID to add the task to. 77 | * \param prio 78 | * Priority of the log service. 79 | */ 80 | void prs_svc_log_init(prs_sched_id_t sched_id, prs_task_prio_t prio) 81 | { 82 | struct prs_task_create_params params = { 83 | .name = "prs_svc_log", 84 | .userdata = 0, 85 | .stack_size = 8192, 86 | .prio = prio, 87 | .entry = prs_svc_log_task 88 | }; 89 | struct prs_task* task = prs_task_create(¶ms); 90 | PRS_FATAL_WHEN(!task); 91 | 92 | const prs_result_t result = prs_sched_add_task(sched_id, task->id); 93 | PRS_FATAL_WHEN(result != PRS_OK); 94 | } 95 | -------------------------------------------------------------------------------- /prs/systeminfo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains system information definitions. 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | static struct prs_systeminfo s_prs_systeminfo; 37 | 38 | /** 39 | * \brief 40 | * Returns the system information. 41 | * \note 42 | * This function actually retrieves the system information only the first time it is called. 43 | */ 44 | struct prs_systeminfo* prs_systeminfo_get(void) 45 | { 46 | static prs_bool_t s_systeminfo_set = PRS_FALSE; 47 | 48 | if (!s_systeminfo_set) { 49 | s_prs_systeminfo.os_id = PRS_PAL_OS; 50 | s_prs_systeminfo.os = PRS_PAL_OS_NAME; 51 | s_prs_systeminfo.os_version = prs_pal_os_get_version(); 52 | s_prs_systeminfo.arch_id = PRS_PAL_ARCH; 53 | s_prs_systeminfo.arch = PRS_PAL_ARCH_NAME; 54 | s_prs_systeminfo.arch_bits = PRS_PAL_POINTER_SIZE * 8; 55 | //s_prs_systeminfo.march; 56 | s_prs_systeminfo.compiler_id = PRS_PAL_COMPILER; 57 | s_prs_systeminfo.compiler = PRS_PAL_COMPILER_NAME; 58 | s_prs_systeminfo.compiler_version = PRS_PAL_COMPILER_VERSION; 59 | s_prs_systeminfo.computer = prs_pal_os_get_computer(); 60 | s_prs_systeminfo.config_id = PRS_PAL_CONFIG; 61 | s_prs_systeminfo.config = PRS_PAL_CONFIG_NAME; 62 | s_prs_systeminfo.version_number = 0; 63 | s_prs_systeminfo.version = PRS_VERSION_NAME; 64 | prs_size_t core_count = prs_pal_os_get_core_count(); 65 | if (core_count > PRS_MAX_CPU) { 66 | /* Clamp the number of CPUs to the maximum supported by PRS */ 67 | core_count = PRS_MAX_CPU; 68 | } 69 | s_prs_systeminfo.core_count = core_count; 70 | 71 | s_systeminfo_set = PRS_TRUE; 72 | } 73 | 74 | return &s_prs_systeminfo; 75 | } 76 | -------------------------------------------------------------------------------- /prs/task.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Portable Runtime System (PRS) 3 | * Copyright (C) 2016 Alexandre Tremblay 4 | * 5 | * This file is part of PRS. 6 | * 7 | * PRS is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Affero General Public License as 9 | * published by the Free Software Foundation, either version 3 of the 10 | * License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Affero General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Affero General Public License 18 | * along with this program. If not, see . 19 | * 20 | * portableruntimesystem@gmail.com 21 | */ 22 | 23 | /** 24 | * \file 25 | * \brief 26 | * This file contains the private declarations for a task control block (\ref prs_task). 27 | */ 28 | 29 | #ifndef _PRSP_TASK_H 30 | #define _PRSP_TASK_H 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | /** 40 | * \brief 41 | * The enumeration of all the possible task states. 42 | */ 43 | enum prs_task_state { 44 | /** \brief The task is not started and is waiting to be added to a scheduler. */ 45 | PRS_TASK_STATE_STOPPED = 0, 46 | /** \brief The task is ready to be executed. */ 47 | PRS_TASK_STATE_READY, 48 | /** \brief The task is currently executing. */ 49 | PRS_TASK_STATE_RUNNING, 50 | /** \brief The task is blocked, waiting for an event to occur. */ 51 | PRS_TASK_STATE_BLOCKED, 52 | /** \brief The task partly destroyed. */ 53 | PRS_TASK_STATE_ZOMBIE 54 | }; 55 | 56 | /** 57 | * \brief 58 | * The task token contains its current state and a version number which is used to ensure that it can be unblocked 59 | * atomically. 60 | */ 61 | typedef prs_uint_t prs_task_token_t; 62 | 63 | struct prs_task { 64 | prs_task_id_t id; 65 | 66 | char name[PRS_MAX_TASK_NAME]; 67 | void* userdata; 68 | 69 | void* stack; 70 | prs_size_t stack_size; 71 | 72 | prs_task_prio_t prio; 73 | 74 | prs_proc_id_t proc_id; 75 | prs_sched_id_t sched_id; 76 | 77 | void* sched_userdata; 78 | 79 | PRS_ATOMIC prs_task_token_t state; 80 | 81 | struct prs_pal_context* context; 82 | 83 | void (*entry)(void* userdata); 84 | 85 | struct prs_msgq* msgq; 86 | }; 87 | 88 | enum prs_task_state prs_task_get_state(struct prs_task* task); 89 | void prs_task_change_state(struct prs_task* task, enum prs_task_state expected_state, enum prs_task_state new_state); 90 | 91 | prs_task_token_t prs_task_block(struct prs_task* task); 92 | prs_bool_t prs_task_unblock(struct prs_task* task, prs_task_token_t token, prs_uint8_t cause); 93 | 94 | prs_uint8_t prs_task_get_last_unblock_cause(struct prs_task* task); 95 | 96 | prs_result_t prs_task_set_proc(struct prs_task* task, prs_proc_id_t proc_id); 97 | 98 | #endif /* _PRSP_TASK_H */ 99 | --------------------------------------------------------------------------------