ThreadPool * ThreadPool_new(uint16_t min_th_num, uint16_t max_th_num) { ThreadPool * new_th_pool; new_th_pool = NULL; new_th_pool = (ThreadPool*)safe_malloc(sizeof(ThreadPool)); new_th_pool->min_num = min_th_num; new_th_pool->max_num = max_th_num; new_th_pool->pool_stop_need = FALSE; new_th_pool->pool_wait_need = TRUE; new_th_pool->stop_wait_need = TRUE; new_th_pool->idle_queue = Queue_new(max_th_num * 2); new_th_pool->curt_queue = Queue_new(max_th_num * 2); new_th_pool->busy_threshold = BUSY_THRESHOLD; new_th_pool->manage_interval = MANAGE_INTERVAL; pthread_mutex_init(&new_th_pool->pool_lock, NULL); pthread_cond_init(&new_th_pool->pool_cond, NULL); pthread_mutex_init(&new_th_pool->stop_lock, NULL); pthread_cond_init(&new_th_pool->stop_cond, NULL); log_debug(log_cat, "threadPool new success"); return new_th_pool; }
int main( void) { if (!run_test(Queue_new(true))) return -1; if (!run_test(Queue_new(false))) return -1; return 0; }
int main(void) { const int MAX_QUEUE_SIZE = 7; Queue_t queue = Queue_new(); const char * dllName = userChoice(); dynamic_t * dll = dynamic_init(dllName); if (NULL == dll) { printf("Can't load dynamic!\n"); return 1; } if (NULL == dll->check) { printf("Can't get compare function!\n"); return 1; } if (NULL == dll->react) { printf("Can't get reaction function!\n"); return 1; } puts("Dynamic loaded!"); srand(time(NULL)); for (int i = 0; i < MAX_QUEUE_SIZE; i++) { int prec = !(rand() % 3) ? 1 + rand() % 15 : 0; printf("%dth day's precipitation: %d\n", i, prec); Queue_enqueue(queue, prec); } if(dll->check(queue)) dll->react(); Queue_delete(queue); return 0; }
int main() { // moduleTests_Queue(); Queue_t queue1 = Queue_new(); Queue_t queue2 = Queue_new(); for(int i = 0; i < 10; i++){ Queue_enqueue(queue1, randInt()); Queue_enqueue(queue2, randInt()); } Queue_bindQueues(queue1, queue2); Queue_subscribeSingleOverflowEvent(queue1, singleQueueOverflow1, "queue1"); Queue_subscribeSingleOverflowEvent(queue1, singleQueueOverflow2, "queue1"); Queue_subscribeSingleOverflowEvent(queue2, singleQueueOverflow1, "queue2"); Queue_subscribeSingleOverflowEvent(queue2, singleQueueOverflow2, "queue2"); Queue_subscribePairOverflowEvent(queue1, queuePairOverflow, "queue1"); Queue_subscribePairEmptyEvent(queue1, queuePairEmpty, "queue1"); Queue_subscribePairOverflowEvent(queue2, queuePairOverflow, "queue2"); Queue_subscribePairEmptyEvent(queue2, queuePairEmpty, "queue2"); for(int i = 0; i < 50; i++){ int rndInt = randInt(); if(randBool()){ if(rndInt >= 0){ Queue_enqueue(queue1, rndInt); }else{ Queue_dequeue(queue1); } }else{ if(rndInt >= 0){ Queue_enqueue(queue2, rndInt); }else{ Queue_dequeue(queue2); } } } Queue_delete(queue1); Queue_delete(queue2); return 0; }
Connection* Connection_new(ConnectionCallbacks* callbacks) { Connection* conn = cx_alloc(sizeof(Connection)); conn->callbacks = callbacks; conn->response_queue = Queue_new(); ((List*)conn->response_queue)->f_node_data_free = free_response; return conn; }
static int test_enqueue() { Queue* queue = Queue_new(0); int array[] = {1, 2, 3}; Queue_enqueue(queue, &array[0]); Queue_enqueue(queue, &array[1]); Queue_enqueue(queue, &array[2]); nu_assert_eq_int(3, Queue_size(queue)); Queue_free(queue); return 0; }
static int test_capacity() { Queue* queue = Queue_new(2); int array[] = {1, 2, 3}; nu_assert(Queue_enqueue(queue, &array[0])); nu_assert(Queue_enqueue(queue, &array[1])); nu_assert(!Queue_enqueue(queue, &array[2])); Queue_dequeue(queue); nu_assert(Queue_enqueue(queue, &array[2])); Queue_free(queue); return 0; }
static void test_Queue() { Queue* queue = Queue_new(); Consumer* consumers[NTHREADS_TOTAL]; int i_thread; for (i_thread = 0; i_thread < (NTHREADS_TOTAL / 2); i_thread++) consumers[i_thread] = Consumer_start(queue, i_thread, get_item); for (; i_thread < NTHREADS_TOTAL; i_thread++) consumers[i_thread] = Consumer_start(queue, i_thread, get_item_timed); PROFILE_BEGIN_FMT("Processing %d simple requests with %d threads\n", NITERATATIONS, NTHREADS_TOTAL); int i_item; int expected_sum = 0; for (i_item = 0; i_item < NITERATATIONS; i_item++) { int* x = cx_alloc(sizeof(int)); *x = i_item; Queue_add(queue, x); /* simulate submission delay */ expected_sum += i_item; #ifdef SUBMISSION_DELAY_MAX_MSEC usleep((rand() % SUBMISSION_DELAY_MAX_MSEC) * 1000); #endif } /* wait for threads to finish processing */ int total_processed = 0; for (i_thread = 0; i_thread < NTHREADS_TOTAL; i_thread++) { Consumer* consumer = consumers[i_thread]; // see http://stackoverflow.com/questions/5610677/valgrind-memory-leak-errors-when-using-pthread-create // http://stackoverflow.com/questions/5282099/signal-handling-in-pthreads pthread_join(*consumer->thread, NULL); XFLOG("Consumer[%d] processed %d", consumer->id, consumer->processed); total_processed += consumer->processed; Consumer_free(consumer); } PROFILE_END TEST_ASSERT_EQUAL(total_processed, NITERATATIONS); }
static int test_size() { Queue* queue = Queue_new(0); nu_assert_eq_int(0, Queue_size(queue)); int array[] = {1}; Queue_enqueue(queue, &array[0]); nu_assert_eq_int(1, Queue_size(queue)); Queue_dequeue(queue); nu_assert_eq_int(0, Queue_size(queue)); Queue_free(queue); return 0; }
static int test_empty() { Queue* queue = Queue_new(0); nu_assert(Queue_empty(queue)); int array[] = {1}; Queue_enqueue(queue, &array[0]); nu_assert(!Queue_empty(queue)); Queue_dequeue(queue); nu_assert(Queue_empty(queue)); Queue_free(queue); return 0; }
static int test_front_back() { Queue* queue = Queue_new(0); nu_assert_eq_ptr(NULL, Queue_front(queue)); nu_assert_eq_ptr(NULL, Queue_back(queue)); int array[] = {1, 2, 3}; Queue_enqueue(queue, &array[0]); Queue_enqueue(queue, &array[1]); Queue_enqueue(queue, &array[2]); nu_assert_eq_ptr(&array[0], Queue_front(queue)); nu_assert_eq_ptr(&array[2], Queue_back(queue)); nu_assert_eq_int(3, Queue_size(queue)); Queue_free(queue); return 0; }
int main() { gVar = (int *)malloc((sizeof(int))*CL*RTN); inittest(); gq = Queue_new(); t0 = clock(); printf("Starting Test 1 ...\n"); start_multi(0); wait_multi(); //t2 = clock(); //if ( checkresult != 0 ) // return -1; printf("TEST 1 , Multi In.\n"); printf(" PUT USE : %f seconds\n", (double)(t1 - t0) / CLOCKS_PER_SEC); //printf(" GET USE : %f seconds\n", (double)(t2 - t0) / CLOCKS_PER_SEC); t0 = clock(); printf("Starting Test 2 ...\n"); start_multi(1); wait_multi2(); //t1 = clock(); if ( checkresult() != 0 ) return -1; printf("TEST 2 , Multi Out.\n"); //printf(" PUT USE : %f seconds\n", (double)(t1 - t0) / CLOCKS_PER_SEC); printf(" GET USE : %f seconds\n", (double)(t2 - t0) / CLOCKS_PER_SEC); inittest(); gq = Queue_new(); t0 = clock(); printf("Starting Test 3 ...\n"); start_multi(0); single_get(); t2 = clock(); wait_multi(); if ( checkresult() != 0 ) return -1; printf("TEST 3 , Multi In Single Out.\n"); printf(" PUT USE : %f seconds\n", (double)(t1 - t0) / CLOCKS_PER_SEC); printf(" GET USE : %f seconds\n", (double)(t2 - t0) / CLOCKS_PER_SEC); inittest(); gq = Queue_new(); t0 = clock(); printf("Starting Test 4 ...\n"); single_put(); t1 = clock(); printf("TEST 4 , Single In .\n"); printf(" PUT USE : %f seconds\n", (double)(t1 - t0) / CLOCKS_PER_SEC); //printf(" GET USE : %f seconds\n", (double)(t2 - t0) / CLOCKS_PER_SEC); t0 = clock(); printf("Starting Test 5 ...\n"); start_multi(2); wait_multi2(); //t1 = clock(); if ( checkresult() != 0 ) return -1; printf("TEST 5 , Multi Out 2.\n"); //printf(" PUT USE : %f seconds\n", (double)(t1 - t0) / CLOCKS_PER_SEC); printf(" GET USE : %f seconds\n", (double)(t2 - t0) / CLOCKS_PER_SEC); inittest(); gq = Queue_new(); t0 = clock(); printf("Starting Test 6 ...\n"); start_multi(0); start_multi(1); wait_multi(); wait_multi2(); if ( checkresult() != 0 ) return -1; printf("TEST 6 , Multi In Multi Out.\n"); printf(" PUT USE : %f seconds\n", (double)(t1 - t0) / CLOCKS_PER_SEC); printf(" GET USE : %f seconds\n", (double)(t2 - t0) / CLOCKS_PER_SEC); inittest(); gq = Queue_new(); t0 = clock(); printf("Starting Test 7 ...\n"); start_multi(0); start_multi(2); wait_multi(); wait_multi2(); if ( checkresult() != 0 ) return -1; printf("TEST 7 , Multi In Multi Out 2.\n"); printf(" PUT USE : %f seconds\n", (double)(t1 - t0) / CLOCKS_PER_SEC); printf(" GET USE : %f seconds\n", (double)(t2 - t0) / CLOCKS_PER_SEC); return 0; }