In gcc/objc/: 2011-04-12 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / libgo / runtime / channel.h
blobcd439bf4ccb7a942b050c08c6bd0f363a3275d09
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. */
7 #include <stdint.h>
8 #include <pthread.h>
10 /* This structure is used when a select is waiting for a synchronous
11 channel. */
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. */
25 _Bool *is_read;
28 /* A channel is a pointer to this structure. */
30 struct __go_channel
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 /* The list of select statements waiting to send on a synchronous
54 channel. */
55 struct __go_channel_select *select_send_queue;
56 /* The list of select statements waiting to receive on a synchronous
57 channel. */
58 struct __go_channel_select *select_receive_queue;
59 /* If a select statement is waiting for this channel, it sets these
60 pointers. When something happens on the channel, the channel
61 locks the mutex, signals the condition, and unlocks the
62 mutex. */
63 pthread_mutex_t *select_mutex;
64 pthread_cond_t *select_cond;
65 /* The number of entries in the circular buffer. */
66 unsigned int num_entries;
67 /* Where to store the next value. */
68 unsigned int next_store;
69 /* Where to fetch the next value. If next_fetch == next_store, the
70 buffer is empty. If next_store + 1 == next_fetch, the buffer is
71 full. */
72 unsigned int next_fetch;
73 /* The circular buffer. */
74 uint64_t data[];
77 /* The mutex used to control access to the value pointed to by the
78 __go_channel_select selected field. No additional mutexes may be
79 acquired while this mutex is held. */
80 extern pthread_mutex_t __go_select_data_mutex;
82 extern struct __go_channel *__go_new_channel (size_t, size_t);
84 extern _Bool __go_synch_with_select (struct __go_channel *, _Bool);
86 extern void __go_broadcast_to_select (struct __go_channel *);
88 extern void __go_send_acquire (struct __go_channel *, _Bool);
90 extern _Bool __go_send_nonblocking_acquire (struct __go_channel *);
92 extern void __go_send_release (struct __go_channel *);
94 extern void __go_send_small (struct __go_channel *, uint64_t, _Bool);
96 extern _Bool __go_send_nonblocking_small (struct __go_channel *, uint64_t);
98 extern void __go_send_big (struct __go_channel *, const void *, _Bool);
100 extern _Bool __go_send_nonblocking_big (struct __go_channel *, const void *);
102 extern _Bool __go_receive_acquire (struct __go_channel *, _Bool);
104 #define RECEIVE_NONBLOCKING_ACQUIRE_DATA 0
105 #define RECEIVE_NONBLOCKING_ACQUIRE_NODATA 1
106 #define RECEIVE_NONBLOCKING_ACQUIRE_CLOSED 2
108 extern int __go_receive_nonblocking_acquire (struct __go_channel *);
110 extern uint64_t __go_receive_small (struct __go_channel *, _Bool);
112 extern uint64_t __go_receive_small_closed (struct __go_channel *, _Bool,
113 _Bool *);
115 extern void __go_receive_release (struct __go_channel *);
117 struct __go_receive_nonblocking_small
119 /* Value read from channel, or 0. */
120 uint64_t __val;
121 /* True if value was read from channel. */
122 _Bool __success;
123 /* True if channel is closed. */
124 _Bool __closed;
127 extern struct __go_receive_nonblocking_small
128 __go_receive_nonblocking_small (struct __go_channel *);
130 extern _Bool __go_receive_big (struct __go_channel *, void *, _Bool);
132 extern _Bool __go_receive_nonblocking_big (struct __go_channel *, void *,
133 _Bool *);
135 extern void __go_unlock_and_notify_selects (struct __go_channel *);
137 extern _Bool __go_builtin_closed (struct __go_channel *);
139 extern void __go_builtin_close (struct __go_channel *);
141 extern size_t __go_chan_len (struct __go_channel *);
143 extern size_t __go_chan_cap (struct __go_channel *);