28 KineticCountingSemaphore * sem = calloc(1,
sizeof(KineticCountingSemaphore));
29 if (sem == NULL) {
return NULL; }
30 pthread_mutex_init(&sem->mutex, NULL);
31 pthread_cond_init(&sem->available, NULL);
41 pthread_mutex_lock(&sem->mutex);
45 while (sem->count == 0) {
46 pthread_cond_wait(&sem->available, &sem->mutex);
51 uint32_t before = sem->count--;
52 uint32_t after = sem->count;
53 uint32_t waiting = sem->num_waiting;
55 pthread_mutex_unlock(&sem->mutex);
57 LOGF3(
"Concurrent ops throttle -- TAKE: %u => %u (waiting=%u)", before, after, waiting);
63 pthread_mutex_lock(&sem->mutex);
65 if (sem->count == 0 && sem->num_waiting > 0) {
66 pthread_cond_signal(&sem->available);
69 uint32_t before = sem->count++;
70 uint32_t after = sem->count;
71 uint32_t waiting = sem->num_waiting;
73 pthread_mutex_unlock(&sem->mutex);
75 LOGF3(
"Concurrent ops throttle -- GIVE: %u => %u (waiting=%u)", before, after, waiting);
82 pthread_mutex_destroy(&sem->mutex);
83 pthread_cond_destroy(&sem->available);
void KineticCountingSemaphore_Destroy(KineticCountingSemaphore *const sem)
void KineticCountingSemaphore_Take(KineticCountingSemaphore *const sem)
void KineticCountingSemaphore_Give(KineticCountingSemaphore *const sem)
#define KINETIC_ASSERT(cond)
#define LOGF3(message,...)
KineticCountingSemaphore * KineticCountingSemaphore_Create(uint32_t counts)