name-lookup.h (cp_binding_level): Removed unused member names_size.
[official-gcc.git] / libgo / runtime / go-rec-nb-small.c
blobeb0a25e12b7d63b4fc9bc1651afa7002857f5ee9
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. */
7 #include <stdint.h>
9 #include "go-assert.h"
10 #include "go-panic.h"
11 #include "channel.h"
13 /* Prepare to receive something on a nonblocking channel. */
15 int
16 __go_receive_nonblocking_acquire (struct __go_channel *channel)
18 int i;
19 _Bool has_data;
21 i = pthread_mutex_lock (&channel->lock);
22 __go_assert (i == 0);
24 while (channel->selected_for_receive)
26 i = pthread_cond_wait (&channel->cond, &channel->lock);
27 __go_assert (i == 0);
30 if (channel->is_closed
31 && (channel->num_entries == 0
32 ? channel->next_store == 0
33 : channel->next_fetch == channel->next_store))
35 __go_unlock_and_notify_selects (channel);
36 return RECEIVE_NONBLOCKING_ACQUIRE_CLOSED;
39 if (channel->num_entries > 0)
40 has_data = channel->next_fetch != channel->next_store;
41 else
43 if (channel->waiting_to_receive)
45 /* Some other goroutine is already waiting for data on this
46 channel, so we can't pick it up. */
47 has_data = 0;
49 else if (channel->next_store > 0)
51 /* There is data on the channel. */
52 has_data = 1;
54 else if (__go_synch_with_select (channel, 0))
56 /* We synched up with a select sending data, so there will
57 be data for us shortly. Tell the select to go, and then
58 wait for the data. */
59 __go_broadcast_to_select (channel);
61 while (channel->next_store == 0)
63 i = pthread_cond_wait (&channel->cond, &channel->lock);
64 __go_assert (i == 0);
67 has_data = 1;
69 else
71 /* Otherwise there is no data. */
72 has_data = 0;
75 if (has_data)
77 channel->waiting_to_receive = 1;
78 __go_assert (channel->next_store == 1);
82 if (!has_data)
84 i = pthread_mutex_unlock (&channel->lock);
85 __go_assert (i == 0);
86 return RECEIVE_NONBLOCKING_ACQUIRE_NODATA;
89 return RECEIVE_NONBLOCKING_ACQUIRE_DATA;
92 /* Receive something 64 bits or smaller on a nonblocking channel. */
94 struct __go_receive_nonblocking_small
95 __go_receive_nonblocking_small (struct __go_channel *channel)
97 uintptr_t element_size;
98 struct __go_receive_nonblocking_small ret;
100 element_size = channel->element_type->__size;
101 __go_assert (element_size <= sizeof (uint64_t));
103 int data = __go_receive_nonblocking_acquire (channel);
104 if (data != RECEIVE_NONBLOCKING_ACQUIRE_DATA)
106 ret.__val = 0;
107 ret.__success = 0;
108 ret.__closed = data == RECEIVE_NONBLOCKING_ACQUIRE_CLOSED;
109 return ret;
112 ret.__val = channel->data[channel->next_fetch];
114 __go_receive_release (channel);
116 ret.__success = 1;
117 ret.__closed = 0;
119 return ret;