summaryrefslogtreecommitdiff
path: root/libgo/runtime/thread-sema.c
diff options
context:
space:
mode:
authorbstarynk <bstarynk@138bc75d-0d04-0410-961f-82ee72b054a4>2011-11-24 09:19:09 +0000
committerbstarynk <bstarynk@138bc75d-0d04-0410-961f-82ee72b054a4>2011-11-24 09:19:09 +0000
commitd75c80bef0af7eadcd5f85c15a81b91043d9e873 (patch)
tree7c304af07f86f709bbc9dc565c9d52a3b75ac370 /libgo/runtime/thread-sema.c
parente76de565db9500971211ab0337fbd43466129ad3 (diff)
downloadgcc-d75c80bef0af7eadcd5f85c15a81b91043d9e873.tar.gz
2011-11-24 Basile Starynkevitch <basile@starynkevitch.net>
MELT branch merged with trunk rev 181685 using svnmerge git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/melt-branch@181689 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgo/runtime/thread-sema.c')
-rw-r--r--libgo/runtime/thread-sema.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/libgo/runtime/thread-sema.c b/libgo/runtime/thread-sema.c
new file mode 100644
index 00000000000..b0a6dc337fd
--- /dev/null
+++ b/libgo/runtime/thread-sema.c
@@ -0,0 +1,74 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include "runtime.h"
+
+#include <errno.h>
+#include <stdlib.h>
+#include <time.h>
+#include <semaphore.h>
+
+/* Create a semaphore. */
+
+uintptr
+runtime_semacreate(void)
+{
+ sem_t *p;
+
+ /* Call malloc rather than runtime_malloc. This will allocate space
+ on the C heap. We can't call runtime_malloc here because it
+ could cause a deadlock. */
+ p = malloc (sizeof (sem_t));
+ if (sem_init (p, 0, 0) != 0)
+ runtime_throw ("sem_init");
+ return (uintptr) p;
+}
+
+/* Acquire m->waitsema. */
+
+int32
+runtime_semasleep (int64 ns)
+{
+ int r;
+
+ if (ns >= 0)
+ {
+ struct timespec ts;
+
+ ns += runtime_nanotime ();
+ ts.tv_sec = ns / 1000000000LL;
+ ts.tv_nsec = ns % 1000000000LL;
+ r = sem_timedwait ((sem_t *) m->waitsema, &ts);
+ if (r != 0)
+ {
+ if (errno == ETIMEDOUT || errno == EINTR)
+ return -1;
+ runtime_throw ("sema_timedwait");
+ }
+ return 0;
+ }
+
+ while (sem_wait ((sem_t *) m->waitsema) != 0)
+ {
+ if (errno == EINTR)
+ continue;
+ runtime_throw ("sem_wait");
+ }
+
+ return 0;
+}
+
+/* Wake up mp->waitsema. */
+
+void
+runtime_semawakeup (M *mp)
+{
+ if (sem_post ((sem_t *) mp->waitsema) != 0)
+ runtime_throw ("sem_post");
+}
+
+void
+runtime_osinit(void)
+{
+}