src/manager/src/ws_mng_threads_utils.c

Go to the documentation of this file.
00001 /*******************************************************************************
00002 This file is part of mdictionary.
00003 
00004 mdictionary is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 mdictionary is distributed in the hope that it will be useful, 
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License 
00015 along with mdictionary; if not, write to the Free Software
00016 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00017 
00018 Copyright 2006-2008 ComArch S.A.
00019 *******************************************************************************/
00026 #include <ws_mng_threads_utils.h>
00027 
00028 
00029 /* added by Dariusz Wiechecki 
00030  * trying to lock mutext and tells if it was locked */
00036 gboolean try_lock_was_locked(WSMngSearchData* data, gchar* fun)
00037 {
00038         gboolean res = !g_static_mutex_trylock(data->action_working);
00039         if (res)
00040         {
00041                 g_debug("[M-action] %s - FAILED - locked already!", fun);
00042         }
00043         else
00044         {
00045                 g_debug("[M-action] %s - SUCCESS - locked!", fun);
00046         }
00047 
00048         return res;
00049 }
00050 
00051 /* added by Dariusz Wiechecki
00052  * check if recursive mutex is already locked 
00053  * must be call from critical section!!! */
00060 gboolean is_rec_locked(GStaticRecMutex* m)
00061 {
00062         gboolean r = !g_static_rec_mutex_trylock(m);
00063         if (FALSE == r)
00064         {
00065                 g_static_rec_mutex_unlock(m);
00066                 g_debug("[M-stop] STOP mutex is UNLOCKED!");
00067         }
00068         else
00069         {
00070                 g_debug("[M-stop] STOP mutex is LOCKED");
00071         }
00072         return r;
00073 }
00074 
00075 /* added by Dariusz Wiechecki
00076  * stop current thread if there is such a need - newer thread is waiting */
00080 void stop_if_needed(WSMngSearchAtom* data)
00081 {
00082         static GStaticMutex _loc;
00083         static GStaticMutex* loc = NULL;
00084         if(NULL == loc && NULL == data)
00085         {
00086                 g_debug("Initializing static mutex. function:%s\n",__FUNCTION__);
00087                 g_static_mutex_init (loc);
00088                 loc = &_loc;
00089                 return;
00090         }
00091 
00092         WSMngSearchData* app_data = data->data;
00093         /* critical section for calling is_rec_locked() function*/
00094         g_static_mutex_lock(loc);
00095         if ( is_rec_locked(app_data->action_stop) )
00096         {
00097                 g_debug("[T-leaving] <---- Leaving thread (not finished).\n");
00098                 ws_dbus_notify(app_data->dbus_data, WS_DBUS_WORDS_LIST_FINISHED);
00099                 ws_dbus_notify(app_data->dbus_data, WS_DBUS_TRANSLATION_FINISHED);
00100                 free_search_atom(data);
00101                 g_static_mutex_unlock(app_data->action_working);
00102                 g_static_mutex_unlock(loc);
00103                 g_thread_exit (NULL);
00104         }
00105         g_static_mutex_unlock(loc);
00106 }
00107 
00111 void free_search_atom(WSMngSearchAtom* data)
00112 {
00113         g_debug("[T-clear] Cleaning after thread\n");
00114         g_assert(NULL != data);
00115         if (NULL != data->word)
00116         {
00117                 g_free(data->word);
00118         }
00119         if (NULL != data->trans)
00120         {
00121                 g_free(data->trans);
00122         }
00123         if (NULL != data->word_list)
00124         {
00125                 /* free memory used by each word from word list */
00126                 gint i = 0;
00127                 for (; i < data->word_list->len; i++)
00128                 {
00129                         g_free(g_array_index(data->word_list,gchar* ,i));
00130                 }
00131                 /* free memory used by GArray */
00132                 g_array_free(data->word_list, TRUE);
00133                 data->word_list = NULL;
00134         }
00135         g_free(data);
00136 }
00137 
00142 WSMngSearchAtom *create_search_atom(WSMngSearchData* app_data, gchar* word)
00143 {
00144         WSMngSearchAtom* search_data = NULL;
00145         search_data = (WSMngSearchAtom*)g_try_malloc(sizeof(WSMngSearchAtom));
00146         if (NULL == search_data)
00147         {
00148                 g_debug("[L-*] allocatting memmory for data failed!");
00149                 return NULL;
00150         }
00151         /* get the word sended by dbus */
00152         search_data->word = g_strdup(word);
00153         search_data->data = app_data;
00154         search_data->word_list = NULL;
00155         search_data->trans = NULL;
00156 
00157         return search_data;
00158 }
00159 

Generated on Fri Jan 11 14:30:17 2008 for mDictionary Project by  doxygen 1.5.1