Bump release : v1.0.1
[profile/ivi/message-port.git] / lib / msgport-factory.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of message-port.
5  *
6  * Copyright (C) 2013 Intel Corporation.
7  *
8  * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  */
25
26 #include "msgport-factory.h"
27 #include "msgport-manager.h"
28 #include "common/log.h"
29 #include <glib.h>
30
31 GHashTable *__managers = NULL; /* GThread:MsgPortManager */
32 G_LOCK_DEFINE(managers);
33
34 static 
35 void msgport_factory_init ()
36 {
37     G_LOCK(managers);
38
39     if (!__managers)
40         __managers = g_hash_table_new_full (g_direct_hash, g_direct_equal,
41                               NULL, (GDestroyNotify)g_object_unref);
42
43     G_UNLOCK(managers);
44 }
45
46 void msgport_factory_uninit ()
47 {
48     G_LOCK(managers);
49
50     if (__managers) {
51         g_hash_table_destroy (__managers);
52         __managers = NULL;
53     }
54
55     G_UNLOCK(managers);
56 }
57
58 MsgPortManager * msgport_factory_get_manager () 
59 {
60     MsgPortManager *manager = NULL;
61     GThread *self_thread = g_thread_self ();
62
63     if (!__managers) msgport_factory_init ();
64
65     G_LOCK(managers);
66
67     manager = MSGPORT_MANAGER (g_hash_table_lookup (__managers, self_thread));
68
69     if (!manager) {
70         manager = msgport_manager_new ();
71
72         if (manager) 
73             g_hash_table_insert (__managers, (gpointer)self_thread, manager);
74     }
75
76     G_UNLOCK(managers);
77
78     return manager;
79 }