UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
wp_worker Struct Reference

This class represents a worker that is called from within the pool. More...

Collaboration diagram for wp_worker:
Collaboration graph

Public Member Functions

 wp_worker (worker_state_observer &observer)
 
 ~wp_worker ()
 
void * pop (wp_task_data *)
 
void push (wp_task_data *)
 
void run ()
 

Static Public Member Functions

static void * enter_loop (void *args)
 

Public Attributes

queue< wp_task_data * > m_data
 
pthread_mutex_t m_lock
 
worker_state_observer & m_state_observer
 
pthread_cond_t m_task_completed_cv
 
pthread_cond_t m_task_ready_cv
 
pthread_t m_thread_id
 

Detailed Description

This class represents a worker that is called from within the pool.

Definition at line 79 of file worker.cpp.

Constructor & Destructor Documentation

◆ wp_worker()

wp_worker::wp_worker ( worker_state_observer & observer)
inline

Definition at line 80 of file worker.cpp.

80 :
81 m_state_observer(observer)
82 {
83 int ret;
84 ret = pthread_mutex_init(&m_lock, NULL);
85 assert(ret == 0);
86 ret = pthread_cond_init(&m_task_ready_cv, NULL);
87 assert(ret == 0);
88 ret = pthread_cond_init(&m_task_completed_cv, NULL);
89 assert(ret == 0);
90
91 ret = pthread_create(&m_thread_id, NULL, wp_worker::enter_loop, this);
92 assert(ret == 0);
93 }
worker_state_observer & m_state_observer
Definition worker.cpp:115
pthread_cond_t m_task_ready_cv
Definition worker.cpp:111
pthread_t m_thread_id
Definition worker.cpp:113
pthread_cond_t m_task_completed_cv
Definition worker.cpp:112
static void * enter_loop(void *args)
Definition worker.cpp:118
pthread_mutex_t m_lock
Definition worker.cpp:110

References enter_loop(), m_lock, m_task_completed_cv, m_task_ready_cv, and m_thread_id.

Here is the call graph for this function:

◆ ~wp_worker()

wp_worker::~wp_worker ( )
inline

Definition at line 94 of file worker.cpp.

94 {
95 wp_task_data *poisoned = new wp_task_data(NULL, NULL, this, false);
96 this->push(poisoned);
97
98 pthread_join(m_thread_id, NULL);
99 pthread_mutex_destroy(&m_lock);
100 pthread_cond_destroy(&m_task_ready_cv);
101 pthread_cond_destroy(&m_task_completed_cv);
102 }
Holds data to be passed to worker.
Definition worker.cpp:65
void push(wp_task_data *)
Definition worker.cpp:157

References m_lock, m_task_completed_cv, m_task_ready_cv, m_thread_id, and push().

Here is the call graph for this function:

Member Function Documentation

◆ enter_loop()

void * wp_worker::enter_loop ( void * args)
static

Definition at line 118 of file worker.cpp.

118 {
119 set_thread_name("worker");
120 wp_worker *instance = (wp_worker *) args;
121 instance->run();
122
123 return NULL;
124}
This class represents a worker that is called from within the pool.
Definition worker.cpp:79
void set_thread_name(const char *name)
Definition thread.c:72
static class worker_pool instance
Definition worker.cpp:250

References instance, and set_thread_name().

Referenced by wp_worker().

Here is the call graph for this function:

◆ pop()

void * wp_worker::pop ( wp_task_data * d)

Definition at line 165 of file worker.cpp.

165 {
166 void *res = NULL;
167
168 pthread_mutex_lock(&m_lock);
169 while(!d->m_returned) {
170 pthread_cond_wait(&m_task_completed_cv, &m_lock);
171 }
172 res = d->m_result;
173 delete d;
174 pthread_mutex_unlock(&m_lock);
175
176 return res;
177}
void * m_result
Definition worker.cpp:70
bool m_returned
Definition worker.cpp:71

References m_lock, wp_task_data::m_result, wp_task_data::m_returned, and m_task_completed_cv.

Referenced by worker_pool::wait_task().

◆ push()

void wp_worker::push ( wp_task_data * data)

Definition at line 157 of file worker.cpp.

157 {
158 pthread_mutex_lock(&m_lock);
159 assert(m_data.size() == 0);
160 m_data.push(data);
161 pthread_mutex_unlock(&m_lock);
162 pthread_cond_signal(&m_task_ready_cv);
163}
queue< wp_task_data * > m_data
Definition worker.cpp:109

References m_data, m_lock, and m_task_ready_cv.

Referenced by ~wp_worker(), and worker_pool::run_async().

◆ run()

void wp_worker::run ( )

Definition at line 126 of file worker.cpp.

126 {
127 while(1) {
128 struct wp_task_data *data;
129 pthread_mutex_lock(&m_lock);
130 while(m_data.empty()) {
131 pthread_cond_wait(&m_task_ready_cv, &m_lock);
132 }
133 data = m_data.front();
134 m_data.pop();
135 pthread_mutex_unlock(&m_lock);
136
137 // poisoned pill
138 if(data->m_task == NULL) {
139 delete data;
140 return;
141 }
142
143 void *res = data->m_task(data->m_data);
144
145 pthread_mutex_lock(&m_lock);
146 data->m_result = res;
147 data->m_returned = true;
148 pthread_cond_signal(&m_task_completed_cv);
150 if (data->m_detached) {
151 delete data;
152 }
153 pthread_mutex_unlock(&m_lock);
154 }
155}
virtual void notify(wp_worker *)=0
void * m_data
Definition worker.cpp:69
bool m_detached
Definition worker.cpp:73
runnable_t m_task
Definition worker.cpp:68

References wp_task_data::m_data, m_data, wp_task_data::m_detached, m_lock, wp_task_data::m_result, wp_task_data::m_returned, m_state_observer, wp_task_data::m_task, m_task_completed_cv, m_task_ready_cv, and worker_state_observer::notify().

Here is the call graph for this function:

Member Data Documentation

◆ m_data

queue<wp_task_data*> wp_worker::m_data

Definition at line 109 of file worker.cpp.

Referenced by push(), and run().

◆ m_lock

pthread_mutex_t wp_worker::m_lock

Definition at line 110 of file worker.cpp.

Referenced by wp_worker(), ~wp_worker(), pop(), push(), and run().

◆ m_state_observer

worker_state_observer& wp_worker::m_state_observer

Definition at line 115 of file worker.cpp.

Referenced by run().

◆ m_task_completed_cv

pthread_cond_t wp_worker::m_task_completed_cv

Definition at line 112 of file worker.cpp.

Referenced by wp_worker(), ~wp_worker(), pop(), and run().

◆ m_task_ready_cv

pthread_cond_t wp_worker::m_task_ready_cv

Definition at line 111 of file worker.cpp.

Referenced by wp_worker(), ~wp_worker(), push(), and run().

◆ m_thread_id

pthread_t wp_worker::m_thread_id

Definition at line 113 of file worker.cpp.

Referenced by wp_worker(), and ~wp_worker().


The documentation for this struct was generated from the following file: