remote_client_session_semaphore.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "remote_client_session_semaphore.h"
  2. #include <util/stream/output.h>
  3. #include <util/system/yassert.h>
  4. using namespace NBus;
  5. using namespace NBus::NPrivate;
  6. TRemoteClientSessionSemaphore::TRemoteClientSessionSemaphore(TAtomicBase limit, const char* name)
  7. : Name(name)
  8. , Limit(limit)
  9. , Current(0)
  10. , StopSignal(0)
  11. {
  12. Y_ABORT_UNLESS(limit > 0, "limit must be > 0");
  13. Y_UNUSED(Name);
  14. }
  15. TRemoteClientSessionSemaphore::~TRemoteClientSessionSemaphore() {
  16. Y_ABORT_UNLESS(AtomicGet(Current) == 0);
  17. }
  18. bool TRemoteClientSessionSemaphore::TryAcquire() {
  19. if (!TryWait()) {
  20. return false;
  21. }
  22. AtomicIncrement(Current);
  23. return true;
  24. }
  25. bool TRemoteClientSessionSemaphore::TryWait() {
  26. if (AtomicGet(Current) < Limit)
  27. return true;
  28. if (Y_UNLIKELY(AtomicGet(StopSignal)))
  29. return true;
  30. return false;
  31. }
  32. void TRemoteClientSessionSemaphore::Acquire() {
  33. Wait();
  34. Increment();
  35. }
  36. void TRemoteClientSessionSemaphore::Increment() {
  37. IncrementMultiple(1);
  38. }
  39. void TRemoteClientSessionSemaphore::IncrementMultiple(TAtomicBase count) {
  40. AtomicAdd(Current, count);
  41. Updated();
  42. }
  43. void TRemoteClientSessionSemaphore::Release() {
  44. ReleaseMultiple(1);
  45. }
  46. void TRemoteClientSessionSemaphore::ReleaseMultiple(TAtomicBase count) {
  47. AtomicSub(Current, count);
  48. Updated();
  49. }
  50. void TRemoteClientSessionSemaphore::Stop() {
  51. AtomicSet(StopSignal, 1);
  52. Updated();
  53. }