Commit 7b81a1dd authored by jehan's avatar jehan

improve mainloop thread safety

parent 912c1257
......@@ -90,6 +90,26 @@ BELLESIP_EXPORT belle_sip_source_t* belle_sip_main_loop_create_timeout(belle_sip
, unsigned int timeout_value_ms
,const char* timer_name);
/**
* Adds a timeout into the main loop
* The caller of this function is responsible for freeing (with belle_sip_object_unref()) the returned belle_sip_source_t object when it is no longer
* needed.
* @param ml
* @param func a callback function to be called to notify timeout expiration
* @param data a pointer to be passed to the callback
* @param timeout_value_ms duration of the timeout.
* @param timer_name name of the timer, can be null
* @param function called when source is removed, can be null
* @returns timeout belle_sip_source_t with ref count = 1
**/
BELLESIP_EXPORT belle_sip_source_t* belle_sip_main_loop_create_timeout_with_remove_cb (belle_sip_main_loop_t *ml
, belle_sip_source_func_t func
, void *data
, unsigned int timeout_value_ms
, const char* timer_name
, belle_sip_source_remove_callback_t remove_func);
/**
* Schedule an arbitrary task at next main loop iteration.
**/
......
......@@ -338,16 +338,28 @@ void belle_sip_main_loop_add_source(belle_sip_main_loop_t *ml, belle_sip_source_
ml->nsources++;
}
belle_sip_source_t* belle_sip_main_loop_create_timeout(belle_sip_main_loop_t *ml
, belle_sip_source_func_t func
, void *data
, unsigned int timeout_value_ms
,const char* timer_name) {
belle_sip_source_t* belle_sip_main_loop_create_timeout_with_remove_cb( belle_sip_main_loop_t *ml
, belle_sip_source_func_t func
, void *data
, unsigned int timeout_value_ms
, const char* timer_name
, belle_sip_source_remove_callback_t remove_func) {
belle_sip_source_t * s=belle_sip_timeout_source_new(func,data,timeout_value_ms);
belle_sip_object_set_name((belle_sip_object_t*)s,timer_name);
if (remove_func) {
belle_sip_source_set_remove_cb(s, remove_func);
}
belle_sip_main_loop_add_source(ml,s);
return s;
}
belle_sip_source_t* belle_sip_main_loop_create_timeout(belle_sip_main_loop_t *ml
, belle_sip_source_func_t func
, void *data
, unsigned int timeout_value_ms
,const char* timer_name) {
return belle_sip_main_loop_create_timeout_with_remove_cb(ml, func, data, timeout_value_ms,timer_name,NULL);
}
unsigned long belle_sip_main_loop_add_timeout(belle_sip_main_loop_t *ml, belle_sip_source_func_t func, void *data, unsigned int timeout_value_ms){
belle_sip_source_t * s=belle_sip_main_loop_create_timeout(ml,func,data,timeout_value_ms,"Timer");
......@@ -506,6 +518,8 @@ static void belle_sip_main_loop_iterate(belle_sip_main_loop_t *ml){
}
/* Step 3: find timeouted sources */
bctbx_mutex_lock(&ml->timer_sources_mutex); /*iterator chain might be alterated by element insertion*/
it = bctbx_map_begin(ml->timer_sources);
end = bctbx_map_end(ml->timer_sources);
while (!bctbx_iterator_equals(it,end)) {
......@@ -527,6 +541,7 @@ static void belle_sip_main_loop_iterate(belle_sip_main_loop_t *ml){
}
bctbx_iterator_delete(it);
bctbx_iterator_delete(end);
bctbx_mutex_unlock(&ml->timer_sources_mutex);
/* Step 4: notify those to be notified */
for(elem=to_be_notified;elem!=NULL;){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment