From: Tomas Mudrunka Date: Wed, 23 Jun 2021 14:13:35 +0000 (+0200) Subject: Makefile X-Git-Url: http://git.harvie.cz/?p=mirrors%2FPrograms.git;a=commitdiff_plain;h=5251b63c80878b87d8cb808efd3978bd819c4f8b Makefile --- diff --git a/c/pthread_extra/Makefile b/c/pthread_extra/Makefile new file mode 100644 index 0000000..7668a2c --- /dev/null +++ b/c/pthread_extra/Makefile @@ -0,0 +1,32 @@ +#COPT=-O2 -flto -ftree-vectorize -ftree-slp-vectorize -fvisibility=hidden -D_FORTIFY_SOURCE=2 -DMG_ENABLE_FS=0 -fstack-protector-strong $(CARCH) +COPT=-fvisibility=hidden -DMG_ENABLE_FS=0 -fstack-protector-strong $(CARCH) +CDEF=-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -D_DEFAULT_SOURCE=1 -D_GNU_SOURCE +CERR=-Wall -Wextra -Werror -pedantic -Werror=date-time -Wformat-security -Wduplicated-cond -Wfloat-equal -Wshadow -Wlogical-not-parentheses -Wnull-dereference + +CLIB:= -pthread -I . +LIB:= -pthread + +CFLAGS+=$(COPT) $(CERR) -Wjump-misses-init $(CDEF) -std=c11 $(CLIB) +CXXFLAGS+=$(COPT) $(CERR) $(CDEF) -std=c++11 $(CLIB) +LDFLAGS+=$(COPT) $(LIB) + +BIN=test +OBJ=pthread_multi.o pthread_msgqueue.o test.o + +all: $(BIN) +$(BIN): $(OBJ) + $(CXX) -o $(BIN) $(OBJ) $(LDFLAGS) + strip --strip-unneeded $(BIN) + +clean: + rm -f $(BIN) $(OBJ) $(DEPS) + +DEPS:=$(OBJ:.o=.d) +-include $(DEPS) + +.c.o: + $(CC) -c $(CFLAGS) -MD -o $@ $< + +.cpp.o: + $(CXX) -c $(CXXFLAGS) -MD -o $@ $< + diff --git a/c/pthread_extra/pthread_extra.h b/c/pthread_extra/pthread_extra.h index 84cba06..e1fe782 100644 --- a/c/pthread_extra/pthread_extra.h +++ b/c/pthread_extra/pthread_extra.h @@ -21,7 +21,7 @@ typedef struct pthread_mq_t { pthread_mutex_t lock; pthread_cond_t cond_readable; pthread_cond_t cond_writable; - void * data; + uint8_t * data; size_t msg_size; size_t msg_count; size_t msg_count_max; @@ -38,7 +38,7 @@ bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, bool peek, const // Multi mutex locking -#define pthread_mutex_swap(a, b) ({ pthread_mutex_t *s; s = (a); a = (b); b = s; }) +#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)) diff --git a/c/pthread_extra/pthread_msgqueue.c b/c/pthread_extra/pthread_msgqueue.c index dbdb65f..168dc7f 100644 --- a/c/pthread_extra/pthread_msgqueue.c +++ b/c/pthread_extra/pthread_msgqueue.c @@ -49,6 +49,7 @@ 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) { + //printf("S-Timed: %p\n", abs_timeout); int ret; //Lock queue @@ -56,9 +57,12 @@ bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, const //Wait for queue to be in writable condition while(!pthread_mq_writable(mq)) { + //printf("S+Timed: %p\n", abs_timeout); if(abs_timeout == NULL) { ret = pthread_cond_wait(&mq->cond_writable, &mq->lock); } else { + //printf("STimed: %p\n", abs_timeout); + assert(abs_timeout != NULL); ret = pthread_cond_timedwait(&mq->cond_writable, &mq->lock, abs_timeout); } if(ret) { @@ -68,7 +72,7 @@ bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, const } //Write data to queue - size_t idx = ( ( mq->msg_count_max + mq->head_idx + (to_front?-1:mq->msg_count) ) % mq->msg_count_max ); + size_t idx = ( ( mq->head_idx + (to_front?mq->msg_count_max-1:mq->msg_count) ) % mq->msg_count_max ); void *ptr = mq->data + (idx * mq->msg_size); mq->msg_count++; if(to_front) mq->head_idx = (mq->msg_count_max + mq->head_idx - 1) % mq->msg_count_max; @@ -91,6 +95,8 @@ bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, bool peek, const if(abs_timeout == NULL) { ret = pthread_cond_wait(&mq->cond_readable, &mq->lock); } else { + //printf("RTimed: %p\n", abs_timeout); + assert(abs_timeout != NULL); ret = pthread_cond_timedwait(&mq->cond_readable, &mq->lock, abs_timeout); } if(ret) { diff --git a/c/pthread_extra/test.c b/c/pthread_extra/test.c index a41ca54..891cd50 100644 --- a/c/pthread_extra/test.c +++ b/c/pthread_extra/test.c @@ -17,7 +17,7 @@ int main_mumu() { //Set timeout struct timespec tm; clock_gettime(CLOCK_REALTIME, &tm); - tm.tv_sec += 5; + tm.tv_sec += 2; //Lock one of the locks for testing pthread_mutex_lock(lck[2]); @@ -29,24 +29,27 @@ int main_mumu() { printf("FAILED!\n"); } + return 0; } pthread_mq_t myq; -void *thread_recv(void *args) { +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); + printf("RECVD: %.6s\t\t(waiting %d)\n", str, (int)pthread_mq_waiting(&myq)); + sleep(1); } } int main() { - main_mumu(); + //main_mumu(); - char tmp[128]; + //char tmp[128]; pthread_mq_init(&myq, 6, 5);