Makefile
authorTomas Mudrunka <tomas@mudrunka.cz>
Wed, 23 Jun 2021 14:13:35 +0000 (16:13 +0200)
committerTomas Mudrunka <tomas@mudrunka.cz>
Wed, 23 Jun 2021 14:13:35 +0000 (16:13 +0200)
c/pthread_extra/Makefile [new file with mode: 0644]
c/pthread_extra/pthread_extra.h
c/pthread_extra/pthread_msgqueue.c
c/pthread_extra/test.c

diff --git a/c/pthread_extra/Makefile b/c/pthread_extra/Makefile
new file mode 100644 (file)
index 0000000..7668a2c
--- /dev/null
@@ -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)\r
+COPT=-fvisibility=hidden -DMG_ENABLE_FS=0 -fstack-protector-strong $(CARCH)\r
+CDEF=-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -D_DEFAULT_SOURCE=1 -D_GNU_SOURCE\r
+CERR=-Wall -Wextra -Werror -pedantic -Werror=date-time -Wformat-security -Wduplicated-cond -Wfloat-equal -Wshadow -Wlogical-not-parentheses -Wnull-dereference\r
+\r
+CLIB:= -pthread -I .\r
+LIB:= -pthread\r
+\r
+CFLAGS+=$(COPT) $(CERR) -Wjump-misses-init $(CDEF) -std=c11 $(CLIB)\r
+CXXFLAGS+=$(COPT) $(CERR) $(CDEF) -std=c++11 $(CLIB)\r
+LDFLAGS+=$(COPT) $(LIB)\r
+\r
+BIN=test\r
+OBJ=pthread_multi.o pthread_msgqueue.o test.o\r
+\r
+all: $(BIN)\r
+$(BIN): $(OBJ)\r
+       $(CXX) -o $(BIN) $(OBJ) $(LDFLAGS)\r
+       strip --strip-unneeded $(BIN)\r
+\r
+clean:\r
+       rm -f $(BIN) $(OBJ) $(DEPS)\r
+\r
+DEPS:=$(OBJ:.o=.d)\r
+-include $(DEPS)\r
+\r
+.c.o:\r
+       $(CC) -c $(CFLAGS) -MD -o $@ $<\r
+\r
+.cpp.o:\r
+       $(CXX) -c $(CXXFLAGS) -MD -o $@ $<\r
+\r
index 84cba0654b60d7aa7ee5d018440fcf0589f151b0..e1fe782cec638133d401592152c7c582644e7a59 100644 (file)
@@ -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))
index dbdb65ffc7166d3138a684a310cb72a6e7a6845e..168dc7f676fb7eb57b3a684ca11de7aadbcb878a 100644 (file)
@@ -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) {
index a41ca54a0023cdcfc714f18540d5fc9fe30ec4da..891cd504c2632171618254e0f32db5e85bc813d6 100644 (file)
@@ -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);
 
This page took 0.165459 seconds and 4 git commands to generate.