8000 tmr,main: thread safe tmr handling by sreimers · Pull Request #690 · baresip/re · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

tmr,main: thread safe tmr handling #690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/re_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void re_thread_async_cancel(intptr_t id);

void re_set_mutex(void *mutexp);

struct tmrl *re_tmrl_get(void);

/** Polling methods */
enum poll_method {
Expand Down
8 changes: 8 additions & 0 deletions include/re_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@
* Copyright (C) 2022 Sebastian Reimers
*/

#ifndef RE_H_THREAD__
#define RE_H_THREAD__

#if defined(HAVE_THREADS)
#include <threads.h>

#else

#if defined(WIN32)

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#define ONCE_FLAG_INIT INIT_ONCE_STATIC_INIT
typedef INIT_ONCE once_flag;
Expand Down Expand Up @@ -286,3 +292,5 @@ int mutex_alloc(mtx_t **mtx);
*/
int thread_create_name(thrd_t *thr, const char *name, thrd_start_t func,
void *arg);

#endif /* RE_H_THREAD__ */
11 changes: 8 additions & 3 deletions include/re_tmr.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,34 @@
*/


#include <re_thread.h>

/**
* Defines the timeout handler
*
* @param arg Handler argument
*/
typedef void (tmr_h)(void *arg);

struct tmrl;

/** Defines a timer */
struct tmr {
struct le le; /**< Linked list element */
mtx_t *lock; /**< Mutex lock */
tmr_h *th; /**< Timeout handler */
void *arg; /**< Handler argument */
uint64_t jfs; /**< Jiffies for timeout */
const char *file;
int line;
};


void tmr_poll(struct list *tmrl);
int tmrl_alloc(struct tmrl **tmrl);
void tmr_poll(struct tmrl *tmrl);
uint64_t tmr_jiffies_usec(void);
uint64_t tmr_jiffies(void);
uint64_t tmr_jiffies_rt_usec(void);
uint64_t tmr_next_timeout(struct list *tmrl);
uint64_t tmr_next_timeout(struct tmrl *tmrl);
void tmr_debug(void);
int tmr_status(struct re_printf *pf, void *unused);

Expand Down
25 changes: 15 additions & 10 deletions src/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
#include <re_mem.h>
#include <re_mbuf.h>
#include <re_list.h>
#include <re_thread.h>
#include <re_tmr.h>
#include <re_main.h>
#include <re_thread.h>
#include <re_btrace.h>
#include <re_atomic.h>
#include "main.h"
Expand Down Expand Up @@ -82,7 +82,7 @@ struct re {
bool update; /**< File descriptor set need updating */
RE_ATOMIC bool polling; /**< Is polling flag */
int sig; /**< Last caught signal */
struct list tmrl; /**< List of timers */
struct tmrl *tmrl; /**< List of timers */

#ifdef HAVE_EPOLL
struct epoll_event *events; /**< Event set for epoll() */
Expand Down Expand Up @@ -114,6 +114,7 @@ static void re_destructor(void *arg)
poll_close(re);
mem_deref(re->mutex);
mem_deref(re->async);
mem_deref(re->tmrl);
}


Expand Down Expand Up @@ -148,7 +149,12 @@ int re_alloc(struct re **rep)
}
re->mutexp = re->mutex;

list_init(&re->tmrl);
err = tmrl_alloc(&re->tmrl);
if (err) {
DEBUG_WARNING("thread_init: tmrl_alloc error\n");
goto out;
}

re->async = NULL;
re->tid = thrd_current();

Expand Down Expand Up @@ -668,7 +674,7 @@ void fd_close(re_sock_t fd)
*/
static int fd_poll(struct re *re)
{
const uint64_t to = tmr_next_timeout(&re->tmrl);
const uint64_t to = tmr_next_timeout(re->tmrl);
int i, n, index;
#ifdef HAVE_SELECT
fd_set rfds, wfds, efds;
Expand Down Expand Up @@ -1028,14 +1034,14 @@ int re_main(re_signal_h *signalh)
#endif
#ifdef WIN32
if (WSAEINVAL == err) {
tmr_poll(&re->tmrl);
tmr_poll(re->tmrl);
continue;
}
#endif
break;
}

tmr_poll(&re->tmrl);
tmr_poll(re->tmrl);
}
re_unlock(re);

Expand Down Expand Up @@ -1370,17 +1376,16 @@ int re_thread_check(void)
*
* @note only used by tmr module
*/
struct list *tmrl_get(void);
struct list *tmrl_get(void)
struct tmrl *re_tmrl_get(void)
{
struct re *re = re_get();

if (!re) {
DEBUG_WARNING("tmrl_get: re not ready\n");
DEBUG_WARNING("re_tmrl_get: re not ready\n");
return NULL;
}

return &re->tmrl;
return re->tmrl;
}


Expand Down
Loading
0