1 /*******************************************************************************
2 This file is part of WhiteStork.
4 WhiteStork is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 WhiteStork is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with WhiteStork; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 Copyright 2006 ComArch S.A.
19 *******************************************************************************/
20 #include <ws_mng_threads_utils.h>
23 /* added by Dariusz Wiechecki
24 * trying to lock mutext and tells if it was locked */
25 gboolean try_lock_was_locked(WSMngSearchData* data, gchar* fun)
27 gboolean res = !g_static_mutex_trylock(data->action_working);
30 g_debug("[M-action] %s - FAILED - locked already!", fun);
34 g_debug("[M-action] %s - SUCCESS - locked!", fun);
40 /* added by Dariusz Wiechecki
41 * check if recursive mutex is already locked
42 * must be call from critical section!!! */
43 gboolean is_rec_locked(GStaticRecMutex* m)
45 gboolean r = !g_static_rec_mutex_trylock(m);
48 g_static_rec_mutex_unlock(m);
49 g_debug("[M-stop] STOP mutex is UNLOCKED!");
53 g_debug("[M-stop] STOP mutex is LOCKED");
58 /* added by Dariusz Wiechecki
59 * stop current thread if there is such a need - newer thread is waiting */
60 void stop_if_needed(WSMngSearchData* data)
62 static GStaticMutex _loc;
63 static GStaticMutex* loc = NULL;
64 if(NULL == loc && NULL == data)
66 g_debug("Initializing static mutex. function:%s\n",__FUNCTION__);
67 g_static_mutex_init (loc);
72 /* critical section for calling is_rec_locked() function*/
73 g_static_mutex_lock(loc);
74 if ( is_rec_locked(data->action_stop) )
76 g_debug("[T-leaving] <---- Leaving thread (not finished).\n");
77 ws_dbus_notify(data->dbus_data, WS_DBUS_WORDS_LIST_FINISHED);
78 ws_dbus_notify(data->dbus_data, WS_DBUS_TRANSLATION_FINISHED);
79 /* TODO clean after this thread!!!! */
80 g_static_mutex_unlock(data->action_working);
81 g_static_mutex_unlock(loc);
84 g_static_mutex_unlock(loc);