Skip to content

Commit

Permalink
Merge pull request #102 from pzaino/develop
Browse files Browse the repository at this point in the history
Cumulative set of changes merged from Develop to main branch
  • Loading branch information
pzaino authored Mar 18, 2022
2 parents 08e8c69 + fc74281 commit 2fe4ae5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,17 @@ LIBST:=$(LIBDIR)/lib$(LIBNAME).a
###############################################################################
# Targets:

# Use "make all" to build both core library and tests for production
.PHONY: all
all: CFLAGS += $(P_CFLAGS)
all: core test

# Use "make clean" to clean your previous builds
.PHONY: clean
clean:
$(RM) -r $(LIBDIR) $(OBJ) $(TESTDIR)/bin ./*.o

# Use "make configure" to set your configuration (useful to enable code in your IDE)
.PHONY: configure
configure: $(SRC)/$(LIBNAME)_config.h $(SCRIPTSDIR)/ux_set_extension
@echo ----------------------------------------------------------------
Expand All @@ -249,12 +252,14 @@ configure: $(SRC)/$(LIBNAME)_config.h $(SCRIPTSDIR)/ux_set_extension
$(RVAL5) || :
@echo ----------------------------------------------------------------

# Use "make core" to just build the library FOR PRODUCTION
.PHONY: core
core: configure $(LIBDIR) $(LIBST)

.PHONY: test
test: $(TESTDIR)/bin $(TESTBINS)

# Use "make tests" to build tests and run them
.PHONY: tests
tests: test
$(info )
Expand All @@ -263,11 +268,18 @@ tests: test
$(info ===========================)
for test in $(TESTBINS) ; do time ./$(TESTBIN)$$test ; done

# Use "make debug" to build code for gdb debugger
.PHONY: debug
debug: CFLAGS+= -ggdb3
debug: CODE_MACROS+= -DDEBUG
debug: core tests

# Use "make testing" to build code with sanitizers for testing purposes:
.PHONY: testing
testing: CFLAGS+= -ggdb3 -fsanitize=address -fsanitize=leak -fsanitize=undefined
testing: core tests

# Use "make install" to build and install the core library
.PHONY: install
install: core
sudo cp -f $(LIBST) $(DESTDIR)
Expand Down
17 changes: 8 additions & 9 deletions tests/02ITest004.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
#define MAX_MSG_SIZE 72

// Setup tests:
char *testGrp = "003";
uint8_t testID = 4;
char *testGrp = "004";
uint8_t testID = 1;

#if ( ZVECT_THREAD_SAFE == 1 ) && ( OS_TYPE == 1 )

Expand Down Expand Up @@ -139,21 +139,20 @@ void *consumer(void *arg) {

uint32_t i;
for (i = 0; i < MAX_ITEMS;) {
// For beginners: this is how in C we convert back a void * into the original dtata_type
QueueItem *item = (QueueItem *)malloc(sizeof(QueueItem *));
// For beginners: this is how in C we convert back a void *
// into the original data_type:
QueueItem *item = NULL; // We do not need to allocate item, because
// ZVector vect_remove_front will do it for us :)

int fetched_item= 0;

// Let's retrieve the value from the vector correctly:
//vect_lock(v);

if (!vect_is_empty(v))
{
item = (QueueItem *)vect_remove_front(v);
fetched_item=1;
}

//vect_unlock(v);

if ( fetched_item == 1 && item != NULL )
{
// Let's test if the value we have retrieved is correct:
Expand All @@ -164,7 +163,7 @@ void *consumer(void *arg) {
}

free(item);
// item = NULL;
item = NULL;
}

printf("Consumer thread %i done. Consumed %d events.\n", id, evt_counter);
Expand Down
39 changes: 21 additions & 18 deletions tests/04PTest005.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ void *producer(void *arg) {
qi.msg[max_strLen]='\0';

// Enqueue message on the local queue:
// (this will copy qi into v2, so no need
// to free qi after, we can simply reuse it)
vect_add(v2, &qi);
}

// Now merge the local partition to the shared vector:
//vect_lock(v);

// Now move the local partition to the shared vector:
vect_move(v, v2, 0, MAX_ITEMS);

// We're done, display some stats and terminate the thread:
Expand All @@ -157,7 +157,10 @@ void *producer(void *arg) {
#endif
fflush(stdout);

//vect_unlock(v);
// Given that we have moved all the references to our message
// set from v2 to v, we can safely destroy v2 and recover its
// memory:
vect_destroy(v2);

pthread_exit(NULL);
return NULL;
Expand Down Expand Up @@ -196,7 +199,6 @@ void *consumer(void *arg) {
//if (vect_trylock(v))
//if (vect_wait_for_signal(v))
//{
//vect_lock(v);
//if ( vect_size(v) >= MAX_ITEMS )
if (!vect_move_if(v2, v, 0, MAX_ITEMS, check_if_correct_size))
{
Expand All @@ -220,14 +222,22 @@ void *consumer(void *arg) {
START_JOB:
printf("--- Consumer Thread %*i received a chunk of %*i messages ---\n\n", 3, id, 4, vect_size(v2));

QueueItem *item = (QueueItem *)malloc(sizeof(QueueItem *));
QueueItem *item; // We do not need to allocate item,
// ZVector vect_remove_front will do it for us :)
evt_counter = 0;

for (i = 0; i < MAX_ITEMS; i++)
{
item = (QueueItem *)vect_remove_front(v2);
if ( item->msg != NULL )
{
evt_counter++;
if (i < MAX_ITEMS - 1)
{
free(item);
item = NULL;
}
}
#ifdef DEBUG
// printf("thread %*i, item %*u\n", 10, id, 8, evt_counter);
#endif
Expand All @@ -238,7 +248,9 @@ void *consumer(void *arg) {
printf("Last element in the queue for Consumer Thread %*i: ID (%*d) - Message: %s\n",
8, id, 8, item->eventID, item->msg);
#endif
free(item);

free(item);
item = NULL;

printf("Consumer thread %i done. Consumed %d events.\n", id, evt_counter);

Expand Down Expand Up @@ -285,7 +297,9 @@ int main() {
int err = 0;
int i = 0;
struct thread_args *targs[MAX_THREADS+1];

CCPAL_START_MEASURING;

for (i=0; i < MAX_THREADS / 2; i++) {
targs[i]=(struct thread_args *)malloc(sizeof(struct thread_args));
targs[i]->id=i;
Expand Down Expand Up @@ -322,20 +336,9 @@ int main() {
CCPAL_REPORT_ANALYSIS;
fflush(stdout);

// Allow the Producer and Consumer process
// To start properly:
//usleep(100);

printf("--- Events missed in the queue: %*i ---\n\n", 4, vect_size(v));
fflush(stdout);

// Now wait until the Queue is empty:
//while(!vect_is_empty(v))
// ;

//printf("--- Events missed in the queue: %*i ---\n", 4, vect_size(v));
//fflush(stdout);

printf("Test %s_%d: Delete all left over events (if any):\n", testGrp, testID);
fflush(stdout);

Expand Down

0 comments on commit 2fe4ae5

Please sign in to comment.