1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
// 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 "config.h"
#include "runtime.h"
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <semaphore.h>
/* If we don't have sem_timedwait, use pthread_cond_timedwait instead.
We don't always use condition variables because on some systems
pthread_mutex_lock and pthread_mutex_unlock must be called by the
same thread. That is never true of semaphores. */
struct go_sem
{
sem_t sem;
#ifndef HAVE_SEM_TIMEDWAIT
int timedwait;
pthread_mutex_t mutex;
pthread_cond_t cond;
#endif
};
/* Create a semaphore. */
uintptr
runtime_semacreate(void)
{
struct go_sem *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 (struct go_sem));
if (sem_init (&p->sem, 0, 0) != 0)
runtime_throw ("sem_init");
#ifndef HAVE_SEM_TIMEDWAIT
if (pthread_mutex_init (&p->mutex, NULL) != 0)
runtime_throw ("pthread_mutex_init");
if (pthread_cond_init (&p->cond, NULL) != 0)
runtime_throw ("pthread_cond_init");
#endif
return (uintptr) p;
}
/* Acquire m->waitsema. */
int32
runtime_semasleep (int64 ns)
{
M *m;
struct go_sem *sem;
int r;
m = runtime_m ();
sem = (struct go_sem *) m->waitsema;
if (ns >= 0)
{
int64 abs;
struct timespec ts;
int err;
abs = ns + runtime_nanotime ();
ts.tv_sec = abs / 1000000000LL;
ts.tv_nsec = abs % 1000000000LL;
err = 0;
#ifdef HAVE_SEM_TIMEDWAIT
r = sem_timedwait (&sem->sem, &ts);
if (r != 0)
err = errno;
#else
if (pthread_mutex_lock (&sem->mutex) != 0)
runtime_throw ("pthread_mutex_lock");
while ((r = sem_trywait (&sem->sem)) != 0)
{
r = pthread_cond_timedwait (&sem->cond, &sem->mutex, &ts);
if (r != 0)
{
err = r;
break;
}
}
if (pthread_mutex_unlock (&sem->mutex) != 0)
runtime_throw ("pthread_mutex_unlock");
#endif
if (err != 0)
{
if (err == ETIMEDOUT || err == EAGAIN || err == EINTR)
return -1;
runtime_throw ("sema_timedwait");
}
return 0;
}
while (sem_wait (&sem->sem) != 0)
{
if (errno == EINTR)
continue;
runtime_throw ("sem_wait");
}
return 0;
}
/* Wake up mp->waitsema. */
void
runtime_semawakeup (M *mp)
{
struct go_sem *sem;
sem = (struct go_sem *) mp->waitsema;
if (sem_post (&sem->sem) != 0)
runtime_throw ("sem_post");
#ifndef HAVE_SEM_TIMEDWAIT
if (pthread_mutex_lock (&sem->mutex) != 0)
runtime_throw ("pthread_mutex_lock");
if (pthread_cond_broadcast (&sem->cond) != 0)
runtime_throw ("pthread_cond_broadcast");
if (pthread_mutex_unlock (&sem->mutex) != 0)
runtime_throw ("pthread_mutex_unlock");
#endif
}
void
runtime_osinit (void)
{
runtime_ncpu = getproccount();
}
void
runtime_goenvs (void)
{
runtime_goenvs_unix ();
}
|