int main() { apr_thread_t* push_thds[1000]; apr_thread_t* pop_thds[1000]; apr_threadattr_t* thd_attr; apr_initialize(); apr_pool_create(&global_pool, NULL); apr_atomic_init(global_pool); frl_queue_create(&queue, global_pool, 1, FRL_LOCK_FREE); apr_threadattr_create(&thd_attr, global_pool); apr_time_t now = apr_time_now(); for (int i = 0; i < 100; i++) { apr_thread_create(&pop_thds[i], thd_attr, threadsafe_test_pop, (void*)i, global_pool); apr_thread_create(&push_thds[i], thd_attr, threadsafe_test_push, (void*)i, global_pool); } apr_status_t rv; for (int i = 0; i < 100; i++) { printf("Stop at %d\n", i+1); apr_thread_join(&rv, push_thds[i]); apr_thread_join(&rv, pop_thds[i]); } printf("Pass with %dus.\n", apr_time_now()-now); apr_terminate(); }
TCN_DECLARE(apr_pool_t *) tcn_get_global_pool() { if (!tcn_global_pool) { if (apr_pool_create(&tcn_global_pool, NULL) != APR_SUCCESS) { return NULL; } apr_atomic_init(tcn_global_pool); } return tcn_global_pool; }
TCN_IMPLEMENT_CALL(jboolean, Library, initialize)(TCN_STDARGS) { UNREFERENCED_STDARGS; if (!tcn_global_pool) { apr_initialize(); if (apr_pool_create(&tcn_global_pool, NULL) != APR_SUCCESS) { return JNI_FALSE; } apr_atomic_init(tcn_global_pool); } return JNI_TRUE; }
int main() { apr_pool_t* mempool; apr_initialize(); apr_pool_create(&mempool, NULL); apr_atomic_init(mempool); apr_sockaddr_t* sockaddr; apr_sockaddr_info_get(&sockaddr, "127.0.0.1", APR_INET, 8080, 0, mempool); HttpServer* hs = new HttpServer(100, FRL_LOCK_FREE, mempool); hs->spawn(5, 10, sockaddr); hs->wait(); apr_terminate(); return 0; }
int main(int argc, const char **argv) { apr_status_t status; apr_pool_t *pool; apr_sockaddr_t *address; serf_context_t *context; serf_connection_t *connection; app_baton_t app_ctx; handler_baton_t *handler_ctx; apr_uri_t url; const char *raw_url, *method; int count; apr_getopt_t *opt; char opt_c; char *authn = NULL; const char *opt_arg; /* For the parser threads */ apr_thread_t *thread[3]; apr_threadattr_t *tattr; apr_status_t parser_status; parser_baton_t *parser_ctx; apr_initialize(); atexit(apr_terminate); apr_pool_create(&pool, NULL); apr_atomic_init(pool); /* serf_initialize(); */ /* Default to one round of fetching. */ count = 1; /* Default to GET. */ method = "GET"; apr_getopt_init(&opt, pool, argc, argv); while ((status = apr_getopt(opt, "a:hv", &opt_c, &opt_arg)) == APR_SUCCESS) { int srclen, enclen; switch (opt_c) { case 'a': srclen = strlen(opt_arg); enclen = apr_base64_encode_len(srclen); authn = apr_palloc(pool, enclen + 6); strcpy(authn, "Basic "); (void) apr_base64_encode(&authn[6], opt_arg, srclen); break; case 'h': print_usage(pool); exit(0); break; case 'v': puts("Serf version: " SERF_VERSION_STRING); exit(0); default: break; } } if (opt->ind != opt->argc - 1) { print_usage(pool); exit(-1); } raw_url = argv[opt->ind]; apr_uri_parse(pool, raw_url, &url); if (!url.port) { url.port = apr_uri_port_of_scheme(url.scheme); } if (!url.path) { url.path = "/"; } if (strcasecmp(url.scheme, "https") == 0) { app_ctx.using_ssl = 1; } else { app_ctx.using_ssl = 0; } status = apr_sockaddr_info_get(&address, url.hostname, APR_UNSPEC, url.port, 0, pool); if (status) { printf("Error creating address: %d\n", status); exit(1); } context = serf_context_create(pool); /* ### Connection or Context should have an allocator? */ app_ctx.bkt_alloc = serf_bucket_allocator_create(pool, NULL, NULL); app_ctx.ssl_ctx = NULL; app_ctx.authn = authn; connection = serf_connection_create(context, address, conn_setup, &app_ctx, closed_connection, &app_ctx, pool); handler_ctx = (handler_baton_t*)serf_bucket_mem_alloc(app_ctx.bkt_alloc, sizeof(handler_baton_t)); handler_ctx->allocator = app_ctx.bkt_alloc; handler_ctx->doc_queue = apr_array_make(pool, 1, sizeof(doc_path_t*)); handler_ctx->doc_queue_alloc = app_ctx.bkt_alloc; handler_ctx->requests_outstanding = (apr_uint32_t*)serf_bucket_mem_alloc(app_ctx.bkt_alloc, sizeof(apr_uint32_t)); apr_atomic_set32(handler_ctx->requests_outstanding, 0); handler_ctx->hdr_read = 0; parser_ctx = (void*)serf_bucket_mem_alloc(app_ctx.bkt_alloc, sizeof(parser_baton_t)); parser_ctx->requests_outstanding = handler_ctx->requests_outstanding; parser_ctx->connection = connection; parser_ctx->app_ctx = &app_ctx; parser_ctx->doc_queue = handler_ctx->doc_queue; parser_ctx->doc_queue_alloc = handler_ctx->doc_queue_alloc; /* Restrict ourselves to this host. */ parser_ctx->hostinfo = url.hostinfo; status = apr_thread_mutex_create(&parser_ctx->mutex, APR_THREAD_MUTEX_DEFAULT, pool); if (status) { printf("Couldn't create mutex %d\n", status); return status; } status = apr_thread_cond_create(&parser_ctx->condvar, pool); if (status) { printf("Couldn't create condvar: %d\n", status); return status; } /* Let the handler now which condvar to use. */ handler_ctx->doc_queue_condvar = parser_ctx->condvar; apr_threadattr_create(&tattr, pool); /* Start the parser thread. */ apr_thread_create(&thread[0], tattr, parser_thread, parser_ctx, pool); /* Deliver the first request. */ create_request(url.hostinfo, url.path, NULL, NULL, parser_ctx, pool); /* Go run our normal thread. */ while (1) { int tries = 0; status = serf_context_run(context, SERF_DURATION_FOREVER, pool); if (APR_STATUS_IS_TIMEUP(status)) continue; if (status) { char buf[200]; printf("Error running context: (%d) %s\n", status, apr_strerror(status, buf, sizeof(buf))); exit(1); } /* We run this check to allow our parser threads to add more * requests to our queue. */ for (tries = 0; tries < 3; tries++) { if (!apr_atomic_read32(handler_ctx->requests_outstanding)) { #ifdef SERF_VERBOSE printf("Waiting..."); #endif apr_sleep(100000); #ifdef SERF_VERBOSE printf("Done\n"); #endif } else { break; } } if (tries >= 3) { break; } /* Debugging purposes only! */ serf_debug__closed_conn(app_ctx.bkt_alloc); } printf("Quitting...\n"); serf_connection_close(connection); /* wake up the parser via condvar signal */ apr_thread_cond_signal(parser_ctx->condvar); status = apr_thread_join(&parser_status, thread[0]); if (status) { printf("Error joining thread: %d\n", status); return status; } serf_bucket_mem_free(app_ctx.bkt_alloc, handler_ctx->requests_outstanding); serf_bucket_mem_free(app_ctx.bkt_alloc, parser_ctx); apr_pool_destroy(pool); return 0; }
static void test_init(abts_case *tc, void *data) { APR_ASSERT_SUCCESS(tc, "Could not initliaze atomics", apr_atomic_init(p)); }
SWITCH_DECLARE(switch_status_t) switch_atomic_init(switch_memory_pool_t *pool) { return apr_atomic_init((apr_pool_t *) pool); }
void s_init(apr_pool_t *ppool, apr_pool_t **pool) { apr_pool_create(pool, ppool); apr_atomic_init(*pool); }
int main() { apr_initialize(); apr_pool_create(&mempool, NULL); apr_atomic_init(mempool); frl_radix_tree_create(&tree, mempool, 16, 1000, FRL_LOCK_FREE); frl_md5 key_cache[500000]; for (int i = 0; i < 500000; i++) key_cache[i] = frl_md5((char*)&i, 4); apr_time_t time = apr_time_now(); int a[] = {10, 11, 12, 13, 22}; for (int i = 0; i < 500000; i++) frl_radix_tree_add(tree, key_cache[i].digest, a+i%5); time = apr_time_now()-time; printf("added 500000 key-value pairs to radix tree in %d microsecond.\n", time); time = apr_time_now(); for (int i = 0; i < 500000; i++) { frl_radix_tree_entry_t* entry = frl_radix_tree_get(tree, key_cache[i].digest); if (entry->pointer != a+i%5) printf("unexpected error in looking up.\n"); } time = apr_time_now()-time; printf("looked up 500000 key-value pairs in radix tree in %d microsecond.\n", time); time = apr_time_now(); for (int i = 0; i < 500000; i++) { frl_radix_tree_entry_t* entry = frl_radix_tree_get(tree, key_cache[i].digest); if (entry->pointer != a+i%5) printf("unexpected error in looking up.\n"); apr_status_t status = frl_radix_tree_remove(entry); if (status != APR_SUCCESS) printf("unexpected error in removing.\n"); } time = apr_time_now()-time; printf("removed 500000 key-value pairs to radix tree in %d microsecond.\n", time); apr_hash_t* apr_hash = apr_hash_make(mempool); time = apr_time_now(); for (int i = 0; i < 500000; i++) apr_hash_set(apr_hash, key_cache[i].digest, 16, a+i%5); time = apr_time_now()-time; printf("added 500000 key-value pairs to apr hash table in %d microsecond.\n", time); time = apr_time_now(); for (int i = 0; i < 500000; i++) { void* entry = apr_hash_get(apr_hash, key_cache[i].digest, 16); if (entry != a+i%5) printf("unexpected error in looking up.\n"); } time = apr_time_now()-time; printf("looked up 500000 key-value pairs in apr hash table in %d microsecond.\n", time); /* apr_table_t* apr_table = apr_table_make(mempool, 500000); time = apr_time_now(); char table_data[] = "oh my god!\0"; for (int i = 0; i < 500000; i++) { apr_byte_t q[23]; key_cache[i].base64_encode(q); apr_table_set(apr_table, (char*)q, table_data); } time = apr_time_now()-time; printf("added 500000 key-value pairs to apr table in %d microsecond.\n", time); time = apr_time_now(); for (int i = 0; i < 500000; i++) { apr_byte_t q[23]; key_cache[i].base64_encode(q); const void* entry = apr_table_get(apr_table, (char*)q); } time = apr_time_now()-time; printf("looked up 500000 key-value pairs in apr table in %d microsecond.\n", time); */ std::map<frl_md5, int> map; time = apr_time_now(); for (int i = 0; i < 500000; i++) map[key_cache[i]] = a[i%5]; time = apr_time_now()-time; printf("added 500000 key-value pairs to map in %d microsecond\n", time); time = apr_time_now(); for (int i = 0; i < 500000; i++) { int n = map[key_cache[i]]; if (n != a[i%5]) printf("unexpected error in looking up.\n"); } time = apr_time_now()-time; printf("looked up 500000 key-value pairs in map in %d microsecond\n", time); time = apr_time_now(); for (int i = 0; i < 500000; i++) map.erase(key_cache[i]); time = apr_time_now()-time; printf("looked up 500000 key-value pairs in map in %d microsecond\n", time); apr_terminate(); return 0; }