* spliting manager code into separate files
[mdictionary] / src / manager / src / ws_mng_threads_utils.c
1 /*******************************************************************************
2 This file is part of WhiteStork.
3
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.
8
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.
13
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
17
18 Copyright 2006 ComArch S.A.
19 *******************************************************************************/
20 #include <ws_mng_threads_utils.h>
21
22
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)
26 {
27         gboolean res = !g_static_mutex_trylock(data->action_working);
28         if (res)
29         {
30                 g_debug("[M-action] %s - FAILED - locked already!", fun);
31         }
32         else
33         {
34                 g_debug("[M-action] %s - SUCCESS - locked!", fun);
35         }
36
37         return res;
38 }
39
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)
44 {
45         gboolean r = !g_static_rec_mutex_trylock(m);
46         if (FALSE == r)
47         {
48                 g_static_rec_mutex_unlock(m);
49                 g_debug("[M-stop] STOP mutex is UNLOCKED!");
50         }
51         else
52         {
53                 g_debug("[M-stop] STOP mutex is LOCKED");
54         }
55         return r;
56 }
57
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)
61 {
62         static GStaticMutex _loc;
63         static GStaticMutex* loc = NULL;
64         if(NULL == loc && NULL == data)
65         {
66                 g_debug("Initializing static mutex. function:%s\n",__FUNCTION__);
67                 g_static_mutex_init (loc);
68                 loc = &_loc;
69                 return;
70         }
71
72         /* critical section for calling is_rec_locked() function*/
73         g_static_mutex_lock(loc);
74         if ( is_rec_locked(data->action_stop) )
75         {
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);
82                 g_thread_exit (NULL);
83         }
84         g_static_mutex_unlock(loc);
85 }