process_shared 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/ext/helper/extconf.rb +14 -0
  2. data/ext/helper/helper.c +70 -0
  3. data/lib/mach.rb +12 -0
  4. data/lib/mach/clock.rb +24 -0
  5. data/lib/mach/error.rb +56 -0
  6. data/lib/mach/functions.rb +342 -0
  7. data/lib/mach/host.rb +28 -0
  8. data/lib/mach/port.rb +143 -0
  9. data/lib/mach/semaphore.rb +74 -0
  10. data/lib/mach/task.rb +42 -0
  11. data/lib/mach/time_spec.rb +16 -0
  12. data/lib/process_shared.rb +17 -1
  13. data/lib/process_shared/binary_semaphore.rb +18 -7
  14. data/lib/process_shared/mach.rb +93 -0
  15. data/lib/process_shared/mach/semaphore.rb +39 -0
  16. data/lib/process_shared/posix/errno.rb +40 -0
  17. data/lib/process_shared/posix/libc.rb +78 -0
  18. data/lib/process_shared/posix/semaphore.rb +125 -0
  19. data/lib/process_shared/posix/shared_array.rb +71 -0
  20. data/lib/process_shared/posix/shared_memory.rb +82 -0
  21. data/lib/process_shared/posix/time_spec.rb +13 -0
  22. data/lib/process_shared/posix/time_val.rb +23 -0
  23. data/lib/process_shared/semaphore.rb +26 -73
  24. data/lib/process_shared/shared_array.rb +3 -1
  25. data/lib/process_shared/shared_memory.rb +10 -52
  26. data/lib/process_shared/time_spec.rb +22 -0
  27. data/spec/mach/port_spec.rb +21 -0
  28. data/spec/mach/scratch.rb +67 -0
  29. data/spec/mach/scratch2.rb +78 -0
  30. data/spec/mach/semaphore_spec.rb +60 -0
  31. data/spec/mach/task_spec.rb +31 -0
  32. data/spec/process_shared/scratch.rb +21 -0
  33. data/spec/process_shared/semaphore_spec.rb +12 -11
  34. data/spec/process_shared/shared_memory_spec.rb +1 -1
  35. metadata +46 -36
  36. data/ext/libpsem/bsem.c +0 -188
  37. data/ext/libpsem/bsem.h +0 -32
  38. data/ext/libpsem/constants.c +0 -22
  39. data/ext/libpsem/constants.h +0 -18
  40. data/ext/libpsem/extconf.rb +0 -40
  41. data/ext/libpsem/mempcpy.c +0 -7
  42. data/ext/libpsem/mempcpy.h +0 -13
  43. data/ext/libpsem/mutex.c +0 -15
  44. data/ext/libpsem/mutex.h +0 -14
  45. data/ext/libpsem/psem.c +0 -15
  46. data/ext/libpsem/psem.h +0 -45
  47. data/ext/libpsem/psem_error.c +0 -46
  48. data/ext/libpsem/psem_error.h +0 -11
  49. data/ext/libpsem/psem_posix.c +0 -160
  50. data/ext/libpsem/psem_posix.h +0 -10
  51. data/lib/process_shared/bounded_semaphore.rb +0 -46
  52. data/lib/process_shared/libc.rb +0 -36
  53. data/lib/process_shared/libpsem.bundle +0 -0
  54. data/lib/process_shared/libpsem.so +0 -0
  55. data/lib/process_shared/psem.rb +0 -113
  56. data/spec/process_shared/bounded_semaphore_spec.rb +0 -48
  57. data/spec/process_shared/libc_spec.rb +0 -9
  58. data/spec/process_shared/psem_spec.rb +0 -136
@@ -1,32 +0,0 @@
1
- #ifndef __BSEM_H__
2
- #define __BSEM_H__
3
-
4
- #include "psem.h"
5
- #include "psem_error.h"
6
-
7
- struct bsem {
8
- psem_t psem;
9
- psem_t lock;
10
- int maxvalue;
11
- };
12
-
13
- typedef struct bsem bsem_t;
14
-
15
- extern size_t sizeof_bsem_t;
16
-
17
- bsem_t * bsem_alloc();
18
- void bsem_free(bsem_t *bsem);
19
-
20
- int bsem_open(bsem_t *, const char *, unsigned int, unsigned int, error_t **);
21
- int bsem_close(bsem_t *, error_t **);
22
- int bsem_unlink(const char *, error_t **);
23
-
24
- int bsem_post(bsem_t *, error_t **);
25
- int bsem_wait(bsem_t *, error_t **);
26
- int bsem_trywait(bsem_t *, error_t **);
27
- int bsem_timedwait(bsem_t *, float, error_t **);
28
-
29
- int bsem_getvalue(bsem_t *, int *, error_t **);
30
-
31
- #endif /* __BSEM_H__ */
32
-
@@ -1,22 +0,0 @@
1
- /**
2
- * Define and extern various constants defined as macros.
3
- */
4
-
5
- #include <sys/mman.h> /* PROT_*, MAP_* */
6
- #include <fcntl.h> /* O_* */
7
-
8
- #include "constants.h"
9
-
10
- int o_rdwr = O_RDWR;
11
- int o_creat = O_CREAT;
12
- int o_excl = O_EXCL;
13
-
14
- int prot_read = PROT_READ;
15
- int prot_write = PROT_WRITE;
16
- int prot_exec = PROT_EXEC;
17
- int prot_none = PROT_NONE;
18
-
19
- void * map_failed = MAP_FAILED;
20
-
21
- int map_shared = MAP_SHARED;
22
- int map_private = MAP_PRIVATE;
@@ -1,18 +0,0 @@
1
- #ifndef __CONSTANTS_H__
2
- #define __CONSTANTS_H__
3
-
4
- extern int o_rdwr;
5
- extern int o_creat;
6
- extern int o_excl;
7
-
8
- extern int prot_read;
9
- extern int prot_write;
10
- extern int prot_exec;
11
- extern int prot_none;
12
-
13
- extern void * map_failed;
14
-
15
- extern int map_shared;
16
- extern int map_private;
17
-
18
- #endif
@@ -1,40 +0,0 @@
1
- require 'mkmf'
2
-
3
- $objs = []
4
-
5
- # posix semaphores
6
- if have_func('sem_open', 'semaphore.h') ||
7
- ($libs << '-lpthread' && have_func('sem_open', 'semaphore.h'))
8
- have_func('floorf', 'math.h') or abort("Missing required floorf() in math.h")
9
- have_library('m', 'floorf')
10
-
11
- unless have_func('mempcpy', 'string.h')
12
- $objs << 'mempcpy.o'
13
- end
14
-
15
- have_library('rt', 'sem_open')
16
- else
17
- abort('Win32 or platform without sem_open not supported (yet?)')
18
- end
19
-
20
- c_sources = ['psem.c', 'psem_error.c', 'psem_posix.c', 'bsem.c', 'constants.c']
21
- $objs += ['psem.o', 'psem_error.o', 'bsem.o', 'constants.o']
22
-
23
- if respond_to? :depend_rules
24
- depend_rules <<-END
25
- psem.c: psem.h psem_posix.c
26
- psem_error.c: psem_error.h
27
-
28
- bsem.h: psem.h psem_error.h
29
- bsem.c: psem.h psem_error.h bsem.h
30
-
31
- constants.c: constants.h
32
- mempcpy.c: mempcpy.h
33
-
34
- #{$objs.map { |o| "#{o}: #{o.chomp(".o")}.c" }.join("\n")}
35
-
36
- libpsem.o: #{$objs.join(' ')}
37
- END
38
- end
39
-
40
- create_makefile('libpsem')
@@ -1,7 +0,0 @@
1
- #include "mempcpy.h"
2
-
3
- void *
4
- mempcpy(void *dest, const void *src, size_t n)
5
- {
6
- return (char *) memcpy(dest, src, n) + n;
7
- }
@@ -1,13 +0,0 @@
1
- #ifndef __MEMPCPY_H__
2
- #define __MEMPCPY_H__
3
-
4
- #ifdef HAVE_MEMPCPY
5
- #define __USE_GNU
6
- #else
7
- #include <stdlib.h>
8
- void *mempcpy(void *, const void *, size_t);
9
- #endif
10
-
11
- #include <string.h>
12
-
13
- #endif /* __MEMPCPY_H__ */
@@ -1,15 +0,0 @@
1
- #include <stdlib.h> /* malloc, free */
2
-
3
- #include "mutex.h"
4
-
5
- size_t sizeof_mutex_t = sizeof (mutex_t);
6
-
7
- mutex_t *
8
- mutex_alloc(void) {
9
- return malloc(sizeof(mutex_t));
10
- }
11
-
12
- void
13
- mutex_free(mutex_t * mutex) {
14
- free(mutex);
15
- }
@@ -1,14 +0,0 @@
1
- #ifndef __MUTEX_H__
2
- #define __MUTEX_H__
3
-
4
- #include "bsem.h"
5
-
6
- struct mutex {
7
- bsem_t *bsem;
8
- };
9
-
10
- typedef struct mutex mutex_t;
11
-
12
- extern size_t sizeof_mutex_t;
13
-
14
- #endif /* __MUTEX_H__ */
@@ -1,15 +0,0 @@
1
- #include "psem.h"
2
-
3
- int OK = 0;
4
- int ERROR = -1;
5
-
6
- int E_SOURCE_SYSTEM = 1;
7
- int E_SOURCE_PSEM = 2;
8
-
9
- int E_NAME_TOO_LONG = 1;
10
-
11
- #ifdef HAVE_SEM_OPEN
12
- #include "psem_posix.c"
13
- #endif
14
-
15
- size_t sizeof_psem_t = sizeof (psem_t);
@@ -1,45 +0,0 @@
1
- #ifndef __PSEM_H__
2
- #define __PSEM_H__
3
-
4
- /**
5
- * Portable semaphore interface focusing on cross-process use.
6
- */
7
-
8
- #include <stdlib.h> /* size_t */
9
-
10
- #ifdef HAVE_SEM_OPEN
11
- #include "psem_posix.h"
12
- #endif
13
-
14
- #include "psem_error.h"
15
-
16
- typedef struct psem psem_t;
17
-
18
- extern size_t sizeof_psem_t;
19
-
20
- extern int OK;
21
- extern int ERROR;
22
-
23
- extern int E_SOURCE_SYSTEM;
24
- extern int E_SOURCE_PSEM;
25
-
26
- extern int E_NAME_TOO_LONG;
27
-
28
- int psem_errno();
29
-
30
- psem_t * psem_alloc();
31
- void psem_free(psem_t *);
32
-
33
- int psem_open(psem_t *, const char *, unsigned int, error_t **);
34
- int psem_close(psem_t *, error_t **);
35
- int psem_unlink(const char *, error_t **);
36
-
37
- int psem_post(psem_t *, error_t **);
38
-
39
- int psem_wait(psem_t *, error_t **);
40
- int psem_trywait(psem_t *, error_t **);
41
- int psem_timedwait(psem_t *, float, error_t **);
42
-
43
- int psem_getvalue(psem_t *, int *, error_t **);
44
-
45
- #endif /* __PSEM_H__ */
@@ -1,46 +0,0 @@
1
- /**
2
- * Similar to GError from GLib.
3
- */
4
-
5
- #include <stdlib.h> /* malloc, free */
6
-
7
- #include "psem_error.h"
8
-
9
- struct error {
10
- int error_source;
11
- int error_number;
12
- };
13
-
14
- error_t *
15
- error_alloc()
16
- {
17
- return (error_t *) malloc(sizeof (error_t));
18
- }
19
-
20
- void
21
- error_free(error_t *err)
22
- {
23
- free(err);
24
- }
25
-
26
- void
27
- error_set(error_t *err, int source, int value)
28
- {
29
- err->error_source = source;
30
- err->error_number = value;
31
- }
32
-
33
- void
34
- error_new(error_t **err, int source, int value)
35
- {
36
- if (err != NULL) {
37
- if (*err == NULL) {
38
- *err = error_alloc();
39
- error_set(*err, source, value);
40
- } else {
41
- /* tried to create a new error atop an existing error... */
42
- }
43
- } else {
44
- /* error is being ignored by caller */
45
- }
46
- }
@@ -1,11 +0,0 @@
1
- #ifndef __PSEM_ERROR_H__
2
- #define __PSEM_ERROR_H__
3
-
4
- typedef struct error error_t;
5
-
6
- error_t * error_alloc();
7
- void error_free(error_t *);
8
-
9
- void error_set(error_t *, int, int);
10
-
11
- #endif /* __PSEM_ERROR_H__ */
@@ -1,160 +0,0 @@
1
- /*
2
- * A type which wraps a semaphore
3
- *
4
- * semaphore.c
5
- *
6
- * Copyright (c) 2006-2008, R Oudkerk
7
- *
8
- * All rights reserved.
9
- *
10
- * Redistribution and use in source and binary forms, with or without
11
- * modification, are permitted provided that the following conditions
12
- * are met:
13
- *
14
- * 1. Redistributions of source code must retain the above copyright
15
- * notice, this list of conditions and the following disclaimer.
16
- *
17
- * 2. Redistributions in binary form must reproduce the above
18
- * copyright notice, this list of conditions and the following
19
- * disclaimer in the documentation and/or other materials provided
20
- * with the distribution.
21
- *
22
- * 3. Neither the name of author nor the names of any contributors
23
- * may be used to endorse or promote products derived from this
24
- * software without specific prior written permission.
25
- *
26
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS"
27
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29
- * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
30
- * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
33
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
34
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36
- * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37
- * POSSIBILITY OF SUCH DAMAGE.
38
- */
39
-
40
- /*
41
- * Modifications Copyright (c) 2011, Patrick Mahoney
42
- */
43
-
44
- #include <errno.h>
45
- #include <fcntl.h> /* For O_* constants */
46
- #include <sys/stat.h> /* For mode constants */
47
- #include <semaphore.h>
48
- #include <stdlib.h> /* malloc, free */
49
- #include <math.h> /* floorf */
50
- #include <time.h> /* timespec */
51
-
52
- #include "psem.h"
53
- #include "psem_posix.h"
54
-
55
- psem_t *
56
- psem_alloc(void) {
57
- return (psem_t *) malloc(sizeof(psem_t));
58
- }
59
-
60
- void
61
- psem_free(psem_t *psem) {
62
- free(psem);
63
- }
64
-
65
- #define errcheck_val(expr, errval, err) \
66
- do { \
67
- if ((expr) == (errval)) { \
68
- error_new((err), E_SOURCE_SYSTEM, errno); \
69
- return ERROR; \
70
- } \
71
- } while (0)
72
-
73
- #define errcheck(expr, err) errcheck_val((expr), -1, (err))
74
-
75
- int
76
- psem_open(psem_t *psem, const char *name, unsigned int value, error_t **err)
77
- {
78
- errcheck_val(psem->sem = sem_open(name, O_CREAT | O_EXCL, 0600, value),
79
- SEM_FAILED,
80
- err);
81
- return OK;
82
- }
83
-
84
- int
85
- psem_close(psem_t *psem, error_t **err)
86
- {
87
- errcheck(sem_close(psem->sem), err);
88
- return OK;
89
- }
90
-
91
- int
92
- psem_unlink(const char *name, error_t **err)
93
- {
94
- errcheck(sem_unlink(name), err);
95
- return OK;
96
- }
97
-
98
- int
99
- psem_post(psem_t *psem, error_t **err)
100
- {
101
- errcheck(sem_post(psem->sem), err);
102
- return OK;
103
- }
104
-
105
- int
106
- psem_wait(psem_t *psem, error_t **err)
107
- {
108
- errcheck(sem_wait(psem->sem), err);
109
- return OK;
110
- }
111
-
112
- int
113
- psem_trywait(psem_t *psem, error_t **err)
114
- {
115
- errcheck(sem_trywait(psem->sem), err);
116
- return OK;
117
- }
118
-
119
- #define NS_PER_S (1000 * 1000 * 1000)
120
- #define US_PER_NS (1000)
121
- #define TV_NSEC_MAX (NS_PER_S - 1)
122
-
123
- int
124
- psem_timedwait(psem_t *psem, float timeout_s, error_t **err)
125
- {
126
- struct timeval now;
127
- struct timespec abs_timeout;
128
-
129
- errcheck(gettimeofday(&now, NULL), err);
130
- abs_timeout.tv_sec = now.tv_sec;
131
- abs_timeout.tv_nsec = now.tv_usec * US_PER_NS;
132
-
133
- /* Fun with rounding: careful adding reltive timeout to abs time */
134
- {
135
- time_t sec; /* relative timeout */
136
- long nsec;
137
-
138
- sec = floorf(timeout_s);
139
- nsec = floorf((timeout_s - floorf(timeout_s)) * NS_PER_S);
140
-
141
- abs_timeout.tv_sec += sec;
142
- abs_timeout.tv_nsec += nsec;
143
-
144
- while (abs_timeout.tv_nsec > TV_NSEC_MAX) {
145
- abs_timeout.tv_sec += 1;
146
- abs_timeout.tv_nsec -= NS_PER_S;
147
- }
148
- }
149
-
150
- errcheck(sem_timedwait(psem->sem, &abs_timeout), err);
151
- return OK;
152
- }
153
-
154
- int
155
- psem_getvalue(psem_t *psem, int *sval, error_t **err)
156
- {
157
- errcheck(sem_getvalue(psem->sem, sval), err);
158
- return OK;
159
- }
160
-