commit c508884f16f15681aeb7d60598c10803a0907c03 Author: Unbewohnte Date: Wed Aug 30 13:59:25 2023 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4cc5740 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +test +build/ +.vscode/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e855d08 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ + The MIT License (MIT) + +Copyright © 2023 Kasyanov Nikolay ALexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f6da13a --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +lib: + mkdir -p build + gcc -Wall -Werror -O2 -c src/*.c + mv *.o build + ar rcs build/cthlib.a build/*.o + +test: clean + gcc -Wall -Werror -O2 -static src/cthlib.c testing/test.c -o test + +clean: + rm -rf test build \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..456c576 --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# cthlib +## C threads helper lib + +A small library to help with parallel function execution + +## Functions + +| Funcion | Description | +| --- | ----------- | +| `pool_t* pool_new` | Creates a new pool | +| `void pool_free(pool_t** pool)` | Frees up memory allocated to store a pool | +| `void pool_do(pool_t* pool, void* func, void* arg)` | Runs given function with arguments in parallel | +| `void pool_join(pool_t* pool)` | Waits for all parallel tasks to finish | + +## Test example + +```c +#include "../src/cthlib.h" +#include +#include +#include + + +void job(int* i) { + printf("start: %d\n", *i); + thrd_sleep(&(struct timespec){.tv_sec=2}, NULL); + *i = *i + 1; + printf("end: %d\n", *i); +} + +atomic_int count = 0; + +int main() { + pool_t* p = pool_new(); + + for (size_t i = 0; i < 5; i++) { + // Runs in parallel + pool_do(p, job, (void*) &count); + } + + pool_join(p); + pool_free(&p); +} +``` + +## License +MIT \ No newline at end of file diff --git a/src/cthlib.c b/src/cthlib.c new file mode 100644 index 0000000..f6230fa --- /dev/null +++ b/src/cthlib.c @@ -0,0 +1,86 @@ +/* + The MIT License (MIT) + +Copyright © 2023 Kasyanov Nikolay ALexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + + +#include "cthlib.h" +#include + + +/* +Creates a new pool. + +Returns a pointer to a newly created pool, NULL if an error occurs during creation +*/ +pool_t* pool_new() { + pool_t* pool = NULL; + + pool = malloc(sizeof(pool_t)); + if (!pool) { + return NULL; + } + + pool->thread_count = 0; + pool->threads = NULL; + + return pool; +} + + +/* +Frees up memory allocated to store a pool, sets a pointer to NULL as +to show that there is no pool present. +*/ +void pool_free(pool_t** pool) { + if (!pool) { + return; + } + if (!(*pool)) { + return; + } + + free(*pool); + *pool = NULL; + + return; +} + + +/* +Runs given function with arguments in parallel +*/ +void pool_do(pool_t* pool, void* func, void* arg) { + if (!pool | !func | !arg) { + return; + } + + pool->threads = realloc(pool->threads, sizeof(thrd_t) * (pool->thread_count+1)); + thrd_create(&(pool->threads[pool->thread_count]), (thrd_start_t) func, arg); + pool->thread_count++; + + return; +} + + +/* +Waits for all parallel tasks to finish +*/ +void pool_join(pool_t* pool) { + if (!pool) { + return; + } + + for (size_t i = 0; i < pool->thread_count; i++) { + thrd_join(pool->threads[i], NULL); + } + + return; +} \ No newline at end of file diff --git a/src/cthlib.h b/src/cthlib.h new file mode 100644 index 0000000..d069612 --- /dev/null +++ b/src/cthlib.h @@ -0,0 +1,53 @@ +/* + The MIT License (MIT) + +Copyright © 2023 Kasyanov Nikolay ALexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + + +#ifdef __STDC_NO_THREADS__ +#error "threads.h is not provided on this system!" +#endif + + +#include +#include + + +typedef struct pool_t { + size_t thread_count; + thrd_t* threads; +} pool_t; + + +/* +Creates a new pool. + +Returns a pointer to a newly created pool, NULL if an error occurs during creation +*/ +pool_t* pool_new(); + + +/* +Frees up memory allocated to store a pool, sets a pointer to NULL as +to show that there is no pool present. +*/ +void pool_free(pool_t** pool); + + +/* +Runs given function with arguments in parallel +*/ +void pool_do(pool_t* pool, void* func, void* arg); + + +/* +Waits for all parallel tasks to finish +*/ +void pool_join(pool_t* pool); \ No newline at end of file diff --git a/testing/test.c b/testing/test.c new file mode 100644 index 0000000..878aed5 --- /dev/null +++ b/testing/test.c @@ -0,0 +1,38 @@ +/* + The MIT License (MIT) + +Copyright © 2023 Kasyanov Nikolay ALexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + + +#include "../src/cthlib.h" +#include +#include +#include + + +void job(int* i) { + printf("start: %d\n", *i); + thrd_sleep(&(struct timespec){.tv_sec=2}, NULL); + *i = *i + 1; + printf("end: %d\n", *i); +} + +atomic_int count = 0; + +int main() { + pool_t* p = pool_new(); + + for (size_t i = 0; i < 5; i++) { + pool_do(p, job, (void*) &count); + } + + pool_join(p); + pool_free(&p); +} \ No newline at end of file