OSDN Git Service

mn10300: Add attribute enabled.
[pf3gnuchains/gcc-fork.git] / libgo / runtime / go-chan-len.c
1 /* go-chan-len.c -- the len 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 len function applied to a channel--the number of
13    elements in the buffer.  This could be done inline but I'm doing it
14    as a function for now to make it easy to change the channel
15    structure.  */
16
17 size_t
18 __go_chan_len (struct __go_channel *channel)
19 {
20   int i;
21   size_t ret;
22
23   if (channel == NULL)
24     return 0;
25
26   i = pthread_mutex_lock (&channel->lock);
27   __go_assert (i == 0);
28
29   if (channel->num_entries == 0)
30     ret = 0;
31   else if (channel->next_fetch == channel->next_store)
32     ret = 0;
33   else
34     ret = ((channel->next_store + channel->num_entries - channel->next_fetch)
35            % channel->num_entries);
36
37   i = pthread_mutex_unlock (&channel->lock);
38   __go_assert  (i == 0);
39
40   return ret;
41 }