00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00026 #include <ws_mng_threads_utils.h>
00027
00028
00029
00030
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
00052
00053
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
00076
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
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
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
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
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