GIT.Harvie.CZ
/
mirrors
/
Programs.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
90c3a0d
)
Kompiluje se
author
Tomas Mudrunka
<tomas@mudrunka.cz>
Wed, 23 Jun 2021 10:32:59 +0000
(12:32 +0200)
committer
Tomas Mudrunka
<tomas@mudrunka.cz>
Wed, 23 Jun 2021 10:32:59 +0000
(12:32 +0200)
c/pthread_extra/pthread_msgqueue.c
patch
|
blob
|
blame
|
history
diff --git
a/c/pthread_extra/pthread_msgqueue.c
b/c/pthread_extra/pthread_msgqueue.c
index 0d4e07ea78c4f58cecc6b4f804cd85e9e0c9d9b6..9531d7884b8e2e13cc7cdf8ef6ef7b74fc0eb145 100644
(file)
--- a/
c/pthread_extra/pthread_msgqueue.c
+++ b/
c/pthread_extra/pthread_msgqueue.c
@@
-8,11
+8,12
@@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
+#include <assert.h>
typedef struct pthread_mq_t {
typedef struct pthread_mq_t {
-
static
pthread_mutex_t lock;
-
static
pthread_cond_t cond_readable;
-
static
pthread_cond_t cond_writable;
+ pthread_mutex_t lock;
+ pthread_cond_t cond_readable;
+ pthread_cond_t cond_writable;
void * data;
size_t msg_size;
size_t msg_count;
void * data;
size_t msg_size;
size_t msg_count;
@@
-21,13
+22,13
@@
typedef struct pthread_mq_t {
char * name;
} pthread_mq_t;
char * name;
} pthread_mq_t;
-bool pthread_mq_readable(pthread_mq_t *mq) { return (mq->count > 0); }
-bool pthread_mq_writable(pthread_mq_t *mq) { return (mq->
count < mq->
count_max); }
+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); }
bool pthread_mq_init(pthread_mq_t *mq, size_t msg_size, size_t msg_count_max) {
bool pthread_mq_init(pthread_mq_t *mq, size_t msg_size, size_t msg_count_max) {
-
mq->lock = PTHREAD_MUTEX_INITIALIZER
;
-
mq->cond_readable = PTHREAD_COND_INITIALIZER
;
-
mq->cond_writable = PTHREAD_COND_INITIALIZER
;
+
pthread_mutex_init(&mq->lock, NULL)
;
+
pthread_cond_init(&mq->cond_readable, NULL)
;
+
pthread_cond_init(&mq->cond_writable, NULL)
;
mq->data = malloc(msg_size*msg_count_max);
mq->msg_size = msg_size;
mq->msg_count_max = msg_count_max;
mq->data = malloc(msg_size*msg_count_max);
mq->msg_size = msg_size;
mq->msg_count_max = msg_count_max;
@@
-43,51
+44,45
@@
void pthread_mq_free(pthread_mq_t *mq) {
free(mq->data);
}
free(mq->data);
}
-
void pthread_mq_cond(pthread_mq_t *mq) {
if(pthread_mq_readable(mq)) pthread_cond_broadcast(&mq->cond_readable);
if(pthread_mq_writable(mq)) pthread_cond_broadcast(&mq->cond_writable);
}
void pthread_mq_cond(pthread_mq_t *mq) {
if(pthread_mq_readable(mq)) pthread_cond_broadcast(&mq->cond_readable);
if(pthread_mq_writable(mq)) pthread_cond_broadcast(&mq->cond_writable);
}
+bool pthread_mq_reset(pthread_mq_t *mq) {
+ if(pthread_mutex_lock(&mq->lock)) return false;
+ mq->msg_count = 0;
+ mq->head_idx = 0;
+ pthread_mq_cond(mq);
+ pthread_mutex_unlock(&mq->lock);
+ return true;
+}
+
bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, bool block, const struct timespec *restrict abs_timeout) {
bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, bool block, const struct timespec *restrict abs_timeout) {
- pthread_mutex_timedlock(&mq->lock, abs_timeout);
+ //Lock queue
+ if(pthread_mutex_timedlock(&mq->lock, abs_timeout)) return false;
+ //Wait for queue to be in writable condition
while(!pthread_mq_writable(mq)) {
while(!pthread_mq_writable(mq)) {
- pthread_mutex_unlock(&mq->lock);
- return false;
+ if(pthread_cond_timedwait(&mq->cond_writable, &mq->lock, abs_timeout)) {
+ pthread_mutex_unlock(&mq->lock);
+ return false;
+ }
}
}
- size_t idx = ( ( mq->count_max + mq->head_idx + (to_front?-1:mq->count) ) % mq->count_max );
+ //Write data to queue
+ size_t idx = ( ( mq->msg_count_max + mq->head_idx + (to_front?-1:mq->msg_count) ) % mq->msg_count_max );
void *ptr = mq->data + (idx * mq->msg_size);
mq->msg_count++;
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;
memcpy(ptr, data, mq->msg_size);
memcpy(ptr, data, mq->msg_size);
+ //Signal conditions and unlock
pthread_mq_cond(mq);
pthread_mutex_unlock(&mq->lock);
return true;
}
pthread_mq_cond(mq);
pthread_mutex_unlock(&mq->lock);
return true;
}
-/*
- mq_timedreceive(3) mq_timedreceive(2)
-
- mq_timedsend(3) mq_timedsend(2)
-
-void vQueueDelete( QueueHandle_t xQueue );
-BaseType_t xQueueReset( QueueHandle_t xQueue );
-
- BaseType_t xQueueSend(
- QueueHandle_t xQueue,
- const void * pvItemToQueue,
- TickType_t xTicksToWait
- );
- BaseType_t xQueueReceive(
- QueueHandle_t xQueue,
- void *pvBuffer,
- TickType_t xTicksToWait
- );
-
-*/
-
int main() {
}
int main() {
}
This page took
0.150641 seconds
and
4
git commands to generate.