From: Tomas Mudrunka Date: Wed, 23 Jun 2021 13:20:01 +0000 (+0200) Subject: Refaktorace knihovny X-Git-Url: https://git.harvie.cz/?a=commitdiff_plain;h=3aac261937c3a94461f4dd8fa64c17d5e158c692;p=mirrors%2FPrograms.git Refaktorace knihovny --- diff --git a/c/pthread_extra/pthread_extra.h b/c/pthread_extra/pthread_extra.h new file mode 100644 index 0000000..84cba06 --- /dev/null +++ b/c/pthread_extra/pthread_extra.h @@ -0,0 +1,54 @@ +#ifndef __PTHREAD_EXTRA_H__ +#define __PTHREAD_EXTRA_H__ + +#include +#include +#include +#include + +#define PTHREAD_XTIME_NOBLOCK (&(struct timespec){ .tv_sec = 0, .tv_nsec = 0 }) +#define PTHREAD_XTIME_FOREVER NULL + +// Message queues + +#define PTHREAD_XMQ_FRONT true +#define PTHREAD_XMQ_BACK false + +#define PTHREAD_XMQ_RECV false +#define PTHREAD_XMQ_PEEK true + +typedef struct pthread_mq_t { + pthread_mutex_t lock; + pthread_cond_t cond_readable; + pthread_cond_t cond_writable; + void * data; + size_t msg_size; + size_t msg_count; + size_t msg_count_max; + size_t head_idx; + char * name; +} pthread_mq_t; + +bool pthread_mq_init(pthread_mq_t *mq, size_t msg_size, size_t msg_count_max); +void pthread_mq_free(pthread_mq_t *mq); +size_t pthread_mq_waiting(pthread_mq_t *mq); +bool pthread_mq_reset(pthread_mq_t *mq); +bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, const struct timespec *restrict abs_timeout); +bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, bool peek, const struct timespec *restrict abs_timeout); + +// Multi mutex locking + +#define pthread_mutex_swap(a, b) ({ pthread_mutex_t *s; s = (a); a = (b); b = s; }) + +#define pthread_mutex_lock_two(a,b) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, true, NULL) +#define pthread_mutex_timedlock_two(a,b,tm) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, true, (tm)) +#define pthread_mutex_trylock_two(a,b) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, false, NULL) + +#define pthread_mutex_lock_multi(lcks,cnt) pthread_mutex_timedlock_multi_generic((lcks),(cnt),true,NULL) +#define pthread_mutex_timedlock_multi(lcks,cnt,tm) pthread_mutex_timedlock_multi_generic((lcks),(cnt),true,(tm)) +#define pthread_mutex_trylock_multi(lcks,cnt) pthread_mutex_timedlock_multi_generic((lcks),(cnt),false,NULL) + +int pthread_mutex_timedlock_multi_generic(pthread_mutex_t **lck, int cnt, bool block, const struct timespec *restrict abs_timeout); + + +#endif //__PTHREAD_EXTRA_H__ diff --git a/c/pthread_extra/pthread_msgqueue.c b/c/pthread_extra/pthread_msgqueue.c index 6e509c7..dbdb65f 100644 --- a/c/pthread_extra/pthread_msgqueue.c +++ b/c/pthread_extra/pthread_msgqueue.c @@ -1,30 +1,11 @@ -/* - * CFLAGS=-lpthread make pthread_msgqueue - */ - #include +#include #include #include #include #include #include #include -#include - -#define PTHREAD_X_NONWAIT (&(struct timespec){ .tv_sec = 0, .tv_nsec = 0 }) -#define PTHREAD_X_FOREVER NULL - -typedef struct pthread_mq_t { - pthread_mutex_t lock; - pthread_cond_t cond_readable; - pthread_cond_t cond_writable; - void * data; - size_t msg_size; - size_t msg_count; - size_t msg_count_max; - size_t head_idx; - char * name; -} pthread_mq_t; bool pthread_mq_readable(pthread_mq_t *mq) { return (mq->msg_count > 0); } bool pthread_mq_writable(pthread_mq_t *mq) { return (mq->msg_count < mq->msg_count_max); } @@ -133,48 +114,3 @@ bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, bool peek, const pthread_mutex_unlock(&mq->lock); return true; } - - - - - - - - - - -pthread_mq_t myq; - -void *thread_recv(void *args) { - char str[128]; - while(1) { - pthread_mq_receive_generic(&myq, &str, false, NULL); - printf("RECVD: %.6s\t\t(waiting %d)\n", str, pthread_mq_waiting(&myq)); - sleep(1); - } -} - -int main() { - char tmp[128]; - - pthread_mq_init(&myq, 6, 5); - - pthread_t t; - pthread_create(&t, NULL, thread_recv, NULL); - - pthread_mq_send_generic(&myq, "AHOJ1", false, NULL); - pthread_mq_send_generic(&myq, "AHOJ2", false, NULL); - pthread_mq_send_generic(&myq, "AHOJ3", true, NULL); - pthread_mq_send_generic(&myq, "AHOJ4", true, NULL); - pthread_mq_send_generic(&myq, "AHOJ5", false, NULL); - pthread_mq_send_generic(&myq, "AHOJ6", true, NULL); - - while(1) { - pthread_mq_send_generic(&myq, "B", false, NULL); - pthread_mq_send_generic(&myq, "A", true, NULL); - pthread_mq_send_generic(&myq, " A", false, NULL); - pthread_mq_send_generic(&myq, " B", false, NULL); - } - - pthread_join(t, NULL); -} diff --git a/c/pthread_extra/pthread_multi.c b/c/pthread_extra/pthread_multi.c index 95c581c..992e627 100644 --- a/c/pthread_extra/pthread_multi.c +++ b/c/pthread_extra/pthread_multi.c @@ -1,22 +1,8 @@ -/* - * CFLAGS=-lpthread make pthread_multi - */ - -#include +#include #include #include #include -#define pthread_mutex_swap(a, b) ({ pthread_mutex_t *s; s = (a); a = (b); b = s; }) - -#define pthread_mutex_lock_two(a,b) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, true, NULL) -#define pthread_mutex_timedlock_two(a,b,tm) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, true, (tm)) -#define pthread_mutex_trylock_two(a,b) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, false, NULL) - -#define pthread_mutex_lock_multi(lcks,cnt) pthread_mutex_timedlock_multi_generic((lcks),(cnt),true,NULL) -#define pthread_mutex_timedlock_multi(lcks,cnt,tm) pthread_mutex_timedlock_multi_generic((lcks),(cnt),true,(tm)) -#define pthread_mutex_trylock_multi(lcks,cnt) pthread_mutex_timedlock_multi_generic((lcks),(cnt),false,NULL) - /* //This is first prototype for two mutexes only, it is useful in order to understand how this all works //Currently it was replaced by macro with the same name @@ -79,28 +65,3 @@ int pthread_mutex_timedlock_multi_generic(pthread_mutex_t **lck, int cnt, bool b pthread_mutex_swap(lck[0],lck[locked]); } } - -int main() { - //Prepare mutex array for tests - static pthread_mutex_t la = PTHREAD_MUTEX_INITIALIZER; - static pthread_mutex_t lb = PTHREAD_MUTEX_INITIALIZER; - static pthread_mutex_t lc = PTHREAD_MUTEX_INITIALIZER; - static pthread_mutex_t ld = PTHREAD_MUTEX_INITIALIZER; - static pthread_mutex_t *lck[4] = {&la, &lb, &lc, &ld}; - - //Set timeout - struct timespec tm; - clock_gettime(CLOCK_REALTIME, &tm); - tm.tv_sec += 5; - - //Lock one of the locks for testing - pthread_mutex_lock(lck[2]); - - if(!pthread_mutex_timedlock_multi(lck, 4, &tm)) { - //if(!pthread_mutex_timedlock_two(&la, lck[2], &tm)) { - printf("LOCKED!\n"); - } else { - printf("FAILED!\n"); - } - -} diff --git a/c/pthread_extra/test.c b/c/pthread_extra/test.c new file mode 100644 index 0000000..a41ca54 --- /dev/null +++ b/c/pthread_extra/test.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include + + +int main_mumu() { + //Prepare mutex array for tests + static pthread_mutex_t la = PTHREAD_MUTEX_INITIALIZER; + static pthread_mutex_t lb = PTHREAD_MUTEX_INITIALIZER; + static pthread_mutex_t lc = PTHREAD_MUTEX_INITIALIZER; + static pthread_mutex_t ld = PTHREAD_MUTEX_INITIALIZER; + static pthread_mutex_t *lck[4] = {&la, &lb, &lc, &ld}; + + //Set timeout + struct timespec tm; + clock_gettime(CLOCK_REALTIME, &tm); + tm.tv_sec += 5; + + //Lock one of the locks for testing + pthread_mutex_lock(lck[2]); + + if(!pthread_mutex_timedlock_multi(lck, 4, &tm)) { + //if(!pthread_mutex_timedlock_two(&la, lck[2], &tm)) { + printf("LOCKED!\n"); + } else { + printf("FAILED!\n"); + } + +} + + +pthread_mq_t myq; + +void *thread_recv(void *args) { + char str[128]; + while(1) { + pthread_mq_receive_generic(&myq, &str, false, PTHREAD_XTIME_FOREVER); + printf("RECVD: %.6s\t\t(waiting %d)\n", str, pthread_mq_waiting(&myq)); + //sleep(1); + } +} + +int main() { + main_mumu(); + + char tmp[128]; + + pthread_mq_init(&myq, 6, 5); + + pthread_t t; + pthread_create(&t, NULL, thread_recv, NULL); + + pthread_mq_send_generic(&myq, "AHOJ1", false, NULL); + pthread_mq_send_generic(&myq, "AHOJ2", false, NULL); + pthread_mq_send_generic(&myq, "AHOJ3", true, NULL); + pthread_mq_send_generic(&myq, "AHOJ4", true, NULL); + pthread_mq_send_generic(&myq, "AHOJ5", false, NULL); + pthread_mq_send_generic(&myq, "AHOJ6", true, NULL); + + while(1) { + pthread_mq_send_generic(&myq, "B", false, NULL); + pthread_mq_send_generic(&myq, "A", true, NULL); + pthread_mq_send_generic(&myq, " A", false, NULL); + pthread_mq_send_generic(&myq, " B", false, NULL); + sleep(1); + } + + pthread_join(t, NULL); +} +