OSDN Git Service

Update to current version of Go library.
[pf3gnuchains/gcc-fork.git] / libgo / runtime / channel.h
1 /* channel.h -- the channel type for Go.
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 #include <pthread.h>
9
10 /* This structure is used when a select is waiting for a synchronous
11    channel.  */
12
13 struct __go_channel_select
14 {
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.  */
25   _Bool *is_read;
26 };
27
28 /* A channel is a pointer to this structure.  */
29
30 struct __go_channel
31 {
32   /* A mutex to control access to the channel.  */
33   pthread_mutex_t lock;
34   /* A condition variable.  This is signalled when data is added to
35      the channel and when data is removed from the channel.  */
36   pthread_cond_t cond;
37   /* The size of elements on this channel.  */
38   size_t element_size;
39   /* True if a goroutine is waiting to send on a synchronous
40      channel.  */
41   _Bool waiting_to_send;
42   /* True if a goroutine is waiting to receive on a synchronous
43      channel.  */
44   _Bool waiting_to_receive;
45   /* True if this channel was selected for send in a select statement.
46      This looks out all other sends.  */
47   _Bool selected_for_send;
48   /* True if this channel was selected for receive in a select
49      statement.  This locks out all other receives.  */
50   _Bool selected_for_receive;
51   /* True if this channel has been closed.  */
52   _Bool is_closed;
53   /* True if at least one null value has been read from a closed
54      channel.  */
55   _Bool saw_close;
56   /* The list of select statements waiting to send on a synchronous
57      channel.  */
58   struct __go_channel_select *select_send_queue;
59   /* The list of select statements waiting to receive on a synchronous
60      channel.  */
61   struct __go_channel_select *select_receive_queue;
62   /* If a select statement is waiting for this channel, it sets these
63      pointers.  When something happens on the channel, the channel
64      locks the mutex, signals the condition, and unlocks the
65      mutex.  */
66   pthread_mutex_t *select_mutex;
67   pthread_cond_t *select_cond;
68   /* The number of entries in the circular buffer.  */
69   unsigned int num_entries;
70   /* Where to store the next value.  */
71   unsigned int next_store;
72   /* Where to fetch the next value.  If next_fetch == next_store, the
73      buffer is empty.  If next_store + 1 == next_fetch, the buffer is
74      full.  */
75   unsigned int next_fetch;
76   /* The circular buffer.  */
77   uint64_t data[];
78 };
79
80 /* The mutex used to control access to the value pointed to by the
81    __go_channel_select selected field.  No additional mutexes may be
82    acquired while this mutex is held.  */
83 extern pthread_mutex_t __go_select_data_mutex;
84
85 extern struct __go_channel *__go_new_channel (size_t, size_t);
86
87 extern _Bool __go_synch_with_select (struct __go_channel *, _Bool);
88
89 extern void __go_broadcast_to_select (struct __go_channel *);
90
91 extern void __go_send_acquire (struct __go_channel *, _Bool);
92
93 extern _Bool __go_send_nonblocking_acquire (struct __go_channel *);
94
95 extern void __go_send_release (struct __go_channel *);
96
97 extern void __go_send_small (struct __go_channel *, uint64_t, _Bool);
98
99 extern _Bool __go_send_nonblocking_small (struct __go_channel *, uint64_t);
100
101 extern void __go_send_big (struct __go_channel *, const void *, _Bool);
102
103 extern _Bool __go_send_nonblocking_big (struct __go_channel *, const void *);
104
105 extern _Bool __go_receive_acquire (struct __go_channel *, _Bool);
106
107 #define RECEIVE_NONBLOCKING_ACQUIRE_DATA 0
108 #define RECEIVE_NONBLOCKING_ACQUIRE_NODATA 1
109 #define RECEIVE_NONBLOCKING_ACQUIRE_CLOSED 2
110
111 extern int __go_receive_nonblocking_acquire (struct __go_channel *);
112
113 extern uint64_t __go_receive_small (struct __go_channel *, _Bool);
114
115 extern uint64_t __go_receive_small_closed (struct __go_channel *, _Bool,
116                                            _Bool *);
117
118 extern void __go_receive_release (struct __go_channel *);
119
120 struct __go_receive_nonblocking_small
121 {
122   /* Value read from channel, or 0.  */
123   uint64_t __val;
124   /* True if value was read from channel.  */
125   _Bool __success;
126   /* True if channel is closed.  */
127   _Bool __closed;
128 };
129
130 extern struct __go_receive_nonblocking_small
131 __go_receive_nonblocking_small (struct __go_channel *);
132
133 extern _Bool __go_receive_big (struct __go_channel *, void *, _Bool);
134
135 extern _Bool __go_receive_nonblocking_big (struct __go_channel *, void *,
136                                            _Bool *);
137
138 extern void __go_unlock_and_notify_selects (struct __go_channel *);
139
140 extern _Bool __go_builtin_closed (struct __go_channel *);
141
142 extern void __go_builtin_close (struct __go_channel *);
143
144 extern size_t __go_chan_len (struct __go_channel *);
145
146 extern size_t __go_chan_cap (struct __go_channel *);