Browse Source

Initial commit

master
commit
c508884f16
  1. 3
      .gitignore
  2. 9
      LICENSE
  3. 11
      Makefile
  4. 47
      README.md
  5. 86
      src/cthlib.c
  6. 53
      src/cthlib.h
  7. 38
      testing/test.c

3
.gitignore vendored

@ -0,0 +1,3 @@
test
build/
.vscode/

9
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.

11
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

47
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 <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

86
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 <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;
}

53
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 <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);

38
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 <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…
Cancel
Save