Fix PR47707
[official-gcc.git] / libgo / runtime / go-chan-cap.c
blobdf603bf102f2ad5c70794f10f1001a4e410cdb24
1 /* go-chan-cap.c -- the cap function applied to a channel.
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 <stddef.h>
9 #include "go-assert.h"
10 #include "channel.h"
12 /* Return the cap function applied to a channel--the size of the
13 buffer. This could be done inline but I'm doing it as a function
14 for now to make it easy to change the channel structure. */
16 size_t
17 __go_chan_cap (struct __go_channel *channel)
19 int i;
20 size_t ret;
22 if (channel == NULL)
23 return 0;
25 i = pthread_mutex_lock (&channel->lock);
26 __go_assert (i == 0);
28 if (channel->num_entries == 0)
29 ret = 0;
30 else
32 /* One slot is always unused. We added 1 when we created the
33 channel. */
34 ret = channel->num_entries - 1;
37 i = pthread_mutex_unlock (&channel->lock);
38 __go_assert (i == 0);
40 return ret;