OSDN Git Service

PR c++/51009
[pf3gnuchains/gcc-fork.git] / libgo / runtime / go-chan-cap.c
1 /* go-chan-cap.c -- the cap function applied to a channel.
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 <stddef.h>
8
9 #include "go-assert.h"
10 #include "channel.h"
11
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.  */
15
16 int
17 __go_chan_cap (struct __go_channel *channel)
18 {
19   int i;
20   int ret;
21
22   if (channel == NULL)
23     return 0;
24
25   i = pthread_mutex_lock (&channel->lock);
26   __go_assert (i == 0);
27
28   if (channel->num_entries == 0)
29     ret = 0;
30   else
31     {
32       /* One slot is always unused.  We added 1 when we created the
33          channel.  */
34       ret = channel->num_entries - 1;
35     }
36
37   i = pthread_mutex_unlock (&channel->lock);
38   __go_assert  (i == 0);
39
40   return ret;
41 }