FreeSWITCH API Documentation  1.7.0
switch_core_rwlock.c
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
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
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Anthony Minessale II <anthm@freeswitch.org>
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  */
34 
35 #include <switch.h>
37 
38 
40 {
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 
53 {
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 
66 {
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 
79 #ifdef SWITCH_DEBUG_RWLOCKS
80 SWITCH_DECLARE(switch_status_t) switch_core_session_perform_read_lock(switch_core_session_t *session, const char *file, const char *func, int line)
81 #else
83 #endif
84 {
86 
87  if (session->rwlock) {
88  if (switch_test_flag(session, SSF_DESTROYED) || switch_channel_down_nosig(session->channel)) {
89  status = SWITCH_STATUS_FALSE;
90  if (switch_thread_rwlock_tryrdlock(session->rwlock) == SWITCH_STATUS_SUCCESS) {
91  if (switch_channel_test_flag(session->channel, CF_THREAD_SLEEPING)) {
92 #ifdef SWITCH_DEBUG_RWLOCKS
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));
95 #endif
97  }
98  switch_thread_rwlock_unlock(session->rwlock);
99  }
100 #ifdef SWITCH_DEBUG_RWLOCKS
101  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",
102  switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
103 #endif
104  } else {
105  status = (switch_status_t) switch_thread_rwlock_tryrdlock(session->rwlock);
106 #ifdef SWITCH_DEBUG_RWLOCKS
107  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",
108  switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
109 #endif
110  }
111  }
112 
113  return status;
114 }
115 
116 
117 #ifdef SWITCH_DEBUG_RWLOCKS
118 SWITCH_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
121 #endif
122 {
124 
125  if (session->rwlock) {
126  if (switch_test_flag(session, SSF_DESTROYED) || switch_channel_get_state(session->channel) >= CS_DESTROY) {
127  status = SWITCH_STATUS_FALSE;
128 #ifdef SWITCH_DEBUG_RWLOCKS
129  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",
130  switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
131 #endif
132  } else {
133  status = (switch_status_t) switch_thread_rwlock_tryrdlock(session->rwlock);
134 #ifdef SWITCH_DEBUG_RWLOCKS
135  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",
136  switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
137 #endif
138  }
139  }
140 
141  return status;
142 }
143 
144 #ifdef SWITCH_DEBUG_RWLOCKS
145 SWITCH_DECLARE(void) switch_core_session_perform_write_lock(switch_core_session_t *session, const char *file, const char *func, int line)
146 {
147 
148  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",
149  switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
150 #else
152 {
153 #endif
154  switch_thread_rwlock_wrlock(session->rwlock);
155 }
156 
157 #ifdef SWITCH_DEBUG_RWLOCKS
158 SWITCH_DECLARE(void) switch_core_session_perform_rwunlock(switch_core_session_t *session, const char *file, const char *func, int line)
159 {
160  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",
161  switch_core_session_get_uuid(session), switch_channel_get_name(session->channel));
162 #else
164 {
165 #endif
166  switch_thread_rwlock_unlock(session->rwlock);
167 
168 }
169 
170 /* For Emacs:
171  * Local Variables:
172  * mode:c
173  * indent-tabs-mode:t
174  * tab-width:4
175  * c-basic-offset:4
176  * End:
177  * For VIM:
178  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
179  */
switch_channel_state_t switch_channel_get_state(switch_channel_t *channel)
Get the current state of a channel in the state engine.
switch_status_t switch_core_session_io_write_lock(switch_core_session_t *session)
switch_status_t switch_thread_rwlock_unlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:263
uint32_t switch_channel_test_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Test for presence of given flag on a given channel.
switch_status_t switch_core_session_io_read_lock(switch_core_session_t *session)
switch_status_t switch_core_session_io_rwunlock(switch_core_session_t *session)
switch_status_t switch_thread_rwlock_wrlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:237
switch_status_t switch_core_session_read_lock_hangup(switch_core_session_t *session)
switch_status_t switch_thread_rwlock_tryrdlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:232
#define switch_channel_down_nosig(_channel)
void switch_core_session_rwunlock(switch_core_session_t *session)
char * switch_core_session_get_uuid(_In_ switch_core_session_t *session)
Retrieve the unique identifier from a session.
void switch_core_session_write_lock(switch_core_session_t *session)
switch_status_t
Common return values.
Main Library Header.
#define SWITCH_DECLARE(type)
switch_status_t switch_core_session_wake_session_thread(_In_ switch_core_session_t *session)
switch_status_t switch_core_session_read_lock(switch_core_session_t *session)
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
char * switch_channel_get_name(switch_channel_t *channel)
Retrieve the name of a given channel.