120 lines
2.5 KiB
C
120 lines
2.5 KiB
C
|
|
/* SPDX-License-Identifier: Apache-2.0 */
|
||
|
|
/**
|
||
|
|
* @file textbench.c
|
||
|
|
*
|
||
|
|
* @brief Testbench code file for Duplex Queue library
|
||
|
|
*
|
||
|
|
* @copyright Copyright (C) 2024 Jackrabbit Founders LLC. All rights reserved.
|
||
|
|
*
|
||
|
|
* @date Feb 2024
|
||
|
|
* @author Barrett Edwards <code@jrlabs.io>
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
|
||
|
|
/* INCLUDES ==================================================================*/
|
||
|
|
|
||
|
|
#include <unistd.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
|
||
|
|
#include <linux/types.h>
|
||
|
|
|
||
|
|
#include <ptrqueue.h>
|
||
|
|
|
||
|
|
#include "duplexqueue.h"
|
||
|
|
|
||
|
|
/* MACROS ====================================================================*/
|
||
|
|
|
||
|
|
#define QUEUE_CAPACITY 10
|
||
|
|
#define ITERATIONS 10000
|
||
|
|
|
||
|
|
/* ENUMERATIONS ==============================================================*/
|
||
|
|
|
||
|
|
/* STRUCTS ===================================================================*/
|
||
|
|
|
||
|
|
/* GLOBAL VARIABLES ==========================================================*/
|
||
|
|
|
||
|
|
/* PROTOTYPES ================================================================*/
|
||
|
|
|
||
|
|
|
||
|
|
void *consumer(void *arg)
|
||
|
|
{
|
||
|
|
struct duplex_queue *dq;
|
||
|
|
void *ptr;
|
||
|
|
int rv;
|
||
|
|
|
||
|
|
dq = (struct duplex_queue*) arg;
|
||
|
|
|
||
|
|
printf("%s started \n", __FUNCTION__);
|
||
|
|
|
||
|
|
for ( __u64 i = 0 ; i < ITERATIONS ; i++ ) {
|
||
|
|
ptr = pq_pop(dq->tx, 1);
|
||
|
|
rv = pq_push(dq->rx, ptr);
|
||
|
|
printf("%s pushed on to rx queue val:%p rv:%d\n", __FUNCTION__, ptr, rv);
|
||
|
|
}
|
||
|
|
|
||
|
|
return NULL;
|
||
|
|
}
|
||
|
|
|
||
|
|
void *producer(void *arg)
|
||
|
|
{
|
||
|
|
struct duplex_queue *dq;
|
||
|
|
int rv;
|
||
|
|
void *ptr;
|
||
|
|
|
||
|
|
dq = (struct duplex_queue*) arg;
|
||
|
|
|
||
|
|
printf("%s started \n", __FUNCTION__);
|
||
|
|
|
||
|
|
for ( __u64 i = 0 ; i < ITERATIONS ; i++ ) {
|
||
|
|
ptr = pq_pop(dq->rx, 1);
|
||
|
|
rv = pq_push(dq->tx, ptr);
|
||
|
|
printf("%s pushed on to tx queue val:%p rv:%d\n", __FUNCTION__, ptr, rv);
|
||
|
|
}
|
||
|
|
|
||
|
|
return NULL;
|
||
|
|
}
|
||
|
|
|
||
|
|
void threads(struct duplex_queue *dq)
|
||
|
|
{
|
||
|
|
int rv;
|
||
|
|
|
||
|
|
pthread_t producer_thread;
|
||
|
|
pthread_t consumer_thread;
|
||
|
|
|
||
|
|
rv = pthread_create( &consumer_thread, NULL, consumer, (void*) dq );
|
||
|
|
if (rv != 0) {
|
||
|
|
printf("Error creating producer thread\n");
|
||
|
|
exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
sleep(1);
|
||
|
|
|
||
|
|
rv = pthread_create( &producer_thread, NULL, producer, (void*) dq );
|
||
|
|
if (rv != 0) {
|
||
|
|
printf("Error creating producer thread\n");
|
||
|
|
exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
sleep(1);
|
||
|
|
|
||
|
|
printf("%s Waiting for threads to exit\n", __FUNCTION__);
|
||
|
|
pthread_join( producer_thread, NULL);
|
||
|
|
printf("%s joined with producer thread\n", __FUNCTION__);
|
||
|
|
pthread_join( consumer_thread, NULL);
|
||
|
|
printf("%s joined with consumer thread\n", __FUNCTION__);
|
||
|
|
}
|
||
|
|
|
||
|
|
int main()
|
||
|
|
{
|
||
|
|
struct duplex_queue *dq;
|
||
|
|
|
||
|
|
dq = dq_init(QUEUE_CAPACITY, 4);
|
||
|
|
|
||
|
|
threads(dq);
|
||
|
|
|
||
|
|
dq_free(dq);
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|