Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) Igor Sysoev |
| 4 | */ |
| 5 | |
| 6 | |
| 7 | #ifndef _NGX_SHMTX_H_INCLUDED_ |
| 8 | #define _NGX_SHMTX_H_INCLUDED_ |
| 9 | |
| 10 | |
| 11 | #include <ngx_config.h> |
| 12 | #include <ngx_core.h> |
| 13 | |
| 14 | |
| 15 | typedef struct { |
| 16 | #if (NGX_HAVE_ATOMIC_OPS) |
| 17 | ngx_atomic_t *lock; |
| 18 | #else |
| 19 | ngx_fd_t fd; |
| 20 | u_char *name; |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 21 | #endif |
| 22 | } ngx_shmtx_t; |
| 23 | |
| 24 | |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 25 | ngx_int_t ngx_shmtx_create(ngx_shmtx_t *mtx, void *addr, u_char *name); |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | #if (NGX_HAVE_ATOMIC_OPS) |
| 29 | |
| 30 | static ngx_inline ngx_uint_t |
| 31 | ngx_shmtx_trylock(ngx_shmtx_t *mtx) |
| 32 | { |
Igor Sysoev | 896b5e4 | 2007-01-11 16:07:03 +0000 | [diff] [blame^] | 33 | return (*mtx->lock == 0 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid)); |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | #define ngx_shmtx_lock(mtx) ngx_spinlock((mtx)->lock, ngx_pid, 1024) |
| 37 | |
| 38 | #define ngx_shmtx_unlock(mtx) (void) ngx_atomic_cmp_set((mtx)->lock, ngx_pid, 0) |
| 39 | |
| 40 | #define ngx_shmtx_destory(mtx) |
| 41 | |
| 42 | |
| 43 | #else |
| 44 | |
| 45 | static ngx_inline ngx_uint_t |
| 46 | ngx_shmtx_trylock(ngx_shmtx_t *mtx) |
| 47 | { |
| 48 | ngx_err_t err; |
| 49 | |
| 50 | err = ngx_trylock_fd(mtx->fd); |
| 51 | |
| 52 | if (err == 0) { |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | if (err == NGX_EAGAIN) { |
| 57 | return 0; |
| 58 | } |
| 59 | |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 60 | ngx_log_abort(err, ngx_trylock_fd_n " failed"); |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
| 64 | static ngx_inline void |
| 65 | ngx_shmtx_lock(ngx_shmtx_t *mtx) |
| 66 | { |
| 67 | ngx_err_t err; |
| 68 | |
| 69 | err = ngx_lock_fd(mtx->fd); |
| 70 | |
| 71 | if (err == 0) { |
| 72 | return; |
| 73 | } |
| 74 | |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 75 | ngx_log_abort(err, ngx_lock_fd_n " failed"); |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
| 79 | static ngx_inline void |
| 80 | ngx_shmtx_unlock(ngx_shmtx_t *mtx) |
| 81 | { |
| 82 | ngx_err_t err; |
| 83 | |
| 84 | err = ngx_unlock_fd(mtx->fd); |
| 85 | |
| 86 | if (err == 0) { |
| 87 | return; |
| 88 | } |
| 89 | |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 90 | ngx_log_abort(err, ngx_unlock_fd_n " failed"); |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
| 94 | void ngx_shmtx_destory(ngx_shmtx_t *mtx); |
| 95 | |
| 96 | #endif |
| 97 | |
| 98 | |
| 99 | #endif /* _NGX_SHMTX_H_INCLUDED_ */ |