summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorFelipe Contreras <felipe.contreras@nokia.com>2008-11-20 16:59:56 +0000
committerFelipe Contreras <felipe.contreras@nokia.com>2008-11-20 16:59:56 +0000
commit24464fc76ed884be2d63e81f078e06a3b4d1a106 (patch)
tree4e8ef2a377382414546660c04d7bf7b0760461b2 /util
parent7943c61d05a1851977a1f05c65a81869dfd90bd3 (diff)
Add new semaphore utility.
Diffstat (limited to 'util')
-rw-r--r--util/Makefile.am3
-rw-r--r--util/sem.c71
-rw-r--r--util/sem.h41
3 files changed, 114 insertions, 1 deletions
diff --git a/util/Makefile.am b/util/Makefile.am
index f21b8d2..fb6e0c3 100644
--- a/util/Makefile.am
+++ b/util/Makefile.am
@@ -1,6 +1,7 @@
noinst_LTLIBRARIES = libutil.la
-libutil_la_SOURCES = async_queue.c async_queue.h
+libutil_la_SOURCES = async_queue.c async_queue.h \
+ sem.c sem.h
libutil_la_CFLAGS = $(GTHREAD_CFLAGS)
libutil_la_LIBADD = $(GTHREAD_LIBS)
diff --git a/util/sem.c b/util/sem.c
new file mode 100644
index 0000000..8c6c109
--- /dev/null
+++ b/util/sem.c
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2008 Nokia Corporation.
+ *
+ * Author: Felipe Contreras <felipe.contreras@nokia.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include <glib.h>
+
+#include "sem.h"
+
+GSem *
+g_sem_new (void)
+{
+ GSem *sem;
+
+ sem = g_new (GSem, 1);
+ sem->condition = g_cond_new ();
+ sem->mutex = g_mutex_new ();
+ sem->counter = 0;
+
+ return sem;
+}
+
+void
+g_sem_free (GSem *sem)
+{
+ g_cond_free (sem->condition);
+ g_mutex_free (sem->mutex);
+ g_free (sem);
+}
+
+void
+g_sem_down (GSem *sem)
+{
+ g_mutex_lock (sem->mutex);
+
+ while (sem->counter == 0)
+ {
+ g_cond_wait (sem->condition, sem->mutex);
+ }
+
+ sem->counter--;
+
+ g_mutex_unlock (sem->mutex);
+}
+
+void
+g_sem_up (GSem *sem)
+{
+ g_mutex_lock (sem->mutex);
+
+ sem->counter++;
+ g_cond_signal (sem->condition);
+
+ g_mutex_unlock (sem->mutex);
+}
diff --git a/util/sem.h b/util/sem.h
new file mode 100644
index 0000000..d8fcc31
--- /dev/null
+++ b/util/sem.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2008 Nokia Corporation.
+ *
+ * Author: Felipe Contreras <felipe.contreras@nokia.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifndef SEM_H
+#define SEM_H
+
+#include <glib.h>
+
+typedef struct GSem GSem;
+
+struct GSem
+{
+ GCond *condition;
+ GMutex *mutex;
+ gint counter;
+};
+
+GSem *g_omx_sem_new (void);
+void g_omx_sem_free (GSem *sem);
+void g_omx_sem_down (GSem *sem);
+void g_omx_sem_up (GSem *sem);
+
+#endif /* SEM_H */