1 /* go-rec-nb-small.c -- nonblocking receive of something smal 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 receive something on a nonblocking channel. */
17 __go_receive_nonblocking_acquire (struct __go_channel *channel)
22 i = pthread_mutex_lock (&channel->lock);
25 while (channel->selected_for_receive)
26 runtime_cond_wait (&channel->cond, &channel->lock);
28 if (channel->is_closed
29 && (channel->num_entries == 0
30 ? channel->next_store == 0
31 : channel->next_fetch == channel->next_store))
33 __go_unlock_and_notify_selects (channel);
34 return RECEIVE_NONBLOCKING_ACQUIRE_CLOSED;
37 if (channel->num_entries > 0)
38 has_data = channel->next_fetch != channel->next_store;
41 if (channel->waiting_to_receive)
43 /* Some other goroutine is already waiting for data on this
44 channel, so we can't pick it up. */
47 else if (channel->next_store > 0)
49 /* There is data on the channel. */
52 else if (__go_synch_with_select (channel, 0))
54 /* We synched up with a select sending data, so there will
55 be data for us shortly. Tell the select to go, and then
57 __go_broadcast_to_select (channel);
59 while (channel->next_store == 0)
60 runtime_cond_wait (&channel->cond, &channel->lock);
66 /* Otherwise there is no data. */
72 channel->waiting_to_receive = 1;
73 __go_assert (channel->next_store == 1);
79 i = pthread_mutex_unlock (&channel->lock);
81 return RECEIVE_NONBLOCKING_ACQUIRE_NODATA;
84 return RECEIVE_NONBLOCKING_ACQUIRE_DATA;
87 /* Receive something 64 bits or smaller on a nonblocking channel. */
89 struct __go_receive_nonblocking_small
90 __go_receive_nonblocking_small (struct __go_channel *channel)
92 uintptr_t element_size;
93 struct __go_receive_nonblocking_small ret;
103 element_size = channel->element_type->__size;
104 __go_assert (element_size <= sizeof (uint64_t));
106 int data = __go_receive_nonblocking_acquire (channel);
107 if (data != RECEIVE_NONBLOCKING_ACQUIRE_DATA)
111 ret.__closed = data == RECEIVE_NONBLOCKING_ACQUIRE_CLOSED;
115 ret.__val = channel->data[channel->next_fetch];
117 __go_receive_release (channel);