diff --git a/core/AmB2BMedia.cpp b/core/AmB2BMedia.cpp index b57a846a..288aee21 100644 --- a/core/AmB2BMedia.cpp +++ b/core/AmB2BMedia.cpp @@ -44,7 +44,7 @@ void B2BMediaStatistics::incCodecWriteUsage(const string &codec_name) { if (codec_name.empty()) return; - AmLock lock(mutex); + lock_guard lock(mutex); map::iterator i = codec_write_usage.find(codec_name); if (i != codec_write_usage.end()) i->second++; else codec_write_usage[codec_name] = 1; @@ -54,7 +54,7 @@ void B2BMediaStatistics::decCodecWriteUsage(const string &codec_name) { if (codec_name.empty()) return; - AmLock lock(mutex); + lock_guard lock(mutex); map::iterator i = codec_write_usage.find(codec_name); if (i != codec_write_usage.end()) { if (i->second > 0) i->second--; @@ -65,7 +65,7 @@ void B2BMediaStatistics::incCodecReadUsage(const string &codec_name) { if (codec_name.empty()) return; - AmLock lock(mutex); + lock_guard lock(mutex); map::iterator i = codec_read_usage.find(codec_name); if (i != codec_read_usage.end()) i->second++; else codec_read_usage[codec_name] = 1; @@ -75,7 +75,7 @@ void B2BMediaStatistics::decCodecReadUsage(const string &codec_name) { if (codec_name.empty()) return; - AmLock lock(mutex); + lock_guard lock(mutex); map::iterator i = codec_read_usage.find(codec_name); if (i != codec_read_usage.end()) { if (i->second > 0) i->second--; @@ -96,7 +96,7 @@ void B2BMediaStatistics::reportCodecWriteUsage(string &dst) bool first = true; dst.clear(); - AmLock lock(mutex); + lock_guard lock(mutex); for (map::iterator i = codec_write_usage.begin(); i != codec_write_usage.end(); ++i) { @@ -117,7 +117,7 @@ void B2BMediaStatistics::reportCodecReadUsage(string &dst) bool first = true; dst.clear(); - AmLock lock(mutex); + lock_guard lock(mutex); for (map::iterator i = codec_read_usage.begin(); i != codec_read_usage.end(); ++i) { @@ -135,7 +135,7 @@ void B2BMediaStatistics::getReport(const AmArg &args, AmArg &ret) AmArg read_usage; { // locked area - AmLock lock(mutex); + lock_guard lock(mutex); for (map::iterator i = codec_write_usage.begin(); i != codec_write_usage.end(); ++i) @@ -548,7 +548,7 @@ bool AmB2BMedia::releaseReference() { void AmB2BMedia::changeSession(bool a_leg, AmB2BSession *new_session) { - AmLock lock(mutex); + lock_guard lock(mutex); changeSessionUnsafe(a_leg, new_session); } @@ -610,7 +610,7 @@ void AmB2BMedia::changeSessionUnsafe(bool a_leg, AmB2BSession *new_session) int AmB2BMedia::writeStreams(unsigned long long ts, unsigned char *buffer) { int res = 0; - AmLock lock(mutex); + lock_guard lock(mutex); for (AudioStreamIterator i = audio.begin(); i != audio.end(); ++i) { if (i->a.writeStream(ts, buffer, i->b) < 0) { res = -1; break; } if (i->b.writeStream(ts, buffer, i->a) < 0) { res = -1; break; } @@ -620,7 +620,7 @@ int AmB2BMedia::writeStreams(unsigned long long ts, unsigned char *buffer) void AmB2BMedia::processDtmfEvents() { - AmLock lock(mutex); + lock_guard lock(mutex); for (AudioStreamIterator i = audio.begin(); i != audio.end(); ++i) { i->a.processDtmfEvents(); i->b.processDtmfEvents(); @@ -632,7 +632,7 @@ void AmB2BMedia::processDtmfEvents() void AmB2BMedia::sendDtmf(bool a_leg, int event, unsigned int duration_ms) { - AmLock lock(mutex); + lock_guard lock(mutex); if(!audio.size()) return; @@ -648,7 +648,7 @@ void AmB2BMedia::sendDtmf(bool a_leg, int event, unsigned int duration_ms) void AmB2BMedia::clearAudio(bool a_leg) { TRACE("clear %s leg audio\n", a_leg ? "A" : "B"); - AmLock lock(mutex); + lock_guard lock(mutex); for (AudioStreamIterator i = audio.begin(); i != audio.end(); ++i) { // remove streams from AmRtpReceiver first! (always both?) @@ -685,7 +685,7 @@ void AmB2BMedia::clearAudio(bool a_leg) void AmB2BMedia::clearRTPTimeout() { - AmLock lock(mutex); + lock_guard lock(mutex); for (AudioStreamIterator i = audio.begin(); i != audio.end(); ++i) { i->a.clearRTPTimeout(); @@ -740,7 +740,7 @@ void AmB2BMedia::replaceConnectionAddress(AmSdp &parser_sdp, bool a_leg, const string& relay_address, const string& relay_public_address) { - AmLock lock(mutex); + lock_guard lock(mutex); /* needed for the 'quick workaround' for non-audio media */ SdpConnection orig_conn = parser_sdp.conn; @@ -1005,7 +1005,7 @@ void AmB2BMedia::updateStreams(bool a_leg, const AmSdp &local_sdp, const AmSdp & INFO("remote SDP: %s\n", s.c_str()); */ - AmLock lock(mutex); + lock_guard lock(mutex); /* streams should be created already (replaceConnectionAddress called before updateLocalSdp uses/assignes their port numbers) */ @@ -1101,7 +1101,7 @@ bool AmB2BMedia::replaceOffer(AmSdp &sdp, bool a_leg) TRACE("replacing offer with a local one\n"); createStreams(sdp); // create missing streams - AmLock lock(mutex); + lock_guard lock(mutex); try { @@ -1134,7 +1134,7 @@ bool AmB2BMedia::replaceOffer(AmSdp &sdp, bool a_leg) void AmB2BMedia::setMuteFlag(bool a_leg, bool set) { - AmLock lock(mutex); + lock_guard lock(mutex); if (a_leg) a_leg_muted = set; else b_leg_muted = set; for (AudioStreamIterator i = audio.begin(); i != audio.end(); ++i) { @@ -1145,7 +1145,7 @@ void AmB2BMedia::setMuteFlag(bool a_leg, bool set) void AmB2BMedia::setFirstStreamInput(bool a_leg, AmAudio *in) { - AmLock lock(mutex); + lock_guard lock(mutex); //for ( i != audio.end(); ++i) { if (!audio.empty()) { AudioStreamIterator i = audio.begin(); @@ -1170,7 +1170,7 @@ void AmB2BMedia::createHoldAnswer(bool a_leg, const AmSdp &offer, AmSdp &answer, // non-disabled stream in the response so we can not set all ports to 0 to // signalize that we don't want to receive anything) - AmLock lock(mutex); + lock_guard lock(mutex); answer = offer; answer.media.clear(); @@ -1220,7 +1220,7 @@ void AmB2BMedia::setRtpLogger(msg_logger* _logger) void AmB2BMedia::setRelayDTMFReceiving(bool enabled) { - AmLock lock(mutex); + lock_guard lock(mutex); DBG("relay_streams.size() = %zd, audio_streams.size() = %zd\n", relay_streams.size(), audio.size()); for (RelayStreamIterator j = relay_streams.begin(); j != relay_streams.end(); j++) { @@ -1244,7 +1244,7 @@ void AmB2BMedia::setRelayDTMFReceiving(bool enabled) { /** set receving of RTP/relay streams (not receiving=drop incoming packets) */ void AmB2BMedia::setReceiving(bool receiving_a, bool receiving_b) { - AmLock lock(mutex); + lock_guard lock(mutex); DBG("relay_streams.size() = %zd, audio_streams.size() = %zd\n", relay_streams.size(), audio.size()); for (RelayStreamIterator j = relay_streams.begin(); j != relay_streams.end(); j++) { @@ -1265,7 +1265,7 @@ void AmB2BMedia::setReceiving(bool receiving_a, bool receiving_b) { void AmB2BMedia::pauseRelay() { - AmLock lock(mutex); + lock_guard lock(mutex); DBG("relay_streams.size() = %zd, audio_streams.size() = %zd\n", relay_streams.size(), audio.size()); relay_paused = true; @@ -1282,7 +1282,7 @@ void AmB2BMedia::pauseRelay() { void AmB2BMedia::restartRelay() { - AmLock lock(mutex); + lock_guard lock(mutex); DBG("relay_streams.size() = %zd, audio_streams.size() = %zd\n", relay_streams.size(), audio.size()); relay_paused = false; @@ -1311,7 +1311,7 @@ void AudioStreamData::debug() void AmB2BMedia::debug() { - AmLock lock(mutex); + lock_guard lock(mutex); // walk through all the streams DBG("B2B media session %p ('%s' <-> '%s'):", diff --git a/core/AmSessionContainer.cpp b/core/AmSessionContainer.cpp index 1f5248e1..ae6e6cbd 100644 --- a/core/AmSessionContainer.cpp +++ b/core/AmSessionContainer.cpp @@ -381,7 +381,7 @@ bool AmSessionContainer::postEvent(const string& local_tag, void AmSessionContainer::setCPSLimit(unsigned int limit) { - AmLock lock(cps_mut); + lock_guard lock(cps_mut); CPSLimit = CPSHardLimit = limit; } @@ -395,7 +395,7 @@ void AmSessionContainer::setCPSSoftLimit(unsigned int percent) struct timeval tv, res; gettimeofday(&tv,0); - AmLock lock(cps_mut); + lock_guard lock(cps_mut); while (cps_queue.size()) { timersub(&tv, &cps_queue.front(), &res); @@ -412,7 +412,7 @@ void AmSessionContainer::setCPSSoftLimit(unsigned int percent) pair AmSessionContainer::getCPSLimit() { - AmLock lock(cps_mut); + lock_guard lock(cps_mut); return pair(CPSHardLimit, CPSLimit); } @@ -421,7 +421,7 @@ unsigned int AmSessionContainer::getAvgCPS() struct timeval tv, res; gettimeofday(&tv,0); - AmLock lock(cps_mut); + lock_guard lock(cps_mut); while (cps_queue.size()) { timersub(&tv, &cps_queue.front(), &res); @@ -438,7 +438,7 @@ unsigned int AmSessionContainer::getAvgCPS() unsigned int AmSessionContainer::getMaxCPS() { - AmLock lock(cps_mut); + lock_guard lock(cps_mut); unsigned int res = max_cps; max_cps = 0; return res; @@ -453,7 +453,7 @@ bool AmSessionContainer::check_and_add_cps(bool emergency_flag) struct timeval tv, res; gettimeofday(&tv,0); - AmLock lock(cps_mut); + lock_guard lock(cps_mut); // check global policy for dropping emergency calls if (!AmConfig::skip_cpslimit_emergency && emergency_flag) { diff --git a/core/AmThread.cpp b/core/AmThread.cpp index c33a0fc0..faf38fe5 100644 --- a/core/AmThread.cpp +++ b/core/AmThread.cpp @@ -111,10 +111,9 @@ void AmThread::start() void AmThread::stop() { - _m_td.lock(); + lock_guard _l(_m_td); if(is_stopped()){ - _m_td.unlock(); return; } @@ -138,12 +137,10 @@ void AmThread::stop() DBG("Thread %lu (%lu) finished detach.\n", (unsigned long int) _pid, (unsigned long int) _td); //pthread_cancel(_td); - - _m_td.unlock(); } void AmThread::cancel() { - _m_td.lock(); + lock_guard _l(_m_td); int res; if ((res = pthread_cancel(_td)) != 0) { @@ -152,8 +149,6 @@ void AmThread::cancel() { DBG("Thread %lu is canceled.\n", (unsigned long int) _pid); _stopped.set(true); } - - _m_td.unlock(); } void AmThread::join() diff --git a/core/AmThread.h b/core/AmThread.h index 5ad448ef..35190144 100644 --- a/core/AmThread.h +++ b/core/AmThread.h @@ -34,6 +34,9 @@ #include #include +#include + +using std::lock_guard; /** * \brief C++ Wrapper class for pthread mutex @@ -49,21 +52,6 @@ public: void unlock(); }; -/** - * \brief Simple lock class - */ -class AmLock -{ - AmMutex& m; -public: - AmLock(AmMutex& _m) : m(_m) { - m.lock(); - } - ~AmLock(){ - m.unlock(); - } -}; - /** * \brief Shared variable. * diff --git a/core/log.cpp b/core/log.cpp index 933657e3..9e794597 100644 --- a/core/log.cpp +++ b/core/log.cpp @@ -202,7 +202,7 @@ void run_log_hooks(int level, pid_t pid, pthread_t tid, const char* func, const */ void register_log_hook(AmLoggingFacility* fac) { - AmLock lock(log_hooks_mutex); + lock_guard lock(log_hooks_mutex); log_hooks.push_back(fac); } diff --git a/core/sip/async_file.cpp b/core/sip/async_file.cpp index f12bc65f..b4252270 100644 --- a/core/sip/async_file.cpp +++ b/core/sip/async_file.cpp @@ -41,7 +41,7 @@ async_file::~async_file() int async_file::write(const void* buf, unsigned int len) { - AmLock _l(*this); + lock_guard _l(*this); if(closed) return Closed; if(error) return Error; @@ -58,7 +58,7 @@ int async_file::write(const void* buf, unsigned int len) int async_file::writev(const struct iovec *iov, int iovcnt) { - AmLock _l(*this); + lock_guard _l(*this); if(closed) return Closed; if(error) return Error; @@ -75,7 +75,7 @@ int async_file::writev(const struct iovec *iov, int iovcnt) void async_file::close() { - AmLock _l(*this); + lock_guard _l(*this); closed = true; event_active(ev_write, 0, 0); } @@ -120,6 +120,6 @@ void async_file::write_cycle() unsigned int async_file::get_buffered_bytes() { - AmLock _l(*this); + lock_guard _l(*this); return fifo_buffer::get_buffered_bytes(); } diff --git a/core/sip/exclusive_file.cpp b/core/sip/exclusive_file.cpp index 7712db67..8273989b 100644 --- a/core/sip/exclusive_file.cpp +++ b/core/sip/exclusive_file.cpp @@ -32,7 +32,7 @@ class _excl_file_reg public: exclusive_file* get(const string& name, bool& is_new) { - AmLock l(files_mut); + lock_guard l(files_mut); map::iterator it = files.find(name); if(it != files.end()) { excl_file_entry& fe = it->second; @@ -62,7 +62,7 @@ public: } void deref(const string& name) { - AmLock l(files_mut); + lock_guard l(files_mut); map::iterator it = files.find(name); if(it != files.end()) { excl_file_entry& fe = it->second; @@ -76,7 +76,7 @@ public: } bool delete_on_flushed(const string& name) { - AmLock l(files_mut); + lock_guard l(files_mut); map::iterator it = files.find(name); if(it != files.end()) { excl_file_entry& fe = it->second; diff --git a/core/sip/msg_logger.cpp b/core/sip/msg_logger.cpp index 0fb53474..27d67ad1 100644 --- a/core/sip/msg_logger.cpp +++ b/core/sip/msg_logger.cpp @@ -105,7 +105,7 @@ int cf_msg_logger::log(const char* buf, int len, string src = addr2str(src_ip); string dst = addr2str(dst_ip); - AmLock _l(*(AmMutex*)excl_fp); + lock_guard _l(*excl_fp); write_src_dst(src); write_src_dst(dst); diff --git a/core/sip/tcp_trsp.cpp b/core/sip/tcp_trsp.cpp index 82802f5f..0e206476 100644 --- a/core/sip/tcp_trsp.cpp +++ b/core/sip/tcp_trsp.cpp @@ -244,7 +244,7 @@ int tcp_trsp_socket::check_connection() int tcp_trsp_socket::send(const sockaddr_storage* sa, const char* msg, const int msg_len, unsigned int flags) { - AmLock _l(sock_mut); + lock_guard _l(sock_mut); if(closed || (check_connection() < 0)) return -1; @@ -309,7 +309,7 @@ void tcp_trsp_socket::on_read(short ev) return; } - AmLock _l(sock_mut); + lock_guard _l(sock_mut); DBG("on_read (connected = %i)",connected); bytes = ::read(sd,get_input(),get_input_free_space()); @@ -426,7 +426,7 @@ int tcp_trsp_socket::parse_input() void tcp_trsp_socket::on_write(short ev) { - AmLock _l(sock_mut); + lock_guard _l(sock_mut); DBG("on_write (connected = %i)",connected); if(!connected) {