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. */
10 /* This structure is used when a select is waiting for a synchronous
13 struct __go_channel_select
15 /* A pointer to the next select waiting for this channel. */
16 struct __go_channel_select
*next
;
17 /* A pointer to the channel which this select will use. This starts
18 out as NULL and is set to the first channel which synchs up with
19 this one. This variable to which this points may only be
20 accessed when __go_select_data_mutex is held. */
21 struct __go_channel
**selected
;
22 /* A pointer to a variable which must be set to true if the
23 goroutine which sets *SELECTED wants to read from the channel,
24 false if it wants to write to it. */
28 /* A channel is a pointer to this structure. */
32 /* A mutex to control access to the channel. */
34 /* A condition variable. This is signalled when data is added to
35 the channel and when data is removed from the channel. */
37 /* The size of elements on this channel. */
39 /* Number of operations on closed channel. */
40 unsigned short closed_op_count
;
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 /* True if at least one null value has been read from a closed
58 /* The list of select statements waiting to send on a synchronous
60 struct __go_channel_select
*select_send_queue
;
61 /* The list of select statements waiting to receive on a synchronous
63 struct __go_channel_select
*select_receive_queue
;
64 /* If a select statement is waiting for this channel, it sets these
65 pointers. When something happens on the channel, the channel
66 locks the mutex, signals the condition, and unlocks the
68 pthread_mutex_t
*select_mutex
;
69 pthread_cond_t
*select_cond
;
70 /* The number of entries in the circular buffer. */
71 unsigned int num_entries
;
72 /* Where to store the next value. */
73 unsigned int next_store
;
74 /* Where to fetch the next value. If next_fetch == next_store, the
75 buffer is empty. If next_store + 1 == next_fetch, the buffer is
77 unsigned int next_fetch
;
78 /* The circular buffer. */
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 /* Maximum permitted number of operations on a closed channel. */
88 #define MAX_CLOSED_OPERATIONS (0x100)
90 extern struct __go_channel
*__go_new_channel (size_t, size_t);
92 extern _Bool
__go_synch_with_select (struct __go_channel
*, _Bool
);
94 extern void __go_broadcast_to_select (struct __go_channel
*);
96 extern _Bool
__go_send_acquire (struct __go_channel
*, _Bool
);
98 #define SEND_NONBLOCKING_ACQUIRE_SPACE 0
99 #define SEND_NONBLOCKING_ACQUIRE_NOSPACE 1
100 #define SEND_NONBLOCKING_ACQUIRE_CLOSED 2
102 extern int __go_send_nonblocking_acquire (struct __go_channel
*);
104 extern void __go_send_release (struct __go_channel
*);
106 extern void __go_send_small (struct __go_channel
*, uint64_t, _Bool
);
108 extern _Bool
__go_send_nonblocking_small (struct __go_channel
*, uint64_t);
110 extern void __go_send_big (struct __go_channel
*, const void *, _Bool
);
112 extern _Bool
__go_send_nonblocking_big (struct __go_channel
*, const void *);
114 extern _Bool
__go_receive_acquire (struct __go_channel
*, _Bool
);
116 #define RECEIVE_NONBLOCKING_ACQUIRE_DATA 0
117 #define RECEIVE_NONBLOCKING_ACQUIRE_NODATA 1
118 #define RECEIVE_NONBLOCKING_ACQUIRE_CLOSED 2
120 extern int __go_receive_nonblocking_acquire (struct __go_channel
*);
122 extern uint64_t __go_receive_small (struct __go_channel
*, _Bool
);
124 extern void __go_receive_release (struct __go_channel
*);
126 struct __go_receive_nonblocking_small
132 extern struct __go_receive_nonblocking_small
133 __go_receive_nonblocking_small (struct __go_channel
*);
135 extern void __go_receive_big (struct __go_channel
*, void *, _Bool
);
137 extern _Bool
__go_receive_nonblocking_big (struct __go_channel
*, void *);
139 extern void __go_unlock_and_notify_selects (struct __go_channel
*);
141 extern _Bool
__go_builtin_closed (struct __go_channel
*);
143 extern void __go_builtin_close (struct __go_channel
*);
145 extern size_t __go_chan_len (struct __go_channel
*);
147 extern size_t __go_chan_cap (struct __go_channel
*);