upstream nginx-0.7.42
[nginx.git] / nginx / src / core / ngx_shmtx.c
1
2 /*
3  * Copyright (C) Igor Sysoev
4  */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9
10
11 #if (NGX_HAVE_ATOMIC_OPS)
12
13
14 ngx_int_t
15 ngx_shmtx_create(ngx_shmtx_t *mtx, void *addr, u_char *name)
16 {
17     mtx->lock = addr;
18
19     return NGX_OK;
20 }
21
22 #else
23
24
25 ngx_int_t
26 ngx_shmtx_create(ngx_shmtx_t *mtx, void *addr, u_char *name)
27 {
28     if (mtx->name) {
29
30         if (ngx_strcmp(name, mtx->name) == 0) {
31             mtx->name = name;
32             return NGX_OK;
33         }
34
35         ngx_shmtx_destory(mtx);
36     }
37
38     mtx->fd = ngx_open_file(name, NGX_FILE_RDWR, NGX_FILE_CREATE_OR_OPEN,
39                             NGX_FILE_DEFAULT_ACCESS);
40
41     if (mtx->fd == NGX_INVALID_FILE) {
42         ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
43                       ngx_open_file_n " \"%s\" failed", name);
44         return NGX_ERROR;
45     }
46
47     if (ngx_delete_file(name) == NGX_FILE_ERROR) {
48         ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
49                       ngx_delete_file_n " \"%s\" failed", name);
50     }
51
52     mtx->name = name;
53
54     return NGX_OK;
55 }
56
57
58 void
59 ngx_shmtx_destory(ngx_shmtx_t *mtx)
60 {
61     if (ngx_close_file(mtx->fd) == NGX_FILE_ERROR) {
62         ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
63                       ngx_close_file_n " \"%s\" failed", mtx->name);
64     }
65 }
66
67
68 #endif