#include "ipfs/exchange/bitswap/engine.h" #include "ipfs/exchange/bitswap/wantlist_queue.h" #include "ipfs/exchange/bitswap/peer_request_queue.h" /*** * A separate thread that processes the queue * @param context the context */ void ipfs_bitswap_engine_wantlist_processor_start(void* ctx) { struct BitswapContext* context = (struct BitswapContext*)ctx; // the loop while (!context->bitswap_engine->shutting_down) { struct WantListQueueEntry* item = ipfs_bitswap_wantlist_queue_pop(context->localWantlist); if (item != NULL) { // if there is something on the queue process it. ipfs_bitswap__wantlist_process_entry(context, item); } else { // if there is nothing on the queue, wait... sleep(2); } } } /*** * A separate thread that processes the queue * @param context the context */ void ipfs_bitswap_engine_peer_request_processor_start(void* ctx) { struct BitswapContext* context = (struct BitswapContext*)ctx; // the loop while (!context->bitswap_engine->shutting_down) { struct BitswapPeerQueueEntry* item = ipfs_bitswap_peer_request_queue_pop(context->peerRequestQueue); if (item != NULL) { // if there is something on the queue process it. ipfs_bitswap_peer_request_process_entry(context, item); } else { // if there is nothing on the queue, wait... sleep(2); } } } /** * Starts the bitswap engine that processes queue items. There * should only be one of these per ipfs instance. * * @param context the context * @returns true(1) on success, false(0) otherwise */ int ipfs_bitswap_engine_start(const struct BitswapContext* context) { context->bitswap_engine->shutting_down = 0; // fire off the threads if (pthread_create(&context->bitswap_engine->wantlist_processor_thread, NULL, ipfs_bitswap_engine_wantlist_processor_start, (void*)context)) { return 0; } if (pthread_create(&context->bitswap_engine->peer_request_processor_thread, NULL, ipfs_bitswap_engine_peer_request_processor_start, (void*)context)) { ipfs_bitswap_engine_stop(context); return 0; } return 1; } /*** * Shut down the engine * @param context the context * @returns true(1) on success, false(0) otherwise */ int ipfs_bitswap_engine_stop(const struct BitswapContext* context) { context->bitswap_engine->shutting_down = 1; int error1 = pthread_join(context->bitswap_engine->wantlist_processor_thread); int error2 = pthread_join(context->bitswap_engine->peer_request_processor_thread); return !error1 && !error2; }