Move thread data to workers

This is a step in the direction that any worker/thread related data
is the property of the worker/thread.
This commit is contained in:
Johan Wikman
2017-04-06 16:12:03 +03:00
parent 052975bccd
commit c11ca1c328
5 changed files with 71 additions and 58 deletions

View File

@ -28,6 +28,9 @@
using maxscale::Worker;
// TODO: Temporarily moved here.
THREAD_DATA *thread_data = NULL;
namespace
{
@ -126,9 +129,10 @@ void poll_resolve_error(int wid, int fd, int errornum, int op)
static bool modules_thread_init();
static void modules_thread_finish();
Worker::Worker(int id, int epoll_fd)
Worker::Worker(int id, int epoll_fd, THREAD_DATA* pThread_data)
: m_id(id)
, m_epoll_fd(epoll_fd)
, m_pThread_data(pThread_data)
, m_pQueue(NULL)
, m_thread(0)
, m_started(false)
@ -149,6 +153,18 @@ Worker::~Worker()
void Worker::init()
{
this_unit.n_workers = config_threadcount();
thread_data = (THREAD_DATA *)MXS_CALLOC(this_unit.n_workers, sizeof(THREAD_DATA));
if (!thread_data)
{
exit(-1);
}
for (int i = 0; i < this_unit.n_workers; i++)
{
thread_data[i].state = THREAD_STOPPED;
}
this_unit.ppWorkers = new (std::nothrow) Worker* [this_unit.n_workers] (); // Zero initialized array
if (!this_unit.ppWorkers)
@ -159,7 +175,7 @@ void Worker::init()
for (int i = 0; i < this_unit.n_workers; ++i)
{
Worker* pWorker = Worker::create(i);
Worker* pWorker = Worker::create(i, &thread_data[i]);
if (pWorker)
{
@ -314,7 +330,7 @@ bool should_shutdown(void* pData)
void Worker::run()
{
this_thread.current_worker_id = m_id;
poll_waitevents(m_epoll_fd, m_id, ::should_shutdown, this);
poll_waitevents(m_epoll_fd, m_id, m_pThread_data, ::should_shutdown, this);
this_thread.current_worker_id = WORKER_ABSENT_ID;
MXS_NOTICE("Worker %d has shut down.", m_id);
@ -374,12 +390,13 @@ void Worker::shutdown_all()
* - Creates a pipe.
* - Adds the read descriptor to the polling mechanism.
*
* @param worker_id The id of the worker.
* @param worker_id The id of the worker.
* @param pThread_data The thread data of the worker.
*
* @return A worker instance if successful, otherwise NULL.
*/
//static
Worker* Worker::create(int worker_id)
Worker* Worker::create(int worker_id, THREAD_DATA* pThread_data)
{
Worker* pThis = NULL;
@ -387,7 +404,7 @@ Worker* Worker::create(int worker_id)
if (epoll_fd != -1)
{
pThis = new (std::nothrow) Worker(worker_id, epoll_fd);
pThis = new (std::nothrow) Worker(worker_id, epoll_fd, pThread_data);
if (pThis)
{