forked from tmthrgd/nginx-ip-blocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_ip_blocker_rwlock.c
49 lines (40 loc) · 1.2 KB
/
ngx_http_ip_blocker_rwlock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include "ngx_ip_blocker_shm.h"
#include <assert.h> // For assert
#include <semaphore.h> // For sem_*
// rlock locks rw for reading.
ngx_int_t ngx_ip_blocker_rwlock_rlock(ngx_ip_blocker_rwlock_st *rw)
{
if (ngx_atomic_fetch_add(&rw->reader_count, 1) < -1) {
// A writer is pending, wait for it.
if (sem_wait(&rw->reader_sem) != 0) {
return NGX_ERROR;
}
}
return NGX_OK;
}
// runlock undoes a single rlock call;
// it does not affect other simultaneous readers.
// It is a run-time error if rw is not locked for reading
// on entry to runlock.
ngx_int_t ngx_ip_blocker_rwlock_runlock(ngx_ip_blocker_rwlock_st *rw)
{
int32_t r;
r = ngx_atomic_fetch_add(&rw->reader_count, -1);
if (r < 1) {
assert(r != 0 && r != -NGX_IP_BLOCKER_MAX_READERS);
// A writer is pending.
if (ngx_atomic_fetch_add(&rw->reader_wait, -1) == 1) {
// The last reader unblocks the writer.
if (sem_post(&rw->writer_sem) != 0) {
return NGX_ERROR;
}
}
}
return NGX_OK;
}