--- /dev/null
+#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
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;
// 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))
}
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
//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) {
}
//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;
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) {
//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]);
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);