Line data Source code
1 : /*
2 : +----------------------------------------------------------------------+
3 : | COLOPL PHP TimeShifter. |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) COLOPL, Inc. |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | info@colopl.co.jp so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Author: Go Kudo <g-kudo@colopl.co.jp> |
16 : +----------------------------------------------------------------------+
17 : */
18 :
19 : #include "php.h"
20 : #include "shared_memory.h"
21 :
22 148 : bool sm_init(sm_t *sm, size_t size) {
23 148 : sm->size = size;
24 :
25 148 : if ((sm->data = mmap(NULL, sm->size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0)) == MAP_FAILED) {
26 0 : return false;
27 : }
28 :
29 148 : if (sem_init(&sm->semaphore, 1, 1) != 0) {
30 0 : return false;
31 : }
32 :
33 148 : return sm;
34 : }
35 :
36 1500 : void sm_read(sm_t *sm, void *dest) {
37 1500 : memcpy(dest, sm->data, sm->size);
38 1500 : }
39 :
40 356 : bool sm_write(sm_t *sm, void *src) {
41 356 : if (!sm->data) {
42 0 : return false;
43 : }
44 :
45 356 : if (sem_wait(&sm->semaphore) != 0) {
46 0 : return false;
47 : }
48 :
49 356 : memcpy(sm->data, src, sm->size);
50 :
51 356 : if (sem_post(&sm->semaphore) != 0) {
52 0 : return false;
53 : }
54 :
55 356 : return true;
56 : }
57 :
58 148 : bool sm_free(sm_t *sm) {
59 148 : if (!sm->data) {
60 0 : return false;
61 : }
62 :
63 148 : if (munmap(sm->data, sm->size) != 0) {
64 0 : return false;
65 : }
66 :
67 148 : if (sem_destroy(&sm->semaphore) != 0) {
68 0 : return false;
69 : }
70 :
71 148 : return true;
72 : }
|