blob: ead94e48423f95da186cf21455430d61963aa25f [file] [log] [blame]
Igor Sysoevffe71442006-02-08 15:33:12 +00001
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
14ngx_int_t
Igor Sysoev67cd3362006-11-20 08:51:45 +000015ngx_shmtx_create(ngx_shmtx_t *mtx, void *addr, u_char *name)
Igor Sysoevffe71442006-02-08 15:33:12 +000016{
17 mtx->lock = addr;
18
19 return NGX_OK;
20}
21
22#else
23
24
25ngx_int_t
Igor Sysoev67cd3362006-11-20 08:51:45 +000026ngx_shmtx_create(ngx_shmtx_t *mtx, void *addr, u_char *name)
Igor Sysoevffe71442006-02-08 15:33:12 +000027{
28 if (mtx->name) {
29
30 if (ngx_strcmp(name, mtx->name) == 0) {
31 mtx->name = name;
Igor Sysoevffe71442006-02-08 15:33:12 +000032 return NGX_OK;
33 }
34
35 ngx_shmtx_destory(mtx);
36 }
37
Igor Sysoev8e750112007-02-11 07:49:12 +000038 mtx->fd = ngx_open_file(name, NGX_FILE_RDWR, NGX_FILE_CREATE_OR_OPEN,
39 NGX_FILE_DEFAULT_ACCESS);
Igor Sysoevffe71442006-02-08 15:33:12 +000040
41 if (mtx->fd == NGX_INVALID_FILE) {
Igor Sysoev67cd3362006-11-20 08:51:45 +000042 ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
Igor Sysoevffe71442006-02-08 15:33:12 +000043 ngx_open_file_n " \"%s\" failed", name);
44 return NGX_ERROR;
45 }
46
47 if (ngx_delete_file(name) == NGX_FILE_ERROR) {
Igor Sysoev67cd3362006-11-20 08:51:45 +000048 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
Igor Sysoevffe71442006-02-08 15:33:12 +000049 ngx_delete_file_n " \"%s\" failed", name);
50 }
51
52 mtx->name = name;
Igor Sysoevffe71442006-02-08 15:33:12 +000053
54 return NGX_OK;
55}
56
57
58void
59ngx_shmtx_destory(ngx_shmtx_t *mtx)
60{
61 if (ngx_close_file(mtx->fd) == NGX_FILE_ERROR) {
Igor Sysoev67cd3362006-11-20 08:51:45 +000062 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
Igor Sysoevffe71442006-02-08 15:33:12 +000063 ngx_close_file_n " \"%s\" failed", mtx->name);
64 }
65}
66
67
68#endif