summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNIIBE Yutaka <gniibe@fsij.org>2013-06-19 12:59:12 +0900
committerNIIBE Yutaka <gniibe@fsij.org>2013-06-19 12:59:12 +0900
commitd77204c7d536806f66d3ad0e477e8039ff441681 (patch)
treea665b32f40e39a5b6bdf62fe2f07e5fc17dea39f
parent058ba0f939f18d890e11828f6a2535559e72bdb7 (diff)
add timed-eventflag
-rw-r--r--ChangeLog3
-rw-r--r--eventflag.c84
-rw-r--r--eventflag.h12
-rw-r--r--rules.mk5
4 files changed, 104 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index c1ba95c..5655301 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
2013-06-19 Niibe Yutaka <gniibe@fsij.org>
+ * rules.mk (CSRC): Add eventflag.c.
+ * eventflag.h, eventflag.c: New.
+
* chopstx.c (chopstx_main): New.
(chx_init): Initialize chopstx_main.
diff --git a/eventflag.c b/eventflag.c
new file mode 100644
index 0000000..658c779
--- /dev/null
+++ b/eventflag.c
@@ -0,0 +1,84 @@
+/*
+ * eventflag.c - Eventflag with timeout
+ *
+ * Copyright (C) 2013 Flying Stone Technology
+ * Author: NIIBE Yutaka <gniibe@fsij.org>
+ *
+ * This file is a part of Chopstx, a thread library for embedded.
+ *
+ * Chopstx is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Chopstx 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * As additional permission under GNU GPL version 3 section 7, you may
+ * distribute non-source form of the Program without the copy of the
+ * GNU GPL normally required by section 4, provided you inform the
+ * receipents of GNU GPL by a written offer.
+ *
+ */
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <chopstx.h>
+#include <eventflag.h>
+
+void
+eventflag_init (struct event_flag *ev, chopstx_t owner)
+{
+ ev->owner = owner;
+ ev->wait_usec = 0;
+ ev->flag = 0;
+ chopstx_mutex_init (&ev->mutex);
+}
+
+
+eventmask_t
+eventflag_wait_timeout (struct event_flag *ev, uint32_t usec)
+{
+ eventmask_t em = 0;
+ int n;
+
+ chopstx_mutex_lock (&ev->mutex);
+
+ if (!ev->flag)
+ {
+ ev->wait_usec = usec;
+ chopstx_mutex_unlock (&ev->mutex);
+ chopstx_usec_wait_var (&ev->wait_usec);
+ chopstx_mutex_lock (&ev->mutex);
+ ev->wait_usec = 0;
+ }
+
+ n = __builtin_ffs (ev->flag);
+ if (n)
+ {
+ em = (1 << (n - 1));
+ ev->flag &= ~em;
+ }
+
+ chopstx_mutex_unlock (&ev->mutex);
+ return em;
+}
+
+
+void
+eventflag_signal (struct event_flag *ev, eventmask_t m)
+{
+ chopstx_mutex_lock (&ev->mutex);
+ ev->flag |= m;
+ if (ev->wait_usec)
+ {
+ ev->wait_usec = 0;
+ chopstx_wakeup_usec_wait (ev->owner);
+ }
+ chopstx_mutex_unlock (&ev->mutex);
+}
diff --git a/eventflag.h b/eventflag.h
new file mode 100644
index 0000000..0b595b8
--- /dev/null
+++ b/eventflag.h
@@ -0,0 +1,12 @@
+typedef uint32_t eventmask_t;
+
+struct eventflag {
+ chopstx_t owner;
+ uint32_t wait_usec;
+ eventmask_t flag;
+ chopstx_mutex_t mutex;
+};
+
+void eventflag_init (struct eventflag *ev, chopstx_t owner);
+eventmask_t eventflag_wait_timeout (struct eventflag *ev, uint32_t usec);
+void eventflag_signal (struct eventflag *ev, eventmask_t m);
diff --git a/rules.mk b/rules.mk
index 91c77ca..eb8c3c5 100644
--- a/rules.mk
+++ b/rules.mk
@@ -1,6 +1,11 @@
# Chopstx make rules.
CSRC += $(CHOPSTX)/entry.c $(CHOPSTX)/chopstx.c
+
+ifneq ($(USE_EVENTFLAG),)
+CSRC += $(CHOPSTX)/eventflag.c
+endif
+
INCDIR = $(CHOPSTX)
BUILDDIR = build