Kasianov Nikolai Alekseevich
1 year ago
commit
c508884f16
7 changed files with 247 additions and 0 deletions
@ -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. |
@ -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
|
@ -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 <threads.h> |
||||
#include <stdio.h> |
||||
#include <stdatomic.h> |
||||
|
||||
|
||||
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 |
@ -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 <stdlib.h> |
||||
|
||||
|
||||
/*
|
||||
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; |
||||
} |
@ -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 <threads.h> |
||||
#include <stdint.h> |
||||
|
||||
|
||||
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); |
@ -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 <threads.h> |
||||
#include <stdio.h> |
||||
#include <stdatomic.h> |
||||
|
||||
|
||||
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); |
||||
} |
Loading…
Reference in new issue