Arax -8d09c51940345c86062e8ef2427c705ae66e5926
A Runtime Framework for Decoupling Applications from Heterogeneous Accelerators
Loading...
Searching...
No Matches
arax_throttle.c
Go to the documentation of this file.
1#include "arax_throttle.h"
2#include "utils/arax_assert.h"
3#include "stdio.h"
4
5#ifdef ARAX_THROTTLE_DEBUG
6void PRINT_THR(arax_throttle_s *thr, long int delta, const char *func)
7{
8 utils_spinlock_lock(&(thr->lock));
9 arax_assert(thr->print_cnt < 100000);
10 printf("#%05ld %30s(%p) ,sz: %6ld ,was: %11lu => is: %11lu, cap: %11ld, used:%11ld)\n",
11 thr->print_cnt,
12 func,
13 thr,
14 delta,
15 thr->available,
16 thr->available + delta,
17 thr->capacity,
18 thr->capacity - (thr->available + delta)
19 );
20 thr->print_cnt++;
21 utils_spinlock_unlock(&(thr->lock));
22}
23
24#else /* ifdef ARAX_THROTTLE_DEBUG */
25#define PRINT_THR(OBJ, DELTA, FUNC)
26#endif /* ifdef ARAX_THROTTLE_DEBUG */
27
28void arax_throttle_init(async_meta_s *meta, arax_throttle_s *thr, size_t a_sz, size_t t_sz)
29{
30 // error check
31 arax_assert(meta);
32 arax_assert(thr);
33 arax_assert(a_sz > 0);
34 arax_assert(t_sz > 0);
35 arax_assert(t_sz >= a_sz);
36
37 // init sizes
38 thr->available = a_sz;
39 thr->capacity = t_sz;
40 // init async
41 async_condition_init(meta, &thr->ready);
42
43 #ifdef ARAX_THROTTLE_DEBUG
44 thr->print_cnt = 0;
45 utils_spinlock_init(&(thr->lock));
46 #endif
47}
48
50 // error check
51 arax_assert(thr);
52
53 if (!sz)
54 return;
55
56 // lock critical section
57 async_condition_lock(&(thr->ready));
58
59 PRINT_THR(thr, +sz, func);
60
61 // inc available size
62 thr->available += sz;
63
64 #ifdef ARAX_THROTTLE_ENFORCE
65 // check bad use of api
66 arax_assert(thr->capacity >= thr->available);
67 #endif
68
69 // notify to stop async_condition_wait
70 async_condition_notify(&(thr->ready));
71
72 // unlock critical section
73 async_condition_unlock(&(thr->ready));
74}
75
76
78 // error check
79 arax_assert(thr);
80
81 if (!sz)
82 return;
83
84 // lock critical section
85 async_condition_lock(&(thr->ready));
86
87 #ifdef ARAX_THROTTLE_ENFORCE
88 // wait till there is space to dec coutner
89 while (thr->available < sz)
90 async_condition_wait(&(thr->ready));
91 #endif
92
93 PRINT_THR(thr, -sz, func);
94
95 // dec available size
96 thr->available -= sz;
97
98 #ifdef ARAX_THROTTLE_ENFORCE
99 // check bad use of api
100 arax_assert(thr->capacity >= thr->available);
101 #endif
102
103 // unlock critical section
104 async_condition_unlock(&(thr->ready));
105}
106
107
109{
110 // error check
111 arax_assert(thr);
112 return thr->available;
113}
114
116{
117 // error check
118 arax_assert(thr);
119 return thr->capacity;
120}
#define arax_assert(EXPR)
Definition arax_assert.h:7
#define ARAX_THROTTLE_DEBUG_FUNC(FUNC)
#define ARAX_THROTTLE_DEBUG_PARAMS
void async_condition_wait(async_condition_s *cond)
void async_condition_unlock(async_condition_s *cond)
void async_condition_notify(async_condition_s *cond)
void async_condition_init(async_meta_s *meta, async_condition_s *cond)
void async_condition_lock(async_condition_s *cond)
#define PRINT_THR(OBJ, DELTA, FUNC)
size_t arax_throttle_get_available_size(arax_throttle_s *thr)
void ARAX_THROTTLE_DEBUG_FUNC arax_throttle_size_dec(arax_throttle_s *thr, size_t sz ARAX_THROTTLE_DEBUG_PARAMS)
void ARAX_THROTTLE_DEBUG_FUNC arax_throttle_size_inc(arax_throttle_s *thr, size_t sz ARAX_THROTTLE_DEBUG_PARAMS)
void arax_throttle_init(async_meta_s *meta, arax_throttle_s *thr, size_t a_sz, size_t t_sz)
size_t arax_throttle_get_total_size(arax_throttle_s *thr)
#define utils_spinlock_lock(V)
Definition queue.c:35
#define utils_spinlock_init(V)
Definition queue.c:34
#define utils_spinlock_unlock(V)
Definition queue.c:36
async_condition_s ready