2017-04-15 21:22:19 +02:00
|
|
|
/** Wrapper around queue that uses POSIX CV's for signalling writes.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Georg Martin Reinke <georg.reinke@rwth-aachen.de>
|
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-04-15 21:22:19 +02:00
|
|
|
*********************************************************************************/
|
2017-04-07 12:18:08 +02:00
|
|
|
|
|
|
|
#include "queue_signalled.h"
|
|
|
|
|
|
|
|
int queue_signalled_init(struct queue_signalled *qs, size_t size, struct memtype *mem)
|
|
|
|
{
|
2017-04-15 18:59:22 +02:00
|
|
|
int ret;
|
2017-04-15 23:12:53 +02:00
|
|
|
|
|
|
|
pthread_condattr_t cvattr;
|
|
|
|
pthread_mutexattr_t mtattr;
|
2017-04-15 23:14:14 +02:00
|
|
|
|
|
|
|
ret = queue_init(&qs->queue, size, mem);
|
2017-04-15 18:59:22 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2017-04-15 23:12:53 +02:00
|
|
|
pthread_mutexattr_init(&mtattr);
|
|
|
|
pthread_condattr_init(&cvattr);
|
|
|
|
|
2017-05-05 19:24:16 +00:00
|
|
|
pthread_mutexattr_setpshared(&mtattr, PTHREAD_PROCESS_SHARED);
|
2017-04-15 23:12:53 +02:00
|
|
|
pthread_condattr_setpshared(&cvattr, PTHREAD_PROCESS_SHARED);
|
|
|
|
|
|
|
|
pthread_mutex_init(&qs->mutex, &mtattr);
|
|
|
|
pthread_cond_init(&qs->ready, &cvattr);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 23:12:53 +02:00
|
|
|
pthread_mutexattr_destroy(&mtattr);
|
|
|
|
pthread_condattr_destroy(&cvattr);
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2017-04-07 12:18:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int queue_signalled_destroy(struct queue_signalled *qs)
|
|
|
|
{
|
2017-04-15 18:59:22 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-04-15 23:14:14 +02:00
|
|
|
ret = queue_destroy(&qs->queue);
|
2017-04-15 18:59:22 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2017-04-07 12:18:08 +02:00
|
|
|
pthread_cond_destroy(&qs->ready);
|
2017-04-15 23:14:14 +02:00
|
|
|
pthread_mutex_destroy(&qs->mutex);
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2017-04-07 12:18:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-15 23:14:02 +02:00
|
|
|
int queue_signalled_push(struct queue_signalled *qs, void *ptr)
|
|
|
|
{
|
|
|
|
int ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 23:14:02 +02:00
|
|
|
ret = queue_push(&qs->queue, ptr);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 23:14:02 +02:00
|
|
|
pthread_mutex_lock(&qs->mutex);
|
|
|
|
pthread_cond_broadcast(&qs->ready);
|
|
|
|
pthread_mutex_unlock(&qs->mutex);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-07 12:18:08 +02:00
|
|
|
int queue_signalled_push_many(struct queue_signalled *qs, void *ptr[], size_t cnt)
|
|
|
|
{
|
2017-04-15 18:59:22 +02:00
|
|
|
int ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 23:14:14 +02:00
|
|
|
ret = queue_push_many(&qs->queue, ptr, cnt);
|
2017-04-15 18:59:22 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 23:14:14 +02:00
|
|
|
pthread_mutex_lock(&qs->mutex);
|
2017-04-15 18:59:22 +02:00
|
|
|
pthread_cond_broadcast(&qs->ready);
|
2017-04-15 23:14:14 +02:00
|
|
|
pthread_mutex_unlock(&qs->mutex);
|
2017-04-15 18:59:22 +02:00
|
|
|
|
|
|
|
return ret;
|
2017-04-07 12:18:08 +02:00
|
|
|
}
|
|
|
|
|
2017-04-15 23:14:02 +02:00
|
|
|
int queue_signalled_pull(struct queue_signalled *qs, void **ptr)
|
|
|
|
{
|
2017-05-12 13:08:34 +02:00
|
|
|
int ret = 0;
|
2017-05-14 11:35:49 +02:00
|
|
|
|
2017-04-15 23:14:02 +02:00
|
|
|
/* Make sure that qs->mutex is unlocked if this thread gets cancelled. */
|
|
|
|
pthread_cleanup_push((void (*)(void*)) pthread_mutex_unlock, &qs->mutex);
|
|
|
|
pthread_mutex_lock(&qs->mutex);
|
2017-05-14 11:35:49 +02:00
|
|
|
|
2017-05-12 13:08:34 +02:00
|
|
|
while (!ret) {
|
|
|
|
ret = queue_pull(&qs->queue, ptr);
|
|
|
|
if (ret == -1)
|
|
|
|
break;
|
2017-05-14 11:35:49 +02:00
|
|
|
|
2017-05-12 13:08:34 +02:00
|
|
|
if (!ret)
|
|
|
|
pthread_cond_wait(&qs->ready, &qs->mutex);
|
|
|
|
}
|
2017-05-14 11:35:49 +02:00
|
|
|
|
2017-04-15 23:14:02 +02:00
|
|
|
pthread_mutex_unlock(&qs->mutex);
|
|
|
|
pthread_cleanup_pop(0);
|
2017-05-14 11:35:49 +02:00
|
|
|
|
2017-05-12 13:08:34 +02:00
|
|
|
return ret;
|
2017-04-15 23:14:02 +02:00
|
|
|
}
|
|
|
|
|
2017-04-07 12:18:08 +02:00
|
|
|
int queue_signalled_pull_many(struct queue_signalled *qs, void *ptr[], size_t cnt)
|
|
|
|
{
|
2017-05-12 13:08:34 +02:00
|
|
|
int ret = 0;
|
2017-05-11 13:49:54 +02:00
|
|
|
|
2017-04-15 23:14:14 +02:00
|
|
|
/* Make sure that qs->mutex is unlocked if this thread gets cancelled. */
|
|
|
|
pthread_cleanup_push((void (*)(void*)) pthread_mutex_unlock, &qs->mutex);
|
|
|
|
pthread_mutex_lock(&qs->mutex);
|
2017-05-14 11:35:49 +02:00
|
|
|
|
2017-05-12 13:08:34 +02:00
|
|
|
while (!ret) {
|
|
|
|
ret = queue_pull_many(&qs->queue, ptr, cnt);
|
|
|
|
if (ret == -1)
|
|
|
|
break;
|
2017-05-14 11:35:49 +02:00
|
|
|
|
2017-05-12 13:08:34 +02:00
|
|
|
if (!ret)
|
|
|
|
pthread_cond_wait(&qs->ready, &qs->mutex);
|
|
|
|
}
|
2017-05-14 11:35:49 +02:00
|
|
|
|
2017-04-15 23:14:14 +02:00
|
|
|
pthread_mutex_unlock(&qs->mutex);
|
2017-04-10 11:39:35 +02:00
|
|
|
pthread_cleanup_pop(0);
|
2017-05-14 11:35:49 +02:00
|
|
|
|
2017-05-12 13:08:34 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int queue_signalled_close(struct queue_signalled *qs) {
|
|
|
|
int ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-05-12 13:08:34 +02:00
|
|
|
pthread_mutex_lock(&qs->mutex);
|
2017-05-14 11:35:49 +02:00
|
|
|
|
2017-05-12 13:08:34 +02:00
|
|
|
ret = queue_close(&qs->queue);
|
2017-05-14 11:35:49 +02:00
|
|
|
|
2017-05-12 13:08:34 +02:00
|
|
|
pthread_cond_broadcast(&qs->ready);
|
|
|
|
pthread_mutex_unlock(&qs->mutex);
|
2017-05-14 11:35:49 +02:00
|
|
|
|
2017-05-12 13:08:34 +02:00
|
|
|
return ret;
|
2017-04-07 12:18:08 +02:00
|
|
|
}
|