From d75c80bef0af7eadcd5f85c15a81b91043d9e873 Mon Sep 17 00:00:00 2001 From: bstarynk Date: Thu, 24 Nov 2011 09:19:09 +0000 Subject: 2011-11-24 Basile Starynkevitch 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 --- libgo/runtime/thread-sema.c | 74 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 libgo/runtime/thread-sema.c (limited to 'libgo/runtime/thread-sema.c') 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 +#include +#include +#include + +/* 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) +{ +} -- cgit v1.2.1