blob: 78d7424f5943938b006da7fb00105793351cbbf2 [file] [log] [blame]
Igor Sysoevd90282d2004-09-28 08:34:51 +00001
2/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Maxim Konovalovf8d59e32012-01-18 15:07:43 +00004 * Copyright (C) Nginx, Inc.
Igor Sysoevd90282d2004-09-28 08:34:51 +00005 */
6
7
Igor Sysoev078d1b22004-06-30 15:30:41 +00008#ifndef _NGX_ATOMIC_H_INCLUDED_
9#define _NGX_ATOMIC_H_INCLUDED_
10
11
12#include <ngx_config.h>
13#include <ngx_core.h>
14
15
Igor Sysoev85080d02004-09-22 16:18:21 +000016#define NGX_HAVE_ATOMIC_OPS 1
17
Igor Sysoev78452232005-10-12 13:50:36 +000018typedef int32_t ngx_atomic_int_t;
19typedef uint32_t ngx_atomic_uint_t;
Igor Sysoev11d75322005-03-01 15:20:36 +000020typedef volatile ngx_atomic_uint_t ngx_atomic_t;
Igor Sysoev29e7d8c2009-09-25 20:25:47 +000021#define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
Igor Sysoev078d1b22004-06-30 15:30:41 +000022
23
Igor Sysoev1b735832004-11-11 14:07:14 +000024#if defined( __WATCOMC__ ) || defined( __BORLANDC__ ) || ( _MSC_VER >= 1300 )
Igor Sysoev078d1b22004-06-30 15:30:41 +000025
Igor Sysoev85080d02004-09-22 16:18:21 +000026/* the new SDK headers */
27
Igor Sysoev078d1b22004-06-30 15:30:41 +000028#define ngx_atomic_cmp_set(lock, old, set) \
Igor Sysoevc31a9bb2005-11-26 10:11:11 +000029 ((ngx_atomic_uint_t) InterlockedCompareExchange((long *) lock, set, old) \
30 == old)
Igor Sysoev078d1b22004-06-30 15:30:41 +000031
32#else
33
Igor Sysoev85080d02004-09-22 16:18:21 +000034/* the old MS VC6.0SP2 SDK headers */
35
Igor Sysoev078d1b22004-06-30 15:30:41 +000036#define ngx_atomic_cmp_set(lock, old, set) \
37 (InterlockedCompareExchange((void **) lock, (void *) set, (void *) old) \
38 == (void *) old)
39
40#endif
41
42
Igor Sysoev78452232005-10-12 13:50:36 +000043#define ngx_atomic_fetch_add(p, add) InterlockedExchangeAdd((long *) p, add)
44
45
Igor Sysoevc2068d02005-10-19 12:33:58 +000046#define ngx_memory_barrier()
47
48
Igor Sysoev875893b2007-05-05 05:50:41 +000049#if defined( __BORLANDC__ ) || ( __WATCOMC__ < 1230 )
50
51/*
52 * Borland C++ 5.5 (tasm32) and Open Watcom C prior to 1.3
53 * do not understand the "pause" instruction
54 */
55
Igor Sysoevffe71442006-02-08 15:33:12 +000056#define ngx_cpu_pause()
57#else
58#define ngx_cpu_pause() __asm { pause }
59#endif
60
61
62void ngx_spinlock(ngx_atomic_t *lock, ngx_atomic_int_t value, ngx_uint_t spin);
Igor Sysoev078d1b22004-06-30 15:30:41 +000063
64#define ngx_trylock(lock) (*(lock) == 0 && ngx_atomic_cmp_set(lock, 0, 1))
65#define ngx_unlock(lock) *(lock) = 0
66
67
68#endif /* _NGX_ATOMIC_H_INCLUDED_ */