<commit_before>#ifndef _CEPH_THROTTLE_H
#define _CEPH_THROTTLE_H

#include "Mutex.h"
#include "Cond.h"

class Throttle {
  int64_t count, max, waiting;
  Mutex lock;
  Cond cond;
  
public:
  Throttle(int64_t m = 0) : count(0), max(m), waiting(0),
			  lock("Throttle::lock") {}

private:
  void _reset_max(int64_t m) {
    if (m < max)
      cond.SignalOne();
    max = m;
  }
  bool _should_wait(int64_t c) {
    return
      max &&
      ((c < max && count + c > max) ||   // normally stay under max
       (c >= max && count > max));       // except for large c
  }
  bool _wait(int64_t c) {
    bool waited = false;
    if (_should_wait(c)) {
      waiting += c;
      do {
	waited = true;
	cond.Wait(lock);
      } while (_should_wait(c));
      waiting -= c;

      // wake up the next guy
      if (waiting)
	cond.SignalOne();
    }
    return waited;
  }

public:
  int64_t get_current() {
    Mutex::Locker l(lock);
    return count;
  }

  int64_t get_max() { return max; }

  bool wait(int64_t m = 0) {
    Mutex::Locker l(lock);
    if (m)
      _reset_max(m);
    return _wait(0);
  }

  int64_t take(int64_t c = 1) {
    Mutex::Locker l(lock);
    count += c;
    return count;
  }

  bool get(int64_t c = 1, int64_t m = 0) {
    Mutex::Locker l(lock);
    if (m)
      _reset_max(m);
    bool waited = _wait(c);
    count += c;
    return waited;
  }

  int64_t put(int64_t c = 1) {
    Mutex::Locker l(lock);
    cond.SignalOne();
    count -= c;
    assert(c>=0); //if count goes negative, we failed somewhere!
    return count;
  }
};


#endif<commit_msg>throttle: fix assert count to actually use count<commit_after>#ifndef _CEPH_THROTTLE_H
#define _CEPH_THROTTLE_H

#include "Mutex.h"
#include "Cond.h"

class Throttle {
  int64_t count, max, waiting;
  Mutex lock;
  Cond cond;
  
public:
  Throttle(int64_t m = 0) : count(0), max(m), waiting(0),
			  lock("Throttle::lock") {}

private:
  void _reset_max(int64_t m) {
    if (m < max)
      cond.SignalOne();
    max = m;
  }
  bool _should_wait(int64_t c) {
    return
      max &&
      ((c < max && count + c > max) ||   // normally stay under max
       (c >= max && count > max));       // except for large c
  }
  bool _wait(int64_t c) {
    bool waited = false;
    if (_should_wait(c)) {
      waiting += c;
      do {
	waited = true;
	cond.Wait(lock);
      } while (_should_wait(c));
      waiting -= c;

      // wake up the next guy
      if (waiting)
	cond.SignalOne();
    }
    return waited;
  }

public:
  int64_t get_current() {
    Mutex::Locker l(lock);
    return count;
  }

  int64_t get_max() { return max; }

  bool wait(int64_t m = 0) {
    Mutex::Locker l(lock);
    if (m)
      _reset_max(m);
    return _wait(0);
  }

  int64_t take(int64_t c = 1) {
    Mutex::Locker l(lock);
    count += c;
    return count;
  }

  bool get(int64_t c = 1, int64_t m = 0) {
    Mutex::Locker l(lock);
    if (m)
      _reset_max(m);
    bool waited = _wait(c);
    count += c;
    return waited;
  }

  int64_t put(int64_t c = 1) {
    Mutex::Locker l(lock);
    cond.SignalOne();
    count -= c;
    assert(count >= 0); //if count goes negative, we failed somewhere!
    return count;
  }
};


#endif
{'fraction_non_alphanumeric': 0.14267795547193476, 'fraction_numerical': 0.02383192223267482, 'mean_word_length': 2.3438155136268346, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2843732', 'n_tokens_mistral': 1314, 'n_tokens_neox': 1264, 'n_words': 321}