Scoop API

scoop:scoop.http.asyncio:0.1.0

src/common.scoop

Selected production source · src/common.scoop

1package scoop.http.asyncio
3import scoop.asyncio.*
5// Copies share the atomic state without another heap wrapper.
6internal struct HttpLock(val held: AtomicBoolean = AtomicBoolean(false)) {
7 private fun acquire() {
8 while (!held.compareAndSet(false, true, MemoryOrder.Acquire, MemoryOrder.Relaxed)) {}
9 }
11 private fun unlock() {
12 held.store(false, MemoryOrder.Release)
13 }
15 internal fun <T> withLock(block: () -> T): T {
16 acquire()
17 try {
18 return block()
19 }
20 finally {
21 unlock()
22 }
23 }
26internal fun <T> present(value: T?): Boolean =
27 when (value) {
28 case Some(_) -> true;
29 case None -> false
30 }
32internal fun httpJob(loop: EventLoop, active: Boolean): Job {
33 if (currentEventLoop() !== loop)
34 throw IllegalStateException(Some("HTTP operation belongs to another event loop"))
35 val job = currentJobOrNone() ?: throw IllegalStateException(Some("HTTP I/O requires a Job"))
36 if (job.isCompleted)
37 throw IllegalStateException(Some("HTTP I/O requires an active Job"))
38 if (active)
39 job.ensureActive()
40 return job
43internal fun httpCount(count: Long, available: Long) {
44 if (count < 0L)
45 throw IllegalArgumentException(Some("negative HTTP transfer count"))
46 if (count > available)
47 throw IndexOutOfBoundsException()
50internal fun httpProgress(count: Long, requested: Long) {
51 if (count < 0L || count > requested)
52 throw IllegalStateException(Some("invalid HTTP transport progress"))