1 /* go-send-nb-small.c -- nonblocking send of something small on a channel.
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. */
10 #include "go-assert.h"
14 /* Prepare to send something on a nonblocking channel. Return true if
15 we acquired the channel, false if we did not acquire it because
16 there is no space to send a value. */
19 __go_send_nonblocking_acquire (struct __go_channel *channel)
24 i = pthread_mutex_lock (&channel->lock);
27 while (channel->selected_for_send)
28 runtime_cond_wait (&channel->cond, &channel->lock);
30 if (channel->is_closed)
32 i = pthread_mutex_unlock (&channel->lock);
34 __go_panic_msg ("send on closed channel");
37 if (channel->num_entries > 0)
38 has_space = ((channel->next_store + 1) % channel->num_entries
39 != channel->next_fetch);
42 /* This is a synchronous channel. If somebody is current
43 sending, then we can't send. Otherwise, see if somebody is
44 waiting to receive, or see if we can synch with a select. */
45 if (channel->waiting_to_send)
47 /* Some other goroutine is currently sending on this
48 channel, which means that we can't. */
51 else if (channel->waiting_to_receive)
53 /* Some other goroutine is waiting to receive a value, so we
54 can send directly to them. */
57 else if (__go_synch_with_select (channel, 1))
59 /* We found a select waiting to receive data, so we can send
61 __go_broadcast_to_select (channel);
66 /* Otherwise, we can't send, because nobody is waiting to
73 channel->waiting_to_send = 1;
74 __go_assert (channel->next_store == 0);
80 i = pthread_mutex_unlock (&channel->lock);
89 /* Send something 64 bits or smaller on a channel. */
92 __go_send_nonblocking_small (struct __go_channel *channel, uint64_t val)
97 __go_assert (channel->element_type->__size <= sizeof (uint64_t));
99 if (!__go_send_nonblocking_acquire (channel))
102 channel->data[channel->next_store] = val;
104 __go_send_release (channel);