]> git.ipfire.org Git - thirdparty/freeswitch.git/blame - src/switch_core_rwlock.c
[core] JB audio: check for jb type and silence some debug. (#1191)
[thirdparty/freeswitch.git] / src / switch_core_rwlock.c
CommitLineData
df1ab07c 1/*
e6a60a20 2 * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
6e7d5d08 3 * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
e6a60a20
AM
4 *
5 * Version: MPL 1.1
6 *
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
16 *
17 * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18 *
19 * The Initial Developer of the Original Code is
ae220d33 20 * Anthony Minessale II <anthm@freeswitch.org>
e6a60a20
AM
21 * Portions created by the Initial Developer are Copyright (C)
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
df1ab07c 25 *
ae220d33 26 * Anthony Minessale II <anthm@freeswitch.org>
e6a60a20
AM
27 * Michael Jerris <mike@jerris.com>
28 * Paul D. Tinsley <pdt at jackhammer.org>
29 *
30 *
31 * switch_core_rwlock.c -- Main Core Library (read / write locks)
32 *
33 */
c815c059 34
e6a60a20 35#include <switch.h>
bcd32125 36#include "private/switch_core_pvt.h"
e6a60a20 37
be1abfe5 38
c4369fc8
AM
39SWITCH_DECLARE(switch_status_t) switch_core_session_io_read_lock(switch_core_session_t *session)
40{
41 switch_status_t status = SWITCH_STATUS_FALSE;
42
43 if (session->io_rwlock) {
44 if (switch_thread_rwlock_tryrdlock(session->io_rwlock) == SWITCH_STATUS_SUCCESS) {
45 status = SWITCH_STATUS_SUCCESS;
46 }
47 }
48
49 return status;
50}
51
52SWITCH_DECLARE(switch_status_t) switch_core_session_io_write_lock(switch_core_session_t *session)
53{
54 switch_status_t status = SWITCH_STATUS_FALSE;
55
56 if (session->io_rwlock) {
57 switch_thread_rwlock_wrlock(session->io_rwlock);
58 status = SWITCH_STATUS_SUCCESS;
59 }
60
61 return status;
62}
63
64
65SWITCH_DECLARE(switch_status_t) switch_core_session_io_rwunlock(switch_core_session_t *session)
66{
67 switch_status_t status = SWITCH_STATUS_FALSE;
68
69 if (session->io_rwlock) {
70 switch_thread_rwlock_unlock(session->io_rwlock);
71 status = SWITCH_STATUS_SUCCESS;
72 }
73
74 return status;
75}
76
77
78
e6a60a20 79#ifdef SWITCH_DEBUG_RWLOCKS
debdfb1a 80SWITCH_DECLARE(switch_status_t) switch_core_session_perform_read_lock(switch_core_session_t *session, const char *file, const char *func, int line)
e6a60a20
AM
81#else
82SWITCH_DECLARE(switch_status_t) switch_core_session_read_lock(switch_core_session_t *session)
83#endif
84{
85 switch_status_t status = SWITCH_STATUS_FALSE;
86
87 if (session->rwlock) {
d96b6a4a
AV
88 if ((status = switch_thread_rwlock_tryrdlock(session->rwlock)) == SWITCH_STATUS_SUCCESS) {
89 if (switch_test_flag(session, SSF_DESTROYED) || switch_channel_down_nosig(session->channel)) {
90 status = SWITCH_STATUS_FALSE;
dc86b797
AM
91 if (switch_channel_test_flag(session->channel, CF_THREAD_SLEEPING)) {
92#ifdef SWITCH_DEBUG_RWLOCKS
2330d424
AM
93 switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_core_session_get_uuid(session), SWITCH_LOG_ERROR, "%s %s Ping thread\n",
94 switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
dc86b797
AM
95#endif
96 switch_core_session_wake_session_thread(session);
97 }
d96b6a4a 98
e6a60a20 99#ifdef SWITCH_DEBUG_RWLOCKS
d96b6a4a
AV
100 switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_core_session_get_uuid(session), SWITCH_LOG_ERROR, "%s %s Read lock FAIL\n",
101 switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
e6a60a20 102#endif
d96b6a4a
AV
103 switch_thread_rwlock_unlock(session->rwlock);
104 } else {
e6a60a20 105#ifdef SWITCH_DEBUG_RWLOCKS
d96b6a4a
AV
106 switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_core_session_get_uuid(session), SWITCH_LOG_ERROR, "%s %s Read lock ACQUIRED\n",
107 switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
e6a60a20 108#endif
d96b6a4a 109 }
e6a60a20
AM
110 }
111 }
112
113 return status;
114}
115
9b98c2d5
AM
116
117#ifdef SWITCH_DEBUG_RWLOCKS
118SWITCH_DECLARE(switch_status_t) switch_core_session_perform_read_lock_hangup(switch_core_session_t *session, const char *file, const char *func, int line)
119#else
120SWITCH_DECLARE(switch_status_t) switch_core_session_read_lock_hangup(switch_core_session_t *session)
121#endif
122{
123 switch_status_t status = SWITCH_STATUS_FALSE;
124
125 if (session->rwlock) {
d96b6a4a
AV
126 if ((status = switch_thread_rwlock_tryrdlock(session->rwlock)) == SWITCH_STATUS_SUCCESS) {
127 if (switch_test_flag(session, SSF_DESTROYED) || switch_channel_get_state(session->channel) >= CS_DESTROY) {
128 status = SWITCH_STATUS_FALSE;
9b98c2d5 129#ifdef SWITCH_DEBUG_RWLOCKS
d96b6a4a
AV
130 switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_core_session_get_uuid(session), SWITCH_LOG_ERROR, "%s %s Read lock FAIL\n",
131 switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
9b98c2d5 132#endif
d96b6a4a
AV
133 switch_thread_rwlock_unlock(session->rwlock);
134 } else {
9b98c2d5 135#ifdef SWITCH_DEBUG_RWLOCKS
d96b6a4a
AV
136 switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_core_session_get_uuid(session), SWITCH_LOG_ERROR, "%s %s Read lock ACQUIRED\n",
137 switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
9b98c2d5 138#endif
d96b6a4a 139 }
9b98c2d5
AM
140 }
141 }
142
143 return status;
144}
145
e6a60a20 146#ifdef SWITCH_DEBUG_RWLOCKS
debdfb1a 147SWITCH_DECLARE(void) switch_core_session_perform_write_lock(switch_core_session_t *session, const char *file, const char *func, int line)
e6a60a20
AM
148{
149
2330d424
AM
150 switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_core_session_get_uuid(session), SWITCH_LOG_ERROR, "%s %s Write lock ACQUIRED\n",
151 switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
e6a60a20
AM
152#else
153SWITCH_DECLARE(void) switch_core_session_write_lock(switch_core_session_t *session)
154{
155#endif
156 switch_thread_rwlock_wrlock(session->rwlock);
157}
158
159#ifdef SWITCH_DEBUG_RWLOCKS
debdfb1a 160SWITCH_DECLARE(void) switch_core_session_perform_rwunlock(switch_core_session_t *session, const char *file, const char *func, int line)
e6a60a20 161{
2330d424
AM
162 switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_core_session_get_uuid(session), SWITCH_LOG_ERROR, "%s %s Read/Write lock CLEARED\n",
163 switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
e6a60a20
AM
164#else
165SWITCH_DECLARE(void) switch_core_session_rwunlock(switch_core_session_t *session)
166{
167#endif
168 switch_thread_rwlock_unlock(session->rwlock);
169
170}
aab6766e
BW
171
172/* For Emacs:
173 * Local Variables:
174 * mode:c
b0ad7ab5 175 * indent-tabs-mode:t
aab6766e
BW
176 * tab-width:4
177 * c-basic-offset:4
178 * End:
179 * For VIM:
32adc789 180 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
aab6766e 181 */