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