summaryrefslogtreecommitdiffhomepage
path: root/src/core/ngx_spinlock.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/ngx_spinlock.c')
-rw-r--r--src/core/ngx_spinlock.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/core/ngx_spinlock.c b/src/core/ngx_spinlock.c
new file mode 100644
index 000000000..c3c65d303
--- /dev/null
+++ b/src/core/ngx_spinlock.c
@@ -0,0 +1,29 @@
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+
+
+void ngx_spinlock(ngx_atomic_t *lock, ngx_uint_t spin)
+{
+ ngx_uint_t tries;
+
+ tries = 0;
+
+ for ( ;; ) {
+
+ if (*lock) {
+ if (ngx_ncpu > 1 && tries++ < spin) {
+ continue;
+ }
+
+ ngx_sched_yield();
+
+ tries = 0;
+
+ } else {
+ if (ngx_atomic_cmp_set(lock, 0, 1)) {
+ return;
+ }
+ }
+ }
+}