Fix r180999.
[gcc.git] / libgo / runtime / go-rec-nb-small.c
1 /* go-rec-nb-small.c -- nonblocking receive of something smal on a channel.
2
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. */
6
7 #include <stdint.h>
8
9 #include "go-assert.h"
10 #include "go-panic.h"
11 #include "channel.h"
12
13 /* Prepare to receive something on a nonblocking channel. */
14
15 int
16 __go_receive_nonblocking_acquire (struct __go_channel *channel)
17 {
18 int i;
19 _Bool has_data;
20
21 i = pthread_mutex_lock (&channel->lock);
22 __go_assert (i == 0);
23
24 while (channel->selected_for_receive)
25 {
26 i = pthread_cond_wait (&channel->cond, &channel->lock);
27 __go_assert (i == 0);
28 }
29
30 if (channel->is_closed
31 && (channel->num_entries == 0
32 ? channel->next_store == 0
33 : channel->next_fetch == channel->next_store))
34 {
35 __go_unlock_and_notify_selects (channel);
36 return RECEIVE_NONBLOCKING_ACQUIRE_CLOSED;
37 }
38
39 if (channel->num_entries > 0)
40 has_data = channel->next_fetch != channel->next_store;
41 else
42 {
43 if (channel->waiting_to_receive)
44 {
45 /* Some other goroutine is already waiting for data on this
46 channel, so we can't pick it up. */
47 has_data = 0;
48 }
49 else if (channel->next_store > 0)
50 {
51 /* There is data on the channel. */
52 has_data = 1;
53 }
54 else if (__go_synch_with_select (channel, 0))
55 {
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);
60
61 while (channel->next_store == 0)
62 {
63 i = pthread_cond_wait (&channel->cond, &channel->lock);
64 __go_assert (i == 0);
65 }
66
67 has_data = 1;
68 }
69 else
70 {
71 /* Otherwise there is no data. */
72 has_data = 0;
73 }
74
75 if (has_data)
76 {
77 channel->waiting_to_receive = 1;
78 __go_assert (channel->next_store == 1);
79 }
80 }
81
82 if (!has_data)
83 {
84 i = pthread_mutex_unlock (&channel->lock);
85 __go_assert (i == 0);
86 return RECEIVE_NONBLOCKING_ACQUIRE_NODATA;
87 }
88
89 return RECEIVE_NONBLOCKING_ACQUIRE_DATA;
90 }
91
92 /* Receive something 64 bits or smaller on a nonblocking channel. */
93
94 struct __go_receive_nonblocking_small
95 __go_receive_nonblocking_small (struct __go_channel *channel)
96 {
97 uintptr_t element_size;
98 struct __go_receive_nonblocking_small ret;
99
100 if (channel == NULL)
101 {
102 ret.__val = 0;
103 ret.__success = 0;
104 ret.__closed = 0;
105 return ret;
106 }
107
108 element_size = channel->element_type->__size;
109 __go_assert (element_size <= sizeof (uint64_t));
110
111 int data = __go_receive_nonblocking_acquire (channel);
112 if (data != RECEIVE_NONBLOCKING_ACQUIRE_DATA)
113 {
114 ret.__val = 0;
115 ret.__success = 0;
116 ret.__closed = data == RECEIVE_NONBLOCKING_ACQUIRE_CLOSED;
117 return ret;
118 }
119
120 ret.__val = channel->data[channel->next_fetch];
121
122 __go_receive_release (channel);
123
124 ret.__success = 1;
125 ret.__closed = 0;
126
127 return ret;
128 }