3336024afd1760d4b78a85cf69e490d47d2c8e60
[framework/multimedia/gst-openmax.git] / util / sem.c
1 /*
2  * Copyright (C) 2008-2009 Nokia Corporation.
3  *
4  * Author: Felipe Contreras <felipe.contreras@nokia.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation
9  * version 2.1 of the License.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #include <glib.h>
23
24 #include "sem.h"
25
26 GSem *
27 g_sem_new (void)
28 {
29   GSem *sem;
30
31   sem = g_new (GSem, 1);
32   sem->condition = g_cond_new ();
33   sem->mutex = g_mutex_new ();
34   sem->counter = 0;
35
36   return sem;
37 }
38
39 void
40 g_sem_free (GSem * sem)
41 {
42   g_cond_free (sem->condition);
43   g_mutex_free (sem->mutex);
44   g_free (sem);
45 }
46
47 void
48 g_sem_down (GSem * sem)
49 {
50   g_mutex_lock (sem->mutex);
51
52   while (sem->counter == 0) {
53     g_cond_wait (sem->condition, sem->mutex);
54   }
55
56   sem->counter--;
57
58   g_mutex_unlock (sem->mutex);
59 }
60
61 void
62 g_sem_up (GSem * sem)
63 {
64   g_mutex_lock (sem->mutex);
65
66   sem->counter++;
67   g_cond_signal (sem->condition);
68
69   g_mutex_unlock (sem->mutex);
70 }