Skip to content

Commit

Permalink
added on new feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Taymindis committed Sep 15, 2018
1 parent c60112a commit 343179c
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 105 deletions.
204 changes: 138 additions & 66 deletions example.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,96 @@
#include "lfstack.h"


typedef void (*test_function)(pthread_t*);

void one_push_and_multi_pop(pthread_t *threads);
void one_pop_and_multi_push(pthread_t *threads);
void multi_push_pop(pthread_t *threads);
void* worker_push_pop(void *);
void* worker_push(void *);
void* worker_pop(void *);
void* worker_single_pop(void *);
void* worker_pushingle_c(void *);

/**Testing must**/
void one_push_and_multi_pop_must(pthread_t *threads);
void one_pop_must_and_multi_push(pthread_t *threads);
void multi_push_pop_must(pthread_t *threads);
void* worker_push_pop_must(void *);
void* worker_push_must(void *);
void* worker_pop_must(void *);
void* worker_single_pop_must(void *);

void running_test(test_function testfn);

struct timeval tv1, tv2;
#define total_put 50000
int nthreads = 8; //sysconf(_SC_NPROCESSORS_ONLN); // Linux
#define total_running_loop 50
int nthreads = 4;
int one_thread = 1;
int nthreads_exited = 0;
lfstack_t *mystack;


void* worker_pop_must(void *arg) {
int i = 0;
int *int_data;
int total_loop = total_put * (*(int*)arg);
while (i++ < total_loop) {
/*Pop*/
int_data = lfstack_pop_must(mystack);
// printf("%d\n", *int_data);

free(int_data);
}
__sync_add_and_fetch(&nthreads_exited, 1);
return 0;
}

void* worker_single_pop_must(void *arg) {
int i = 0;
int *int_data;
int total_loop = total_put * (*(int*)arg);
while (i++ < total_loop) {
/*Pop*/
int_data = lfstack_single_pop_must(mystack);
// printf("%d\n", *int_data);

free(int_data);
}
__sync_add_and_fetch(&nthreads_exited, 1);
return 0;
}

void* worker_push_pop_must(void *arg)
{
int i = 0;
int *int_data;
while (i < total_put) {
int_data = (int*)malloc(sizeof(int));
assert(int_data != NULL);
*int_data = i++;
/*Push*/
while (lfstack_push(mystack, int_data)) {
printf("ENQ FULL?\n");
}

/*Pop*/
int_data = lfstack_pop_must(mystack);
// printf("%d\n", *int_data);
free(int_data);
}
__sync_add_and_fetch(&nthreads_exited, 1);
return 0;
}

void* worker_pop(void *arg) {
int i = 0;
int *int_data;
int total_loop = total_put * (*(int*)arg);
while (i++ < total_loop) {
/*Pop*/
while ((int_data = lfstack_pop(mystack)) == NULL) {

lfstack_sleep(1);
}
// printf("%d\n", *int_data);

Expand All @@ -40,15 +108,17 @@ void* worker_pop(void *arg) {
return 0;
}

void* worker_single_pop(void *arg) {
void* worker_pushingle_c(void *arg) {
int i = 0;
int *int_data;
int total_loop = total_put * (*(int*)arg);
while (i++ < total_loop) {
/*Pop*/
while ((int_data = lfstack_single_pop(mystack)) == NULL) {

lfstack_sleep(1);
}
// printf("%d\n", *int_data);

free(int_data);
}
__sync_add_and_fetch(&nthreads_exited, 1);
Expand All @@ -64,9 +134,10 @@ void* worker_push(void *arg)
int_data = (int*)malloc(sizeof(int));
assert(int_data != NULL);
*int_data = i;
/*Push*/

while (lfstack_push(mystack, int_data)) {
// printf("PUSH FULL?\n");
// printf("ENQ FULL?\n");
}
}
// __sync_add_and_fetch(&nthreads_exited, 1);
Expand All @@ -82,12 +153,14 @@ void* worker_push_pop(void *arg)
int_data = (int*)malloc(sizeof(int));
assert(int_data != NULL);
*int_data = i++;
/*Push*/
while (lfstack_push(mystack, int_data)) {
printf("PUSH FULL?\n");
printf("ENQ FULL?\n");
}

/*Pop*/
while ((int_data = lfstack_pop(mystack)) == NULL) {
// printf("POP EMPTY? %zu\n", lfstack_size(mystack));
lfstack_sleep(1);
}
// printf("%d\n", *int_data);
free(int_data);
Expand All @@ -97,18 +170,17 @@ void* worker_push_pop(void *arg)
}

#define join_threads \
for (i = 0; i < nthreads; i++)\
pthread_join(threads[i], NULL);\
printf("current size= %d\n", (int) lfstack_size(mystack) )
for (i = 0; i < nthreads; i++) {\
pthread_join(threads[i], NULL); \
}

#define detach_thread_and_loop \
for (i = 0; i < nthreads; i++)\
pthread_detach(threads[i]);\
while ( nthreads_exited < nthreads ) \
lfstack_sleep(2);\
lfstack_sleep(10);\
if(lfstack_size(mystack) != 0){\
lfstack_sleep(2);\
printf("current size= %zu\n", lfstack_size(mystack) );\
lfstack_sleep(10);\
}

void multi_push_pop(pthread_t *threads) {
Expand All @@ -121,13 +193,14 @@ void multi_push_pop(pthread_t *threads) {
join_threads;
// detach_thread_and_loop;
}

void one_pop_and_multi_push(pthread_t *threads) {
printf("-----------%s---------------\n", "one_pop_and_multi_push");
int i;
for (i = 0; i < nthreads; i++)
pthread_create(threads + i, NULL, worker_push, &one_thread);

worker_single_pop(&nthreads);
worker_pushingle_c(&nthreads);

join_threads;
// detach_thread_and_loop;
Expand All @@ -147,90 +220,89 @@ void one_push_and_multi_pop(pthread_t *threads) {
#pragma GCC diagnostic pop

}
int main(void)
{
int n;

mystack = malloc(sizeof (lfstack_t));

if (lfstack_init(mystack) == -1)
return -1;
void one_pop_must_and_multi_push(pthread_t *threads) {
printf("-----------%s---------------\n", "one_pop_must_and_multi_push");
int i;
for (i = 0; i < nthreads; i++)
pthread_create(threads + i, NULL, worker_push, &one_thread);

for (n = 0; n < 30; n++) {
printf("Current running at =%d, ", n);
nthreads_exited = 0;
worker_single_pop_must(&nthreads);

/* Spawn threads. */
pthread_t threads[nthreads];
printf("Using %d thread%s.\n", nthreads, nthreads == 1 ? "" : "s");
printf("Total requests %d \n", total_put);
gettimeofday(&tv1, NULL);
join_threads;
// detach_thread_and_loop;
}

multi_push_pop(threads);
void one_push_and_multi_pop_must(pthread_t *threads) {
printf("-----------%s---------------\n", "one_push_and_multi_pop_must");
int i;
for (i = 0; i < nthreads; i++)
pthread_create(threads + i, NULL, worker_pop_must, &one_thread);

gettimeofday(&tv2, NULL);
printf ("Total time = %f seconds\n",
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
(double) (tv2.tv_sec - tv1.tv_sec));
worker_push(&nthreads);

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
detach_thread_and_loop;
#pragma GCC diagnostic pop

//getchar();
lfstack_sleep(1);
assert ( 0 == lfstack_size(mystack) && "Error, all element should be pop out but not");
}

void multi_push_pop_must(pthread_t *threads) {
printf("-----------%s---------------\n", "multi_push_pop_must");
int i;
for (i = 0; i < nthreads; i++) {
pthread_create(threads + i, NULL, worker_push_pop_must, NULL);
}

for (n = 0; n < 30; n++) {
join_threads;
// detach_thread_and_loop;
}

void running_test(test_function testfn) {
int n;
for (n = 0; n < total_running_loop; n++) {
printf("Current running at =%d, ", n);
nthreads_exited = 0;

/* Spawn threads. */
pthread_t threads[nthreads];
printf("Using %d thread%s.\n", nthreads, nthreads == 1 ? "" : "s");
printf("Total requests %d \n", total_put);
gettimeofday(&tv1, NULL);

one_push_and_multi_pop(threads);
testfn(threads);

gettimeofday(&tv2, NULL);
printf ("Total time = %f seconds\n",
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
(double) (tv2.tv_sec - tv1.tv_sec));

//getchar();
lfstack_sleep(1);
assert ( 0 == lfstack_size(mystack) && "Error, all element should be pop out but not");
lfstack_sleep(10);
assert ( 0 == lfstack_size(mystack) && "Error, all stack should be consumed but not");
}
}

for (n = 0; n < 30; n++) {
printf("Current running at =%d, ", n);
nthreads_exited = 0;
int main(void) {
mystack = malloc(sizeof (lfstack_t));
if (lfstack_init(mystack) == -1)
return -1;

/* Spawn threads. */
pthread_t threads[nthreads];
printf("Using %d thread%s.\n", nthreads, nthreads == 1 ? "" : "s");
printf("Total requests %d \n", total_put);
gettimeofday(&tv1, NULL);
running_test(one_push_and_multi_pop);
running_test(one_push_and_multi_pop_must);

one_pop_and_multi_push(threads);
running_test(one_pop_and_multi_push);
running_test(one_pop_must_and_multi_push);

gettimeofday(&tv2, NULL);
printf ("Total time = %f seconds\n",
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
(double) (tv2.tv_sec - tv1.tv_sec));
running_test(multi_push_pop);
running_test(multi_push_pop_must);

//getchar();
lfstack_sleep(1);
assert ( 0 == lfstack_size(mystack) && "Error, all element should be pop out but not");
}

printf("Take a 4 seconds sleep \n");
sleep(4);
printf("Flush all the inactive memory \n");
lfstack_flush(mystack);
// printf("Check the memory usage, it should all flushed, press any key to exit \n");
// getchar();
lfstack_destroy(mystack);
// sleep(3);
free(mystack);

printf("Test Pass!\n");

return 0;
}
Expand Down
Loading

0 comments on commit 343179c

Please sign in to comment.