| 87 | } |
| 88 | |
| 89 | Future<Nothing> acquire() |
| 90 | { |
| 91 | if (!promises.empty()) { |
| 92 | // Need to wait for others to get permits first. |
| 93 | Promise<Nothing>* promise = new Promise<Nothing>(); |
| 94 | promises.push_back(promise); |
| 95 | return promise->future() |
| 96 | .onDiscard(defer(self(), &Self::discard, promise->future())); |
| 97 | } |
| 98 | |
| 99 | if (timeout.remaining() > Seconds(0)) { |
| 100 | // Need to wait a bit longer, but first one in the queue. |
| 101 | Promise<Nothing>* promise = new Promise<Nothing>(); |
| 102 | promises.push_back(promise); |
| 103 | delay(timeout.remaining(), self(), &Self::_acquire); |
| 104 | return promise->future() |
| 105 | .onDiscard(defer(self(), &Self::discard, promise->future())); |
| 106 | } |
| 107 | |
| 108 | // No need to wait! |
| 109 | timeout = Seconds(1) / permitsPerSecond; |
| 110 | return Nothing(); |
| 111 | } |
| 112 | |
| 113 | private: |
| 114 | // Not copyable, not assignable. |