1 /* channel.h -- the channel type for Go.
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. */
12 /* This structure is used when a select is waiting for a synchronous
15 struct __go_channel_select
17 /* A pointer to the next select waiting for this channel. */
18 struct __go_channel_select
*next
;
19 /* A pointer to the channel which this select will use. This starts
20 out as NULL and is set to the first channel which synchs up with
21 this one. This variable to which this points may only be
22 accessed when __go_select_data_mutex is held. */
23 struct __go_channel
**selected
;
24 /* A pointer to a variable which must be set to true if the
25 goroutine which sets *SELECTED wants to read from the channel,
26 false if it wants to write to it. */
30 /* A channel is a pointer to this structure. */
34 /* A mutex to control access to the channel. */
36 /* A condition variable. This is signalled when data is added to
37 the channel and when data is removed from the channel. */
39 /* The type of elements on this channel. */
40 const struct __go_type_descriptor
*element_type
;
41 /* True if a goroutine is waiting to send on a synchronous
43 _Bool waiting_to_send
;
44 /* True if a goroutine is waiting to receive on a synchronous
46 _Bool waiting_to_receive
;
47 /* True if this channel was selected for send in a select statement.
48 This looks out all other sends. */
49 _Bool selected_for_send
;
50 /* True if this channel was selected for receive in a select
51 statement. This locks out all other receives. */
52 _Bool selected_for_receive
;
53 /* True if this channel has been closed. */
55 /* The list of select statements waiting to send on a synchronous
57 struct __go_channel_select
*select_send_queue
;
58 /* The list of select statements waiting to receive on a synchronous
60 struct __go_channel_select
*select_receive_queue
;
61 /* If a select statement is waiting for this channel, it sets these
62 pointers. When something happens on the channel, the channel
63 locks the mutex, signals the condition, and unlocks the
65 pthread_mutex_t
*select_mutex
;
66 pthread_cond_t
*select_cond
;
67 /* The number of entries in the circular buffer. */
68 unsigned int num_entries
;
69 /* Where to store the next value. */
70 unsigned int next_store
;
71 /* Where to fetch the next value. If next_fetch == next_store, the
72 buffer is empty. If next_store + 1 == next_fetch, the buffer is
74 unsigned int next_fetch
;
75 /* The circular buffer. */
79 /* Try to link up with the structure generated by the frontend. */
80 typedef struct __go_channel __go_channel
;
82 /* The mutex used to control access to the value pointed to by the
83 __go_channel_select selected field. No additional mutexes may be
84 acquired while this mutex is held. */
85 extern pthread_mutex_t __go_select_data_mutex
;
87 extern struct __go_channel
*
88 __go_new_channel (const struct __go_type_descriptor
*, uintptr_t);
90 extern _Bool
__go_synch_with_select (struct __go_channel
*, _Bool
);
92 extern void __go_broadcast_to_select (struct __go_channel
*);
94 extern void __go_send_acquire (struct __go_channel
*, _Bool
);
96 extern _Bool
__go_send_nonblocking_acquire (struct __go_channel
*);
98 extern void __go_send_release (struct __go_channel
*);
100 extern void __go_send_small (struct __go_channel
*, uint64_t, _Bool
);
102 extern _Bool
__go_send_nonblocking_small (struct __go_channel
*, uint64_t);
104 extern void __go_send_big (struct __go_channel
*, const void *, _Bool
);
106 extern _Bool
__go_send_nonblocking_big (struct __go_channel
*, const void *);
108 extern _Bool
__go_receive_acquire (struct __go_channel
*, _Bool
);
110 #define RECEIVE_NONBLOCKING_ACQUIRE_DATA 0
111 #define RECEIVE_NONBLOCKING_ACQUIRE_NODATA 1
112 #define RECEIVE_NONBLOCKING_ACQUIRE_CLOSED 2
114 extern int __go_receive_nonblocking_acquire (struct __go_channel
*);
116 extern uint64_t __go_receive_small (struct __go_channel
*, _Bool
);
118 extern uint64_t __go_receive_small_closed (struct __go_channel
*, _Bool
,
121 extern void __go_receive_release (struct __go_channel
*);
123 struct __go_receive_nonblocking_small
125 /* Value read from channel, or 0. */
127 /* True if value was read from channel. */
129 /* True if channel is closed. */
133 extern struct __go_receive_nonblocking_small
134 __go_receive_nonblocking_small (struct __go_channel
*);
136 extern _Bool
__go_receive_big (struct __go_channel
*, void *, _Bool
);
138 extern _Bool
__go_receive_nonblocking_big (struct __go_channel
*, void *,
141 extern void __go_unlock_and_notify_selects (struct __go_channel
*);
143 extern _Bool
__go_builtin_closed (struct __go_channel
*);
145 extern void __go_builtin_close (struct __go_channel
*);
147 extern int __go_chan_len (struct __go_channel
*);
149 extern int __go_chan_cap (struct __go_channel
*);