1/* Copyright 2016 Google Inc.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License. */
14
15#ifndef NSYNC_PUBLIC_NSYNC_COUNTER_H_
16#define NSYNC_PUBLIC_NSYNC_COUNTER_H_
17
18#include <inttypes.h>
19#include "nsync_cpp.h"
20#include "nsync_mu.h"
21#include "nsync_atomic.h"
22#include "nsync_time.h"
23
24NSYNC_CPP_START_
25
26struct nsync_dll_element_s_;
27
28/* An nsync_counter represents an unsigned integer that can count up and down,
29 and wake waiters when zero. */
30typedef struct nsync_counter_s_ *nsync_counter;
31
32/* Return a freshly allocated nsync_counter with the specified value,
33 of NULL if an nsync_counter cannot be created.
34
35 Any non-NULL returned value should be passed to nsync_counter_free() when no
36 longer needed. */
37nsync_counter nsync_counter_new (uint32_t value);
38
39/* Free resources associated with c. Requires that c was allocated by
40 nsync_counter_new(), and no concurrent or future operations are applied to
41 c. */
42void nsync_counter_free (nsync_counter c);
43
44/* Add delta to c, and return its new value. It is a checkable runtime error
45 to decrement c below 0, or to increment c (i.e., apply a delta > 0) after a
46 waiter has waited. */
47uint32_t nsync_counter_add (nsync_counter c, int32_t delta);
48
49/* Return the current value of c. */
50uint32_t nsync_counter_value (nsync_counter c);
51
52/* Wait until c has value 0, or until abs_deadline, then return
53 the value of c. It is a checkable runtime error to increment c after
54 a waiter may have been woken due to the counter reaching zero.
55 If abs_deadline==nsync_time_no_deadline, the deadline
56 is far in the future. */
57uint32_t nsync_counter_wait (nsync_counter c, nsync_time abs_deadline);
58
59NSYNC_COUNTER_CPP_OVERLOAD_
60NSYNC_CPP_END_
61
62#endif /*NSYNC_PUBLIC_NSYNC_COUNTER_H_*/
63