Fix PR47707
[official-gcc.git] / libgo / runtime / go-note.c
blob3b750f30e4dac22ab5fe9bf353fe90419b1755da
1 /* go-note.c -- implement notesleep, notewakeup and noteclear.
3 Copyright 2009 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
7 /* A note is a one-time notification. noteclear clears the note.
8 notesleep waits for a call to notewakeup. notewakeup wakes up
9 every thread waiting on the note. */
11 #include "go-assert.h"
12 #include "runtime.h"
14 /* We use a single global lock and condition variable. It would be
15 better to use a futex on Linux. */
17 static pthread_mutex_t note_lock = PTHREAD_MUTEX_INITIALIZER;
18 static pthread_cond_t note_cond = PTHREAD_COND_INITIALIZER;
20 /* noteclear is called before any calls to notesleep or
21 notewakeup. */
23 void
24 noteclear (Note* n)
26 int32 i;
28 i = pthread_mutex_lock (&note_lock);
29 __go_assert (i == 0);
31 n->woken = 0;
33 i = pthread_mutex_unlock (&note_lock);
34 __go_assert (i == 0);
37 /* Wait until notewakeup is called. */
39 void
40 notesleep (Note* n)
42 int32 i;
44 i = pthread_mutex_lock (&note_lock);
45 __go_assert (i == 0);
47 while (!n->woken)
49 i = pthread_cond_wait (&note_cond, &note_lock);
50 __go_assert (i == 0);
53 i = pthread_mutex_unlock (&note_lock);
54 __go_assert (i == 0);
57 /* Wake up every thread sleeping on the note. */
59 void
60 notewakeup (Note *n)
62 int32 i;
64 i = pthread_mutex_lock (&note_lock);
65 __go_assert (i == 0);
67 n->woken = 1;
69 i = pthread_cond_broadcast (&note_cond);
70 __go_assert (i == 0);
72 i = pthread_mutex_unlock (&note_lock);
73 __go_assert (i == 0);