2024-12-01 19:35:24 +00:00
|
|
|
//| Copyright: (C) 2020-2024 Kevin Larke <contact AT larke DOT org>
|
|
|
|
//| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
|
2024-02-25 21:19:59 +00:00
|
|
|
#include "cwCommon.h"
|
|
|
|
#include "cwLog.h"
|
|
|
|
#include "cwCommonImpl.h"
|
2024-05-29 16:36:57 +00:00
|
|
|
#include "cwTest.h"
|
2024-02-25 21:19:59 +00:00
|
|
|
#include "cwMem.h"
|
|
|
|
#include "cwTime.h"
|
|
|
|
#include "cwObject.h"
|
|
|
|
|
|
|
|
#include "cwNbMpScQueue.h"
|
|
|
|
|
|
|
|
#include "cwThread.h"
|
|
|
|
#include "cwThreadMach.h"
|
2024-03-11 23:21:03 +00:00
|
|
|
#include "cwFile.h"
|
2024-02-25 21:19:59 +00:00
|
|
|
|
|
|
|
namespace cw
|
|
|
|
{
|
|
|
|
namespace nbmpscq
|
|
|
|
{
|
|
|
|
typedef struct block_str
|
|
|
|
{
|
|
|
|
uint8_t* buf; // buf[ bufByteN ]
|
2024-03-11 23:21:03 +00:00
|
|
|
unsigned bufByteN;
|
2024-02-25 21:19:59 +00:00
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
std::atomic<bool> full_flag; // Set if this block is full (i.e. index >= bufByteN)
|
|
|
|
std::atomic<unsigned> index; // Offset to next avail byte in buf[]
|
|
|
|
std::atomic<int> eleN; // Current count of elements stored in buf[]
|
2024-02-25 21:19:59 +00:00
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
struct block_str* link; //
|
2024-02-25 21:19:59 +00:00
|
|
|
|
|
|
|
} block_t;
|
|
|
|
|
|
|
|
typedef struct node_str
|
|
|
|
{
|
2024-03-25 17:31:58 +00:00
|
|
|
std::atomic<struct node_str*> next; // 0
|
|
|
|
block_t* block; // 8
|
|
|
|
unsigned blobByteN; // 16
|
|
|
|
unsigned pad; // 20-24 (mult. of 8)
|
2024-02-25 21:19:59 +00:00
|
|
|
// blob data follows
|
|
|
|
} node_t;
|
|
|
|
|
2024-03-25 17:31:58 +00:00
|
|
|
static_assert( sizeof(node_t) % 8 == 0 );
|
|
|
|
|
2024-02-25 21:19:59 +00:00
|
|
|
typedef struct nbmpscq_str
|
|
|
|
{
|
2024-03-11 23:21:03 +00:00
|
|
|
uint8_t* mem; // Pointer to a single area of memory which holds all blocks.
|
|
|
|
unsigned blkN; // Count of blocks in blockL
|
|
|
|
unsigned blkByteN; // Size of each block_t.mem[] buffer
|
2024-02-25 21:19:59 +00:00
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
block_t* blockL; // Linked list of blocks
|
2024-03-03 21:35:23 +00:00
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
std::atomic<int> cleanBlkN; // count of blocks that need to be cleaned
|
2024-03-03 21:35:23 +00:00
|
|
|
unsigned cleanProcN; // count of times the clear process has been run
|
2024-02-25 21:19:59 +00:00
|
|
|
|
|
|
|
node_t* stub; // dummy node
|
|
|
|
std::atomic<node_t*> head; // last-in
|
2024-03-11 23:21:03 +00:00
|
|
|
|
2024-02-25 21:19:59 +00:00
|
|
|
node_t* tail; // first-out
|
2024-03-25 17:31:58 +00:00
|
|
|
|
|
|
|
node_t* peek;
|
2024-02-25 21:19:59 +00:00
|
|
|
|
|
|
|
} nbmpscq_t;
|
|
|
|
|
|
|
|
nbmpscq_t* _handleToPtr( handle_t h )
|
|
|
|
{ return handleToPtr<handle_t,nbmpscq_t>(h); }
|
|
|
|
|
|
|
|
rc_t _destroy( nbmpscq_t* p )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
if( p != nullptr )
|
|
|
|
{
|
2024-03-25 17:31:58 +00:00
|
|
|
|
|
|
|
block_t* b = p->blockL;
|
|
|
|
while( b != nullptr )
|
|
|
|
{
|
|
|
|
block_t* b0 = b->link;
|
|
|
|
mem::release(b->buf);
|
|
|
|
mem::release(b);
|
|
|
|
b=b0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-25 21:19:59 +00:00
|
|
|
mem::release(p->stub);
|
|
|
|
mem::release(p);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
// _clean() is run by the consumer thread to make empty blocks available.
|
2024-02-25 21:19:59 +00:00
|
|
|
void _clean( nbmpscq_t* p )
|
|
|
|
{
|
|
|
|
block_t* b = p->blockL;
|
2024-03-11 23:21:03 +00:00
|
|
|
// for each block
|
2024-02-25 21:19:59 +00:00
|
|
|
for(; b!=nullptr; b=b->link)
|
|
|
|
{
|
2024-03-11 23:21:03 +00:00
|
|
|
// if this block is full ...
|
2024-02-25 21:19:59 +00:00
|
|
|
if( b->full_flag.load(std::memory_order_acquire) )
|
|
|
|
{
|
2024-03-11 23:21:03 +00:00
|
|
|
// ... and there are no more elements to be read from the block
|
2024-02-25 21:19:59 +00:00
|
|
|
if( b->eleN.load(std::memory_order_acquire) <= 0 )
|
|
|
|
{
|
2024-03-11 23:21:03 +00:00
|
|
|
// decr. the cleanBlkN count
|
2024-03-03 21:35:23 +00:00
|
|
|
unsigned cc = p->cleanBlkN.fetch_add(-1,std::memory_order_relaxed);
|
2024-02-25 21:19:59 +00:00
|
|
|
assert(cc>=1);
|
|
|
|
|
|
|
|
// Note: b->full_flag==true and p->eleN==0 so it is safe to reset the block
|
|
|
|
// because all elements have been removed (eleN==0) and
|
|
|
|
// no other threads will be accessing it (full_flag==true)
|
|
|
|
b->eleN.store(0,std::memory_order_relaxed);
|
|
|
|
b->index.store(0,std::memory_order_relaxed);
|
|
|
|
b->full_flag.store(false,std::memory_order_release);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-03 21:35:23 +00:00
|
|
|
p->cleanProcN += 1;
|
2024-02-25 21:19:59 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 17:31:58 +00:00
|
|
|
void _init_blob( blob_t& b, node_t* node )
|
|
|
|
{
|
|
|
|
if( node == nullptr )
|
|
|
|
{
|
|
|
|
b.blob = nullptr;
|
|
|
|
b.blobByteN = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b.blob = (uint8_t*)(node+1);
|
|
|
|
b.blobByteN = node->blobByteN;
|
|
|
|
}
|
|
|
|
|
|
|
|
b.rc = kOkRC;
|
|
|
|
|
|
|
|
}
|
2024-02-25 21:19:59 +00:00
|
|
|
|
|
|
|
typedef struct shared_str
|
|
|
|
{
|
|
|
|
handle_t qH;
|
|
|
|
std::atomic<unsigned> cnt;
|
|
|
|
|
|
|
|
} test_share_t;
|
|
|
|
|
|
|
|
typedef struct test_str
|
|
|
|
{
|
|
|
|
unsigned id; // thread id
|
|
|
|
unsigned iter; // execution counter
|
2024-03-11 23:21:03 +00:00
|
|
|
unsigned value; //
|
|
|
|
test_share_t* share; // pointer to global shared data
|
2024-02-25 21:19:59 +00:00
|
|
|
} test_t;
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
bool _test_threadFunc( void* arg )
|
2024-02-25 21:19:59 +00:00
|
|
|
{
|
|
|
|
test_t* t = (test_t*)arg;
|
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
// get and increment a global shared counter
|
2024-02-25 21:19:59 +00:00
|
|
|
t->value = t->share->cnt.fetch_add(1,std::memory_order_acq_rel);
|
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
// push the current thread instance record
|
2024-03-03 21:35:23 +00:00
|
|
|
push(t->share->qH,t,sizeof(test_t));
|
2024-03-11 23:21:03 +00:00
|
|
|
|
|
|
|
// incrmemen this threads exec. counter
|
2024-02-25 21:19:59 +00:00
|
|
|
t->iter += 1;
|
|
|
|
|
|
|
|
sleepMs( rand() & 0xf );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2024-03-28 23:43:40 +00:00
|
|
|
|
|
|
|
void _block_report( nbmpscq_t* p )
|
|
|
|
{
|
|
|
|
block_t* b = p->blockL;
|
|
|
|
for(; b!=nullptr; b=b->link)
|
|
|
|
{
|
|
|
|
bool full_fl = b->full_flag.load(std::memory_order_acquire);
|
|
|
|
unsigned index = b->index.load(std::memory_order_acquire);
|
|
|
|
int eleN = b->eleN.load(std::memory_order_acquire);
|
|
|
|
|
|
|
|
printf("full:%i idx:%i eleN:%i\n",full_fl,index,eleN);
|
|
|
|
}
|
|
|
|
}
|
2024-02-25 21:19:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cw::rc_t cw::nbmpscq::create( handle_t& hRef, unsigned initBlkN, unsigned blkByteN )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
nbmpscq_t* p = nullptr;
|
|
|
|
|
|
|
|
if((rc = destroy(hRef)) != kOkRC )
|
|
|
|
goto errLabel;
|
|
|
|
|
|
|
|
p = mem::allocZ<nbmpscq_t>();
|
|
|
|
|
|
|
|
p->stub = mem::allocZ<node_t>();
|
|
|
|
p->head = p->stub; // last-in
|
2024-03-25 17:31:58 +00:00
|
|
|
p->tail = p->stub; // first-out
|
|
|
|
p->peek = nullptr;
|
2024-03-03 21:35:23 +00:00
|
|
|
p->cleanBlkN = 0;
|
2024-02-25 21:19:59 +00:00
|
|
|
|
|
|
|
p->blkN = initBlkN;
|
|
|
|
p->blkByteN = blkByteN;
|
2024-03-25 17:31:58 +00:00
|
|
|
|
|
|
|
for(unsigned i=0; i<initBlkN; ++i)
|
|
|
|
{
|
|
|
|
block_t* b = mem::allocZ<block_t>();
|
|
|
|
b->buf = mem::allocZ<uint8_t>(blkByteN);
|
|
|
|
|
|
|
|
b->bufByteN = blkByteN;
|
|
|
|
|
|
|
|
b->full_flag.store(false);
|
|
|
|
b->index.store(0);
|
|
|
|
b->eleN.store(0);
|
|
|
|
|
|
|
|
b->link = p->blockL;
|
|
|
|
p->blockL = b;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-02-25 21:19:59 +00:00
|
|
|
hRef.set(p);
|
|
|
|
|
|
|
|
errLabel:
|
|
|
|
if(rc != kOkRC )
|
|
|
|
{
|
|
|
|
rc = cwLogError(rc,"NbMpScQueue destroy failed.");
|
|
|
|
_destroy(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
cw::rc_t cw::nbmpscq::destroy( handle_t& hRef )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
if(!hRef.isValid())
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
nbmpscq_t* p = _handleToPtr(hRef);
|
|
|
|
|
|
|
|
if((rc = _destroy(p)) != kOkRC )
|
|
|
|
goto errLabel;
|
|
|
|
|
|
|
|
hRef.clear();
|
|
|
|
errLabel:
|
|
|
|
|
|
|
|
if( rc != kOkRC )
|
|
|
|
rc = cwLogError(rc,"NbMpScQueue destroy failed.");
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cw::rc_t cw::nbmpscq::push( handle_t h, const void* blob, unsigned blobByteN )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
nbmpscq_t* p = _handleToPtr(h);
|
|
|
|
block_t* b = p->blockL;
|
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
// TODO: handle case where blobByteN is greater than p->blkByteN
|
|
|
|
// Note that this case will immediately overflow the queue.
|
|
|
|
|
2024-02-25 21:19:59 +00:00
|
|
|
unsigned nodeByteN = blobByteN + sizeof(node_t);
|
2024-03-25 17:31:58 +00:00
|
|
|
|
|
|
|
// force the size of the node to be a multiple of 8
|
|
|
|
nodeByteN = ((nodeByteN-1) & 0xfffffff8) + 8;
|
|
|
|
|
|
|
|
// We will eventually be addressing node_t records stored in pre-allocated blocks
|
|
|
|
// of memory - be sure that they always begin on 8 byte alignment to conform
|
|
|
|
// to Intel standard.
|
|
|
|
assert( nodeByteN % 8 == 0 );
|
|
|
|
|
2024-03-28 23:43:40 +00:00
|
|
|
if( nodeByteN > p->blkByteN )
|
|
|
|
return cwLogError(kInvalidArgRC,"The blob size is too large:%i > %i.",nodeByteN,p->blkByteN);
|
2024-02-25 21:19:59 +00:00
|
|
|
|
|
|
|
for(; b!=nullptr; b=b->link)
|
|
|
|
{
|
2024-03-03 21:35:23 +00:00
|
|
|
if( b->full_flag.load(std::memory_order_acquire) == false )
|
2024-02-25 21:19:59 +00:00
|
|
|
{
|
2024-03-11 23:21:03 +00:00
|
|
|
// attempt to allocate nodeByteN bytes starting at b->index
|
2024-02-25 21:19:59 +00:00
|
|
|
unsigned idx = b->index.fetch_add(nodeByteN, std::memory_order_acq_rel);
|
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
// if the allocation was not valid
|
2024-02-25 21:19:59 +00:00
|
|
|
if( idx >= b->bufByteN || idx+nodeByteN > b->bufByteN )
|
|
|
|
{
|
2024-03-11 23:21:03 +00:00
|
|
|
// incr the 'clean count' and mark the block as full
|
2024-03-03 21:35:23 +00:00
|
|
|
p->cleanBlkN.fetch_add(1,std::memory_order_relaxed);
|
2024-02-25 21:19:59 +00:00
|
|
|
b->full_flag.store(true,std::memory_order_release);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-11 23:21:03 +00:00
|
|
|
// otherwise this thread owns the allocated block
|
2024-02-25 21:19:59 +00:00
|
|
|
node_t* n = (node_t*)(b->buf + idx);
|
|
|
|
n->blobByteN = blobByteN;
|
|
|
|
n->block = b;
|
|
|
|
|
|
|
|
memcpy(b->buf+idx+sizeof(node_t),blob,blobByteN);
|
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
// incr the block element count
|
2024-03-03 21:35:23 +00:00
|
|
|
b->eleN.fetch_add(1,std::memory_order_release);
|
|
|
|
|
2024-02-25 21:19:59 +00:00
|
|
|
n->next.store(nullptr);
|
|
|
|
// Note that the elements of the queue are only accessed from the end of the queue (tail).
|
|
|
|
// New nodes can therefore safely be updated in two steps:
|
|
|
|
|
|
|
|
// 1. Atomically set _head to the new node and return 'old-head'
|
|
|
|
node_t* prev = p->head.exchange(n,std::memory_order_acq_rel);
|
|
|
|
|
|
|
|
// Note that at this point only the new node may have the 'old-head' as it's predecssor.
|
|
|
|
// Other threads may therefore safely interrupt at this point.
|
|
|
|
|
|
|
|
// 2. Set the old-head next pointer to the new node (thereby adding the new node to the list)
|
|
|
|
prev->next.store(n,std::memory_order_release); // RELEASE 'next' to consumer
|
2024-03-03 21:35:23 +00:00
|
|
|
|
|
|
|
break;
|
2024-02-25 21:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-11 23:21:03 +00:00
|
|
|
|
|
|
|
// if there is no space left in the queue to store the incoming blob
|
|
|
|
if( b == nullptr )
|
|
|
|
{
|
|
|
|
// TODO: continue to iterate through the blocks waiting for the consumer
|
|
|
|
// to make more space available.
|
2024-03-28 23:43:40 +00:00
|
|
|
//_block_report(p);
|
|
|
|
|
|
|
|
// BEWARE: BUG BUG BUG: Since the cwLog makes calls to cwWebSocket
|
|
|
|
// this error message, and subsequent error messages,
|
|
|
|
// will result in a recursive loop which will crash the program.
|
2024-03-11 23:21:03 +00:00
|
|
|
rc = cwLogError(kBufTooSmallRC,"NbMpScQueue overflow.");
|
2024-03-28 23:43:40 +00:00
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
}
|
2024-02-25 21:19:59 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
cw::nbmpscq::blob_t cw::nbmpscq::get( handle_t h )
|
2024-02-25 21:19:59 +00:00
|
|
|
{
|
2024-03-25 17:31:58 +00:00
|
|
|
blob_t blob;
|
2024-03-28 23:43:40 +00:00
|
|
|
nbmpscq_t* p = _handleToPtr(h);
|
|
|
|
|
|
|
|
// We always access the tail element through tail->next.
|
|
|
|
node_t* node = p->tail->next.load(std::memory_order_acquire); // ACQUIRE 'next' from producer
|
2024-03-25 17:31:58 +00:00
|
|
|
|
|
|
|
_init_blob( blob, node );
|
2024-02-25 21:19:59 +00:00
|
|
|
|
2024-03-25 17:31:58 +00:00
|
|
|
return blob;
|
2024-02-25 21:19:59 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 17:31:58 +00:00
|
|
|
cw::nbmpscq::blob_t cw::nbmpscq::advance( handle_t h )
|
|
|
|
{
|
|
|
|
blob_t blob;
|
|
|
|
nbmpscq_t* p = _handleToPtr(h);
|
|
|
|
node_t* t = p->tail;
|
2024-02-25 21:19:59 +00:00
|
|
|
|
2024-03-28 23:43:40 +00:00
|
|
|
// We always access the tail element through tail->next.
|
|
|
|
node_t* next = t->next.load(std::memory_order_acquire); // ACQUIRE 'next' from producer
|
|
|
|
|
|
|
|
// We always leave the last element on the queue to act as 'stub'.
|
2024-02-25 21:19:59 +00:00
|
|
|
if( next != nullptr )
|
|
|
|
{
|
|
|
|
p->tail = next;
|
|
|
|
|
2024-03-28 23:43:40 +00:00
|
|
|
// first 'stub' will not have a valid block pointer
|
|
|
|
if( t->block != nullptr )
|
|
|
|
{
|
|
|
|
int eleN = t->block->eleN.fetch_add(-1,std::memory_order_acq_rel);
|
2024-02-25 21:19:59 +00:00
|
|
|
|
2024-03-28 23:43:40 +00:00
|
|
|
// next was valid and so eleN must be >= 1
|
|
|
|
assert( eleN >= 1 );
|
|
|
|
}
|
2024-02-25 21:19:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-03-03 21:35:23 +00:00
|
|
|
if( p->cleanBlkN.load(std::memory_order_relaxed) > 0 )
|
2024-02-25 21:19:59 +00:00
|
|
|
_clean(p);
|
|
|
|
|
2024-03-25 17:31:58 +00:00
|
|
|
|
|
|
|
_init_blob(blob,next);
|
|
|
|
|
|
|
|
return blob;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cw::nbmpscq::blob_t cw::nbmpscq::peek( handle_t h )
|
|
|
|
{
|
|
|
|
blob_t blob;
|
|
|
|
nbmpscq_t* p = _handleToPtr(h);
|
|
|
|
node_t* n = p->peek;
|
|
|
|
|
|
|
|
// if p->peek is not set ...
|
|
|
|
if( n == nullptr )
|
|
|
|
{
|
|
|
|
// ... then set it to the tail
|
|
|
|
n = p->tail->next.load(std::memory_order_acquire); // ACQUIRE 'next' from producer
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_init_blob(blob,n);
|
|
|
|
|
|
|
|
if( n != nullptr )
|
|
|
|
p->peek = n->next.load(std::memory_order_acquire);
|
|
|
|
|
|
|
|
return blob;
|
2024-03-28 23:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ::cw::nbmpscq::peek_reset(handle_t h)
|
|
|
|
{
|
|
|
|
nbmpscq_t* p = _handleToPtr(h);
|
|
|
|
p->peek = nullptr;
|
2024-03-25 17:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cw::nbmpscq::is_empty( handle_t h )
|
|
|
|
{
|
|
|
|
nbmpscq_t* p = _handleToPtr(h);
|
|
|
|
|
|
|
|
node_t* t = p->tail;
|
|
|
|
node_t* next = t->next.load(std::memory_order_acquire); // ACQUIRE 'next' from producer
|
|
|
|
|
|
|
|
return next == nullptr;
|
2024-02-25 21:19:59 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 18:29:25 +00:00
|
|
|
unsigned cw::nbmpscq::count( handle_t h )
|
|
|
|
{
|
|
|
|
nbmpscq_t* p = _handleToPtr(h);
|
|
|
|
|
|
|
|
block_t* b = p->blockL;
|
|
|
|
int eleN = 0;
|
|
|
|
for(; b!=nullptr; b=b->link)
|
|
|
|
eleN += b->eleN.load(std::memory_order_acquire);
|
|
|
|
|
|
|
|
return eleN;
|
|
|
|
}
|
|
|
|
|
2024-03-03 21:35:23 +00:00
|
|
|
cw::rc_t cw::nbmpscq::test( const object_t* cfg )
|
2024-02-25 21:19:59 +00:00
|
|
|
{
|
|
|
|
rc_t rc=kOkRC,rc0,rc1;
|
|
|
|
|
2024-03-03 21:35:23 +00:00
|
|
|
unsigned testArrayN = 2;
|
|
|
|
test_t* testArray = nullptr;
|
|
|
|
unsigned blkN = 2;
|
|
|
|
unsigned blkByteN = 1024;
|
2024-03-11 23:21:03 +00:00
|
|
|
const char* out_fname = nullptr;
|
2024-02-25 21:19:59 +00:00
|
|
|
time::spec_t t0 = time::current_time();
|
2024-03-03 21:35:23 +00:00
|
|
|
unsigned testDurMs = 0;
|
2024-02-25 21:19:59 +00:00
|
|
|
test_share_t share;
|
|
|
|
handle_t qH;
|
|
|
|
thread_mach::handle_t tmH;
|
2024-03-11 23:21:03 +00:00
|
|
|
file::handle_t fH;
|
2024-03-03 21:35:23 +00:00
|
|
|
|
|
|
|
if((rc = cfg->getv("blkN",blkN,
|
|
|
|
"blkByteN",blkByteN,
|
|
|
|
"testDurMs",testDurMs,
|
2024-03-11 23:21:03 +00:00
|
|
|
"threadN",testArrayN,
|
|
|
|
"out_fname",out_fname)) != kOkRC )
|
2024-03-03 21:35:23 +00:00
|
|
|
{
|
|
|
|
rc = cwLogError(rc,"Test params parse failed.");
|
|
|
|
goto errLabel;
|
|
|
|
}
|
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
if((rc = file::open(fH,out_fname,file::kWriteFl)) != kOkRC )
|
|
|
|
{
|
|
|
|
rc = cwLogError(rc,"Error creating the output file:%s",cwStringNullGuard(out_fname));
|
|
|
|
goto errLabel;
|
|
|
|
}
|
|
|
|
|
2024-03-03 21:35:23 +00:00
|
|
|
if( testArrayN == 0 )
|
|
|
|
{
|
|
|
|
rc = cwLogError(kInvalidArgRC,"The 'threadN' parameter must be greater than 0.");
|
|
|
|
goto errLabel;
|
|
|
|
}
|
2024-03-11 23:21:03 +00:00
|
|
|
|
|
|
|
// create the thread intance records
|
2024-03-03 21:35:23 +00:00
|
|
|
testArray = mem::allocZ<test_t>(testArrayN);
|
2024-02-25 21:19:59 +00:00
|
|
|
|
|
|
|
// create the queue
|
|
|
|
if((rc = create( qH, blkN, blkByteN )) != kOkRC )
|
|
|
|
{
|
|
|
|
rc = cwLogError(rc,"nbmpsc create failed.");
|
|
|
|
goto errLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
share.qH = qH;
|
2024-03-03 21:35:23 +00:00
|
|
|
share.cnt.store(0);
|
2024-02-25 21:19:59 +00:00
|
|
|
|
|
|
|
for(unsigned i=0; i<testArrayN; ++i)
|
|
|
|
{
|
|
|
|
testArray[i].id = i;
|
|
|
|
testArray[i].share = &share;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the thread machine
|
2024-03-11 23:21:03 +00:00
|
|
|
if((rc = thread_mach::create( tmH, _test_threadFunc, testArray, sizeof(test_t), testArrayN )) != kOkRC )
|
2024-02-25 21:19:59 +00:00
|
|
|
{
|
|
|
|
rc = cwLogError(rc,"Thread machine create failed.");
|
|
|
|
goto errLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start the thread machine
|
|
|
|
if((rc = thread_mach::start(tmH)) != kOkRC )
|
|
|
|
{
|
|
|
|
cwLogError(rc,"Thread machine start failed.");
|
|
|
|
goto errLabel;
|
|
|
|
}
|
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
// run the test for 'testDurMs' milliseconds
|
2024-03-03 21:35:23 +00:00
|
|
|
while( time::elapsedMs(t0) < testDurMs )
|
2024-02-25 21:19:59 +00:00
|
|
|
{
|
2024-03-11 23:21:03 +00:00
|
|
|
blob_t b = get(qH);
|
2024-02-25 21:19:59 +00:00
|
|
|
if( b.blob != nullptr )
|
|
|
|
{
|
|
|
|
test_t* t = (test_t*)b.blob;
|
2024-03-11 23:21:03 +00:00
|
|
|
printf(fH,"%i %i %i %i\n",t->id,t->iter,t->value,b.blobByteN);
|
2024-02-25 21:19:59 +00:00
|
|
|
advance(qH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errLabel:
|
2024-03-11 23:21:03 +00:00
|
|
|
file::close(fH);
|
|
|
|
|
2024-02-25 21:19:59 +00:00
|
|
|
if((rc0 = thread_mach::destroy(tmH)) != kOkRC )
|
|
|
|
cwLogError(rc0,"Thread machine destroy failed.");
|
|
|
|
|
|
|
|
if((rc1 = destroy(qH)) != kOkRC )
|
|
|
|
cwLogError(rc1,"nbmpsc queue destroy failed.");
|
|
|
|
|
2024-03-03 21:35:23 +00:00
|
|
|
if( testArray != nullptr )
|
|
|
|
printf("P:%i %i\n",testArray[0].iter, testArray[1].iter);
|
2024-02-25 21:19:59 +00:00
|
|
|
|
2024-03-11 23:21:03 +00:00
|
|
|
// TODO: read back the file and verify that none of the
|
|
|
|
// global incrment values were dropped.
|
|
|
|
|
2024-03-03 21:35:23 +00:00
|
|
|
mem::release(testArray);
|
|
|
|
|
2024-02-25 21:19:59 +00:00
|
|
|
return rcSelect(rc,rc0,rc1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|