2016-06-05 04:54:17 +08:00
|
|
|
#include "tsmpool.h"
|
|
|
|
|
2017-01-11 17:48:25 +08:00
|
|
|
tsmpool::tsmpool(size_t size, int num) :
|
2017-01-11 03:21:30 +08:00
|
|
|
size(size),
|
|
|
|
num(num) //number of buffers of (size) to alloc
|
2016-06-04 23:42:29 +08:00
|
|
|
{
|
|
|
|
this->threads_cntr = 0;
|
|
|
|
this->ok = 1;
|
|
|
|
this->lowest_read_index = -1;
|
2016-06-05 05:05:38 +08:00
|
|
|
this->write_index = 0;
|
2017-01-12 22:27:48 +08:00
|
|
|
this->my_read_index = index_before(0);
|
2017-01-11 03:21:30 +08:00
|
|
|
if (pthread_mutex_init(&this->mutex, NULL) != 0) { this->ok = 0; return; }
|
|
|
|
for(int i=0; i<num; i++)
|
|
|
|
{
|
|
|
|
void* newptr = (void*)new char[size];
|
|
|
|
if(!newptr) { this->ok = 0; return; }
|
|
|
|
buffers.push_back(newptr);
|
|
|
|
}
|
2016-06-04 23:42:29 +08:00
|
|
|
}
|
|
|
|
|
2017-01-11 03:21:30 +08:00
|
|
|
int tsmpool::is_ok() { return this->ok; }
|
2016-06-04 23:42:29 +08:00
|
|
|
|
2016-06-05 04:54:17 +08:00
|
|
|
void* tsmpool::get_write_buffer()
|
|
|
|
{
|
2016-06-05 05:05:38 +08:00
|
|
|
//if(write_index==index_before(lowest_read_index)) return NULL;
|
2017-01-12 22:27:48 +08:00
|
|
|
pthread_mutex_lock(&this->mutex);
|
2016-06-04 23:42:29 +08:00
|
|
|
void* to_return = buffers[write_index];
|
2017-01-12 22:27:48 +08:00
|
|
|
write_index = index_next(write_index);
|
|
|
|
pthread_mutex_unlock(&this->mutex);
|
2017-01-13 00:45:59 +08:00
|
|
|
if(TSM_DEBUG) fprintf(stderr, "gwb: write_index = %d\n", write_index);
|
2017-01-12 22:27:48 +08:00
|
|
|
return to_return;
|
2016-06-04 23:42:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tsmthread_t* tsmpool::register_thread()
|
|
|
|
{
|
2016-06-05 04:54:17 +08:00
|
|
|
if(!ok) return NULL;
|
2016-06-04 23:42:29 +08:00
|
|
|
pthread_mutex_lock(&this->mutex);
|
2017-01-12 22:27:48 +08:00
|
|
|
tsmthread_t* thread = new tsmthread_t();
|
2016-06-05 05:05:38 +08:00
|
|
|
thread->read_index = index_before(write_index);
|
2016-06-04 23:42:29 +08:00
|
|
|
threads.push_back(thread);
|
|
|
|
pthread_mutex_unlock(&this->mutex);
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:37:14 +08:00
|
|
|
void tsmpool::remove_thread(tsmthread_t* thread)
|
2016-06-04 23:42:29 +08:00
|
|
|
{
|
|
|
|
pthread_mutex_lock(&this->mutex);
|
|
|
|
for(int i=0;i<threads.size();i++)
|
|
|
|
if(threads[i] == thread)
|
|
|
|
{
|
|
|
|
delete threads[i];
|
2017-01-11 17:48:25 +08:00
|
|
|
threads.erase(threads.begin()+i);
|
2016-06-04 23:42:29 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&this->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* tsmpool::get_read_buffer(tsmthread_t* thread)
|
|
|
|
{
|
2017-01-12 22:27:48 +08:00
|
|
|
pthread_mutex_lock(&this->mutex);
|
2016-06-08 04:14:36 +08:00
|
|
|
int* actual_read_index = (thread==NULL) ? &my_read_index : &thread->read_index;
|
2017-01-12 23:56:54 +08:00
|
|
|
if(*actual_read_index==index_before(write_index))
|
|
|
|
{
|
2017-01-13 00:45:59 +08:00
|
|
|
if(TSM_DEBUG) fprintf(stderr, "grb: fail,"
|
2017-01-12 23:56:54 +08:00
|
|
|
"read_index %d is just before write_index\n", *actual_read_index);
|
2017-01-13 00:45:59 +08:00
|
|
|
pthread_mutex_unlock(&this->mutex);
|
2017-01-12 23:56:54 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-06-08 04:14:36 +08:00
|
|
|
void* to_return = buffers[*actual_read_index];
|
|
|
|
*actual_read_index=index_next(*actual_read_index);
|
2017-01-12 22:27:48 +08:00
|
|
|
pthread_mutex_unlock(&this->mutex);
|
2017-01-13 00:45:59 +08:00
|
|
|
if(TSM_DEBUG) fprintf(stderr, "grb: read_index = %d\n", *actual_read_index);
|
2017-01-12 22:27:48 +08:00
|
|
|
return to_return;
|
2016-06-04 23:42:29 +08:00
|
|
|
}
|