From 10a58cd7a060c4a774d3595cfff2bcb8aa604ecd Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Wed, 13 Feb 2019 16:02:16 -0500 Subject: [PATCH] TT#52651 strip streambuf into lib and include in recording daemon Change-Id: I1f6638961e9e767063e0b4e6b5d55d88799366d3 --- daemon/.gitignore | 1 + daemon/Makefile | 4 +- daemon/aux.c | 1 - include/aux.h | 130 -------------------------------- lib/auxlib.c | 1 + lib/auxlib.h | 142 +++++++++++++++++++++++++++++++++++ {daemon => lib}/streambuf.c | 7 +- {include => lib}/streambuf.h | 2 +- recording-daemon/.gitignore | 1 + recording-daemon/Makefile | 4 +- recording-daemon/poller.c | 9 +++ recording-daemon/poller.h | 12 +++ t/Makefile | 4 +- 13 files changed, 176 insertions(+), 142 deletions(-) rename {daemon => lib}/streambuf.c (97%) rename {include => lib}/streambuf.h (97%) create mode 100644 recording-daemon/poller.c create mode 100644 recording-daemon/poller.h diff --git a/daemon/.gitignore b/daemon/.gitignore index c648704e7..fb8f071b2 100644 --- a/daemon/.gitignore +++ b/daemon/.gitignore @@ -12,3 +12,4 @@ resample.c str.c fix_frame_channel_layout.h socket.c +streambuf.c diff --git a/daemon/Makefile b/daemon/Makefile index 151947e0a..2fc6273cd 100644 --- a/daemon/Makefile +++ b/daemon/Makefile @@ -119,12 +119,12 @@ ifeq ($(have_bcg729),yes) LDLIBS+= $(bcg729_lib) endif -SRCS= main.c kernel.c poller.c aux.c control_tcp.c streambuf.c call.c control_udp.c redis.c \ +SRCS= main.c kernel.c poller.c aux.c control_tcp.c call.c control_udp.c redis.c \ bencode.c cookie_cache.c udp_listener.c control_ng.strhash.c sdp.strhash.c stun.c rtcp.c \ crypto.c rtp.c call_interfaces.strhash.c dtls.c log.c cli.c graphite.c ice.c \ media_socket.c homer.c recording.c statistics.c cdr.c ssrc.c iptables.c tcp_listener.c \ codec.c load.c dtmf.c -LIBSRCS= loglib.c auxlib.c rtplib.c str.c socket.c +LIBSRCS= loglib.c auxlib.c rtplib.c str.c socket.c streambuf.c ifeq ($(with_transcoding),yes) LIBSRCS+= codeclib.c resample.c endif diff --git a/daemon/aux.c b/daemon/aux.c index f651d5576..8f6d36430 100644 --- a/daemon/aux.c +++ b/daemon/aux.c @@ -39,7 +39,6 @@ static GList *threads_to_join; static GList *threads_running; static cond_t threads_cond = COND_STATIC_INIT; -__thread struct timeval rtpe_now; volatile int rtpe_shutdown; #ifdef NEED_ATOMIC64_MUTEX diff --git a/include/aux.h b/include/aux.h index 4319fc677..75de0bc45 100644 --- a/include/aux.h +++ b/include/aux.h @@ -51,7 +51,6 @@ G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \ /*** GLOBALS ***/ -extern __thread struct timeval rtpe_now; extern volatile int rtpe_shutdown; @@ -254,135 +253,6 @@ void free_buf(char **); -/*** MUTEX ABSTRACTION ***/ - -typedef pthread_mutex_t mutex_t; -typedef pthread_rwlock_t rwlock_t; -typedef pthread_cond_t cond_t; - -#define mutex_init(m) __debug_mutex_init(m, __FILE__, __LINE__) -#define mutex_destroy(m) __debug_mutex_destroy(m, __FILE__, __LINE__) -#define mutex_lock(m) __debug_mutex_lock(m, __FILE__, __LINE__) -#define mutex_trylock(m) __debug_mutex_trylock(m, __FILE__, __LINE__) -#define mutex_unlock(m) __debug_mutex_unlock(m, __FILE__, __LINE__) -#define MUTEX_STATIC_INIT PTHREAD_MUTEX_INITIALIZER - -#define rwlock_init(l) __debug_rwlock_init(l, __FILE__, __LINE__) -#define rwlock_destroy(l) __debug_rwlock_destroy(l, __FILE__, __LINE__) -#define rwlock_lock_r(l) __debug_rwlock_lock_r(l, __FILE__, __LINE__) -#define rwlock_unlock_r(l) __debug_rwlock_unlock_r(l, __FILE__, __LINE__) -#define rwlock_lock_w(l) __debug_rwlock_lock_w(l, __FILE__, __LINE__) -#define rwlock_unlock_w(l) __debug_rwlock_unlock_w(l, __FILE__, __LINE__) - -#define cond_init(c) __debug_cond_init(c, __FILE__, __LINE__) -#define cond_wait(c,m) __debug_cond_wait(c,m, __FILE__, __LINE__) -#define cond_timedwait(c,m,t) __debug_cond_timedwait(c,m,t, __FILE__, __LINE__) -#define cond_signal(c) __debug_cond_signal(c, __FILE__, __LINE__) -#define cond_broadcast(c) __debug_cond_broadcast(c, __FILE__, __LINE__) -#define COND_STATIC_INIT PTHREAD_COND_INITIALIZER - -INLINE int __cond_timedwait_tv(cond_t *c, mutex_t *m, const struct timeval *tv) { - struct timespec ts; - ts.tv_sec = tv->tv_sec; - ts.tv_nsec = tv->tv_usec * 1000; - return pthread_cond_timedwait(c, m, &ts); -} - -#ifndef __THREAD_DEBUG - -#define __debug_mutex_init(m, F, L) pthread_mutex_init(m, NULL) -#define __debug_mutex_destroy(m, F, L) pthread_mutex_destroy(m) -#define __debug_mutex_lock(m, F, L) pthread_mutex_lock(m) -#define __debug_mutex_trylock(m, F, L) pthread_mutex_trylock(m) -#define __debug_mutex_unlock(m, F, L) pthread_mutex_unlock(m) - -#define __debug_rwlock_init(l, F, L) pthread_rwlock_init(l, NULL) -#define __debug_rwlock_destroy(l, F, L) pthread_rwlock_destroy(l) -#define __debug_rwlock_lock_r(l, F, L) pthread_rwlock_rdlock(l) -#define __debug_rwlock_unlock_r(l, F, L) pthread_rwlock_unlock(l) -#define __debug_rwlock_lock_w(l, F, L) pthread_rwlock_wrlock(l) -#define __debug_rwlock_unlock_w(l, F, L) pthread_rwlock_unlock(l) - -#define __debug_cond_init(c, F, L) pthread_cond_init(c, NULL) -#define __debug_cond_wait(c, m, F, L) pthread_cond_wait(c,m) -#define __debug_cond_timedwait(c, m, t, F, L) __cond_timedwait_tv(c,m,t) -#define __debug_cond_signal(c, F, L) pthread_cond_signal(c) -#define __debug_cond_broadcast(c, F, L) pthread_cond_broadcast(c) - -#else - - -#include "log.h" - - - -INLINE int __debug_mutex_init(mutex_t *m, const char *file, unsigned int line) { - write_log(LOG_DEBUG, "mutex_init(%p) at %s:%u", m, file, line); - return pthread_mutex_init(m, NULL); -} -INLINE int __debug_mutex_destroy(mutex_t *m, const char *file, unsigned int line) { - write_log(LOG_DEBUG, "mutex_destroy(%p) at %s:%u", m, file, line); - return pthread_mutex_destroy(m); -} -INLINE int __debug_mutex_lock(mutex_t *m, const char *file, unsigned int line) { - int ret; - write_log(LOG_DEBUG, "mutex_lock(%p) at %s:%u ...", m, file, line); - ret = pthread_mutex_lock(m); - write_log(LOG_DEBUG, "mutex_lock(%p) at %s:%u returning %i", m, file, line, ret); - return ret; -} -INLINE int __debug_mutex_trylock(mutex_t *m, const char *file, unsigned int line) { - int ret; - write_log(LOG_DEBUG, "mutex_trylock(%p) at %s:%u ...", m, file, line); - ret = pthread_mutex_trylock(m); - write_log(LOG_DEBUG, "mutex_trylock(%p) at %s:%u returning %i", m, file, line, ret); - return ret; -} -INLINE int __debug_mutex_unlock(mutex_t *m, const char *file, unsigned int line) { - write_log(LOG_DEBUG, "mutex_unlock(%p) at %s:%u", m, file, line); - return pthread_mutex_unlock(m); -} - -INLINE int __debug_rwlock_init(rwlock_t *m, const char *file, unsigned int line) { - write_log(LOG_DEBUG, "rwlock_init(%p) at %s:%u", m, file, line); - return pthread_rwlock_init(m, NULL); -} -INLINE int __debug_rwlock_destroy(rwlock_t *m, const char *file, unsigned int line) { - write_log(LOG_DEBUG, "rwlock_destroy(%p) at %s:%u", m, file, line); - return pthread_rwlock_destroy(m); -} -INLINE int __debug_rwlock_lock_r(rwlock_t *m, const char *file, unsigned int line) { - int ret; - write_log(LOG_DEBUG, "rwlock_lock_r(%p) at %s:%u ...", m, file, line); - ret = pthread_rwlock_rdlock(m); - write_log(LOG_DEBUG, "rwlock_lock_r(%p) at %s:%u returning %i", m, file, line, ret); - return ret; -} -INLINE int __debug_rwlock_lock_w(rwlock_t *m, const char *file, unsigned int line) { - int ret; - write_log(LOG_DEBUG, "rwlock_lock_w(%p) at %s:%u ...", m, file, line); - ret = pthread_rwlock_wrlock(m); - write_log(LOG_DEBUG, "rwlock_lock_w(%p) at %s:%u returning %i", m, file, line, ret); - return ret; -} -INLINE int __debug_rwlock_unlock_r(rwlock_t *m, const char *file, unsigned int line) { - write_log(LOG_DEBUG, "rwlock_unlock_r(%p) at %s:%u", m, file, line); - return pthread_rwlock_unlock(m); -} -INLINE int __debug_rwlock_unlock_w(rwlock_t *m, const char *file, unsigned int line) { - write_log(LOG_DEBUG, "rwlock_unlock_w(%p) at %s:%u", m, file, line); - return pthread_rwlock_unlock(m); -} - -#define __debug_cond_init(c, F, L) pthread_cond_init(c, NULL) -#define __debug_cond_wait(c, m, F, L) pthread_cond_wait(c,m) -#define __debug_cond_timedwait(c, m, t, F, L) __cond_timedwait_tv(c,m,t) -#define __debug_cond_signal(c, F, L) pthread_cond_signal(c) -#define __debug_cond_broadcast(c, F, L) pthread_cond_broadcast(c) - -#endif - - /*** THREAD HELPERS ***/ diff --git a/lib/auxlib.c b/lib/auxlib.c index 7884662a6..c2f729798 100644 --- a/lib/auxlib.c +++ b/lib/auxlib.c @@ -21,6 +21,7 @@ struct thread_buf { static int version; struct rtpengine_common_config *rtpe_common_config_ptr; +__thread struct timeval rtpe_now; static struct thread_buf __thread t_bufs[NUM_THREAD_BUFS]; static int __thread t_buf_idx; diff --git a/lib/auxlib.h b/lib/auxlib.h index 982dcd479..59f17b1e9 100644 --- a/lib/auxlib.h +++ b/lib/auxlib.h @@ -23,6 +23,16 @@ struct rtpengine_common_config { extern struct rtpengine_common_config *rtpe_common_config_ptr; + + +/*** GLOBALS ***/ + +extern __thread struct timeval rtpe_now; +extern volatile int rtpe_shutdown; + + + + /*** PROTOTYPES ***/ void daemonize(void); @@ -59,4 +69,136 @@ INLINE void random_string(unsigned char *buf, int len) { assert(ret == 1); } + +/*** MUTEX ABSTRACTION ***/ + +typedef pthread_mutex_t mutex_t; +typedef pthread_rwlock_t rwlock_t; +typedef pthread_cond_t cond_t; + +#define mutex_init(m) __debug_mutex_init(m, __FILE__, __LINE__) +#define mutex_destroy(m) __debug_mutex_destroy(m, __FILE__, __LINE__) +#define mutex_lock(m) __debug_mutex_lock(m, __FILE__, __LINE__) +#define mutex_trylock(m) __debug_mutex_trylock(m, __FILE__, __LINE__) +#define mutex_unlock(m) __debug_mutex_unlock(m, __FILE__, __LINE__) +#define MUTEX_STATIC_INIT PTHREAD_MUTEX_INITIALIZER + +#define rwlock_init(l) __debug_rwlock_init(l, __FILE__, __LINE__) +#define rwlock_destroy(l) __debug_rwlock_destroy(l, __FILE__, __LINE__) +#define rwlock_lock_r(l) __debug_rwlock_lock_r(l, __FILE__, __LINE__) +#define rwlock_unlock_r(l) __debug_rwlock_unlock_r(l, __FILE__, __LINE__) +#define rwlock_lock_w(l) __debug_rwlock_lock_w(l, __FILE__, __LINE__) +#define rwlock_unlock_w(l) __debug_rwlock_unlock_w(l, __FILE__, __LINE__) + +#define cond_init(c) __debug_cond_init(c, __FILE__, __LINE__) +#define cond_wait(c,m) __debug_cond_wait(c,m, __FILE__, __LINE__) +#define cond_timedwait(c,m,t) __debug_cond_timedwait(c,m,t, __FILE__, __LINE__) +#define cond_signal(c) __debug_cond_signal(c, __FILE__, __LINE__) +#define cond_broadcast(c) __debug_cond_broadcast(c, __FILE__, __LINE__) +#define COND_STATIC_INIT PTHREAD_COND_INITIALIZER + +INLINE int __cond_timedwait_tv(cond_t *c, mutex_t *m, const struct timeval *tv) { + struct timespec ts; + ts.tv_sec = tv->tv_sec; + ts.tv_nsec = tv->tv_usec * 1000; + return pthread_cond_timedwait(c, m, &ts); +} + +#ifndef __THREAD_DEBUG + +#define __debug_mutex_init(m, F, L) pthread_mutex_init(m, NULL) +#define __debug_mutex_destroy(m, F, L) pthread_mutex_destroy(m) +#define __debug_mutex_lock(m, F, L) pthread_mutex_lock(m) +#define __debug_mutex_trylock(m, F, L) pthread_mutex_trylock(m) +#define __debug_mutex_unlock(m, F, L) pthread_mutex_unlock(m) + +#define __debug_rwlock_init(l, F, L) pthread_rwlock_init(l, NULL) +#define __debug_rwlock_destroy(l, F, L) pthread_rwlock_destroy(l) +#define __debug_rwlock_lock_r(l, F, L) pthread_rwlock_rdlock(l) +#define __debug_rwlock_unlock_r(l, F, L) pthread_rwlock_unlock(l) +#define __debug_rwlock_lock_w(l, F, L) pthread_rwlock_wrlock(l) +#define __debug_rwlock_unlock_w(l, F, L) pthread_rwlock_unlock(l) + +#define __debug_cond_init(c, F, L) pthread_cond_init(c, NULL) +#define __debug_cond_wait(c, m, F, L) pthread_cond_wait(c,m) +#define __debug_cond_timedwait(c, m, t, F, L) __cond_timedwait_tv(c,m,t) +#define __debug_cond_signal(c, F, L) pthread_cond_signal(c) +#define __debug_cond_broadcast(c, F, L) pthread_cond_broadcast(c) + +#else + + +#include "log.h" + + + +INLINE int __debug_mutex_init(mutex_t *m, const char *file, unsigned int line) { + write_log(LOG_DEBUG, "mutex_init(%p) at %s:%u", m, file, line); + return pthread_mutex_init(m, NULL); +} +INLINE int __debug_mutex_destroy(mutex_t *m, const char *file, unsigned int line) { + write_log(LOG_DEBUG, "mutex_destroy(%p) at %s:%u", m, file, line); + return pthread_mutex_destroy(m); +} +INLINE int __debug_mutex_lock(mutex_t *m, const char *file, unsigned int line) { + int ret; + write_log(LOG_DEBUG, "mutex_lock(%p) at %s:%u ...", m, file, line); + ret = pthread_mutex_lock(m); + write_log(LOG_DEBUG, "mutex_lock(%p) at %s:%u returning %i", m, file, line, ret); + return ret; +} +INLINE int __debug_mutex_trylock(mutex_t *m, const char *file, unsigned int line) { + int ret; + write_log(LOG_DEBUG, "mutex_trylock(%p) at %s:%u ...", m, file, line); + ret = pthread_mutex_trylock(m); + write_log(LOG_DEBUG, "mutex_trylock(%p) at %s:%u returning %i", m, file, line, ret); + return ret; +} +INLINE int __debug_mutex_unlock(mutex_t *m, const char *file, unsigned int line) { + write_log(LOG_DEBUG, "mutex_unlock(%p) at %s:%u", m, file, line); + return pthread_mutex_unlock(m); +} + +INLINE int __debug_rwlock_init(rwlock_t *m, const char *file, unsigned int line) { + write_log(LOG_DEBUG, "rwlock_init(%p) at %s:%u", m, file, line); + return pthread_rwlock_init(m, NULL); +} +INLINE int __debug_rwlock_destroy(rwlock_t *m, const char *file, unsigned int line) { + write_log(LOG_DEBUG, "rwlock_destroy(%p) at %s:%u", m, file, line); + return pthread_rwlock_destroy(m); +} +INLINE int __debug_rwlock_lock_r(rwlock_t *m, const char *file, unsigned int line) { + int ret; + write_log(LOG_DEBUG, "rwlock_lock_r(%p) at %s:%u ...", m, file, line); + ret = pthread_rwlock_rdlock(m); + write_log(LOG_DEBUG, "rwlock_lock_r(%p) at %s:%u returning %i", m, file, line, ret); + return ret; +} +INLINE int __debug_rwlock_lock_w(rwlock_t *m, const char *file, unsigned int line) { + int ret; + write_log(LOG_DEBUG, "rwlock_lock_w(%p) at %s:%u ...", m, file, line); + ret = pthread_rwlock_wrlock(m); + write_log(LOG_DEBUG, "rwlock_lock_w(%p) at %s:%u returning %i", m, file, line, ret); + return ret; +} +INLINE int __debug_rwlock_unlock_r(rwlock_t *m, const char *file, unsigned int line) { + write_log(LOG_DEBUG, "rwlock_unlock_r(%p) at %s:%u", m, file, line); + return pthread_rwlock_unlock(m); +} +INLINE int __debug_rwlock_unlock_w(rwlock_t *m, const char *file, unsigned int line) { + write_log(LOG_DEBUG, "rwlock_unlock_w(%p) at %s:%u", m, file, line); + return pthread_rwlock_unlock(m); +} + +#define __debug_cond_init(c, F, L) pthread_cond_init(c, NULL) +#define __debug_cond_wait(c, m, F, L) pthread_cond_wait(c,m) +#define __debug_cond_timedwait(c, m, t, F, L) __cond_timedwait_tv(c,m,t) +#define __debug_cond_signal(c, F, L) pthread_cond_signal(c) +#define __debug_cond_broadcast(c, F, L) pthread_cond_broadcast(c) + +#endif + + + + #endif diff --git a/daemon/streambuf.c b/lib/streambuf.c similarity index 97% rename from daemon/streambuf.c rename to lib/streambuf.c index 073609fbe..26ff48118 100644 --- a/daemon/streambuf.c +++ b/lib/streambuf.c @@ -9,7 +9,7 @@ #include #include "poller.h" -#include "aux.h" +#include "auxlib.h" @@ -18,8 +18,7 @@ struct streambuf *streambuf_new(struct poller *p, int fd) { struct streambuf *b; - b = malloc(sizeof(*b)); - ZERO(*b); + b = g_slice_alloc0(sizeof(*b)); mutex_init(&b->lock); b->buf = g_string_new(""); @@ -33,7 +32,7 @@ struct streambuf *streambuf_new(struct poller *p, int fd) { void streambuf_destroy(struct streambuf *b) { g_string_free(b->buf, TRUE); - free(b); + g_slice_free1(sizeof(*b), b); } diff --git a/include/streambuf.h b/lib/streambuf.h similarity index 97% rename from include/streambuf.h rename to lib/streambuf.h index 0b79d95e2..72a4ae12a 100644 --- a/include/streambuf.h +++ b/lib/streambuf.h @@ -10,7 +10,7 @@ #include "compat.h" #include "str.h" -#include "aux.h" +#include "auxlib.h" diff --git a/recording-daemon/.gitignore b/recording-daemon/.gitignore index 13d93ffe2..ce030d02c 100644 --- a/recording-daemon/.gitignore +++ b/recording-daemon/.gitignore @@ -12,3 +12,4 @@ resample.c str.c fix_frame_channel_layout.h socket.c +streambuf.c diff --git a/recording-daemon/Makefile b/recording-daemon/Makefile index 246495bfd..95ed90cab 100644 --- a/recording-daemon/Makefile +++ b/recording-daemon/Makefile @@ -25,8 +25,8 @@ LDLIBS+= $(shell mysql_config --libs) LDLIBS+= $(shell pkg-config --libs openssl) SRCS= epoll.c garbage.c inotify.c main.c metafile.c stream.c recaux.c packet.c \ - decoder.c output.c mix.c db.c log.c forward.c tag.c -LIBSRCS= loglib.c auxlib.c rtplib.c codeclib.c resample.c str.c socket.c + decoder.c output.c mix.c db.c log.c forward.c tag.c poller.c +LIBSRCS= loglib.c auxlib.c rtplib.c codeclib.c resample.c str.c socket.c streambuf.c OBJS= $(SRCS:.c=.o) $(LIBSRCS:.c=.o) include ../lib/common.Makefile diff --git a/recording-daemon/poller.c b/recording-daemon/poller.c new file mode 100644 index 000000000..29b7c168b --- /dev/null +++ b/recording-daemon/poller.c @@ -0,0 +1,9 @@ +#include "poller.h" + +void poller_blocked(struct poller *p, int fd) { +} +int poller_isblocked(struct poller *p, int fd) { + return 0; +} +void poller_error(struct poller *p, int fd) { +} diff --git a/recording-daemon/poller.h b/recording-daemon/poller.h new file mode 100644 index 000000000..1bf240749 --- /dev/null +++ b/recording-daemon/poller.h @@ -0,0 +1,12 @@ +#ifndef __POLLER_H__ +#define __POLLER_H__ + + +struct poller; + +void poller_blocked(struct poller *, int); +int poller_isblocked(struct poller *, int); +void poller_error(struct poller *, int); + + +#endif diff --git a/t/Makefile b/t/Makefile index 549cfc058..f0f904f27 100644 --- a/t/Makefile +++ b/t/Makefile @@ -62,10 +62,10 @@ SRCS+= transcode-test.c ifeq ($(with_amr_tests),yes) SRCS+= amr-decode-test.c amr-encode-test.c endif -LIBSRCS+= codeclib.c resample.c socket.c +LIBSRCS+= codeclib.c resample.c socket.c streambuf.c DAEMONSRCS+= codec.c call.c ice.c kernel.c media_socket.c stun.c bencode.c poller.c \ dtls.c recording.c statistics.c rtcp.c redis.c iptables.c graphite.c \ - streambuf.c cookie_cache.c udp_listener.c homer.c load.c cdr.c dtmf.c + cookie_cache.c udp_listener.c homer.c load.c cdr.c dtmf.c HASHSRCS+= call_interfaces.c control_ng.c sdp.c endif