1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 func unimplemented(name string) {
17 println(name, "not implemented")
18 *(*int)(unsafe.Pointer(uintptr(1231))) = 1231
22 func semacreate(mp *m) {
27 if err := pthread_mutex_init(&mp.mutex, nil); err != 0 {
28 throw("pthread_mutex_init")
30 if err := pthread_cond_init(&mp.cond, nil); err != 0 {
31 throw("pthread_cond_init")
36 func semasleep(ns int64) int32 {
42 pthread_mutex_lock(&mp.mutex)
46 pthread_mutex_unlock(&mp.mutex)
50 spent := nanotime() - start
52 pthread_mutex_unlock(&mp.mutex)
57 err := pthread_cond_timedwait_relative_np(&mp.cond, &mp.mutex, &t)
58 if err == _ETIMEDOUT {
59 pthread_mutex_unlock(&mp.mutex)
63 pthread_cond_wait(&mp.cond, &mp.mutex)
69 func semawakeup(mp *m) {
70 pthread_mutex_lock(&mp.mutex)
73 pthread_cond_signal(&mp.cond)
75 pthread_mutex_unlock(&mp.mutex)
78 // The read and write file descriptors used by the sigNote functions.
79 var sigNoteRead, sigNoteWrite int32
81 // sigNoteSetup initializes an async-signal-safe note.
83 // The current implementation of notes on Darwin is not async-signal-safe,
84 // because the functions pthread_mutex_lock, pthread_cond_signal, and
85 // pthread_mutex_unlock, called by semawakeup, are not async-signal-safe.
86 // There is only one case where we need to wake up a note from a signal
87 // handler: the sigsend function. The signal handler code does not require
88 // all the features of notes: it does not need to do a timed wait.
89 // This is a separate implementation of notes, based on a pipe, that does
90 // not support timed waits but is async-signal-safe.
91 func sigNoteSetup(*note) {
92 if sigNoteRead != 0 || sigNoteWrite != 0 {
93 throw("duplicate sigNoteSetup")
96 sigNoteRead, sigNoteWrite, errno = pipe()
100 closeonexec(sigNoteRead)
101 closeonexec(sigNoteWrite)
103 // Make the write end of the pipe non-blocking, so that if the pipe
104 // buffer is somehow full we will not block in the signal handler.
105 // Leave the read end of the pipe blocking so that we will block
107 setNonblock(sigNoteWrite)
110 // sigNoteWakeup wakes up a thread sleeping on a note created by sigNoteSetup.
111 func sigNoteWakeup(*note) {
113 write(uintptr(sigNoteWrite), unsafe.Pointer(&b), 1)
116 // sigNoteSleep waits for a note created by sigNoteSetup to be woken.
117 func sigNoteSleep(*note) {
120 read(sigNoteRead, unsafe.Pointer(&b), 1)
124 // BSD interface for threading.
126 // pthread_create delayed until end of goenvs so that we
127 // can look at the environment first.
130 physPageSize = getPageSize()