Skip to content

Commit d3f252c

Browse files
author
Michael Kipper
committed
Review fixes
1 parent 3e04a43 commit d3f252c

File tree

9 files changed

+94
-47
lines changed

9 files changed

+94
-47
lines changed

ext/semian/simple_integer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ semian_simple_integer_increment(int argc, VALUE *argv, VALUE self)
9797
rb_scan_args(argc, argv, "01", &val);
9898

9999
int value = check_increment_arg(val);
100-
if (perform_semop(res->sem_id, 0, value, SEM_UNDO, NULL) == -1) {
100+
if (perform_semop(res->sem_id, 0, value, 0, NULL) == -1) {
101101
rb_raise(eInternal, "error incrementing simple integer, errno: %d (%s)", errno, strerror(errno));
102102
}
103103

ext/semian/sliding_window.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ static const rb_data_type_t semian_simple_sliding_window_type = {
2828
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
2929
};
3030

31-
static void init_fn(void* ptr)
32-
{
33-
semian_simple_sliding_window_shared_t* res = (semian_simple_sliding_window_shared_t*)ptr;
34-
res->max_size = 0;
35-
res->length = 0;
36-
res->start = 0;
37-
}
38-
3931
static int
4032
check_max_size_arg(VALUE max_size)
4133
{
@@ -93,7 +85,10 @@ check_scale_factor_arg(VALUE scale_factor)
9385
static VALUE
9486
grow_window(int sem_id, semian_simple_sliding_window_shared_t* window, int new_max_size)
9587
{
96-
if (new_max_size > SLIDING_WINDOW_MAX_SIZE) return Qnil;
88+
if (new_max_size > SLIDING_WINDOW_MAX_SIZE) {
89+
sem_meta_unlock(sem_id);
90+
rb_raise(rb_eArgError, "Cannot grow window to %d (MAX_SIZE=%d)", new_max_size, SLIDING_WINDOW_MAX_SIZE);
91+
}
9792

9893
int end = window->max_size ? (window->start + window->length) % window->max_size : 0;
9994
dprintf("Growing window - sem_id:%d start:%d end:%d length:%d max_size:%d new_max_size:%d", sem_id, window->start, end, window->length, window->max_size, new_max_size);
@@ -127,7 +122,10 @@ static void swap(int *a, int *b) {
127122
static VALUE
128123
shrink_window(int sem_id, semian_simple_sliding_window_shared_t* window, int new_max_size)
129124
{
130-
if (new_max_size > SLIDING_WINDOW_MAX_SIZE) return Qnil;
125+
if (new_max_size > SLIDING_WINDOW_MAX_SIZE) {
126+
sem_meta_unlock(sem_id);
127+
rb_raise(rb_eArgError, "Cannot shrink window to %d (MAX_SIZE=%d)", new_max_size, SLIDING_WINDOW_MAX_SIZE);
128+
}
131129

132130
int new_length = (new_max_size > window->length) ? window->length : new_max_size;
133131

@@ -256,7 +254,7 @@ semian_simple_sliding_window_initialize(VALUE self, VALUE name, VALUE max_size,
256254

257255
dprintf("Initializing simple sliding window '%s' (key: %lu)", buffer, res->key);
258256
res->sem_id = initialize_single_semaphore(res->key, SEM_DEFAULT_PERMISSIONS, 1);
259-
res->shmem = get_or_create_shared_memory(res->key, init_fn);
257+
res->shmem = get_or_create_shared_memory(res->key);
260258
res->error_threshold = check_max_size_arg(max_size);
261259
res->scale_factor = check_scale_factor_arg(scale_factor);
262260

@@ -396,8 +394,8 @@ semian_simple_sliding_window_clear(VALUE self)
396394
return self;
397395
}
398396

397+
#if 0
399398
// Handy for debugging the sliding window, but too noisy for regular debugging.
400-
/*
401399
static void dprint_window(semian_simple_sliding_window_shared_t *window)
402400
{
403401
dprintf("---");
@@ -407,7 +405,7 @@ static void dprint_window(semian_simple_sliding_window_shared_t *window)
407405
}
408406
dprintf("---");
409407
}
410-
*/
408+
#endif
411409

412410
VALUE
413411
semian_simple_sliding_window_reject(VALUE self)

ext/semian/sysv_semaphores.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ wait_for_new_semaphore_set(uint64_t key, long permissions)
214214
union semun sem_opts;
215215
sem_opts.buf = &sem_ds;
216216

217-
int sem_id = semget(key, 1, permissions);
217+
int sem_id = semget((key_t)key, 1, permissions);
218218
if (sem_id == -1){
219219
raise_semian_syscall_error("semget()", errno);
220220
}

ext/semian/sysv_shared_memory.c

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,33 @@
22

33
#include "util.h"
44

5-
#define TIMEOUT_MS (5 * 1e6)
6-
#define WAIT_MS (10)
7-
#define RETRIES (TIMEOUT_MS / WAIT_MS)
8-
95
void*
10-
wait_for_shared_memory(uint64_t key)
11-
{
12-
for (int i = 0; i < RETRIES; ++i) {
13-
int shmid = shmget(key, SHM_DEFAULT_SIZE, SHM_DEFAULT_PERMISSIONS);
14-
if (shmid != -1) {
15-
return shmat(shmid, NULL, 0);
16-
}
17-
usleep(WAIT_MS);
18-
}
19-
20-
rb_raise(rb_eArgError, "could not get shared memory");
21-
}
22-
23-
void*
24-
get_or_create_shared_memory(uint64_t key, shared_memory_init_fn fn)
6+
get_or_create_shared_memory(uint64_t key)
257
{
268
void* shmem = NULL;
279
if (!key) return NULL;
2810

2911
dprintf("Creating shared memory (key: %lu)", key);
3012
int shmid = shmget(key, SHM_DEFAULT_SIZE, IPC_CREAT | IPC_EXCL | SHM_DEFAULT_PERMISSIONS);
3113
if (shmid != -1) {
32-
dprintf("Created shared memory (key: %lu)", key);
33-
14+
dprintf("Created shared memory (key:%lu sem_id:%d)", key, shmid);
3415
shmem = shmat(shmid, NULL, 0);
3516
if (shmem == (void*)-1) {
3617
rb_raise(rb_eArgError, "could not get shared memory (%s)", strerror(errno));
3718
}
3819

39-
if (fn) fn(shmem);
40-
4120
shmctl(key, IPC_RMID, NULL);
4221
} else {
43-
shmem = wait_for_shared_memory(key);
22+
shmid = shmget(key, SHM_DEFAULT_SIZE, SHM_DEFAULT_PERMISSIONS);
23+
if (shmid == -1) {
24+
rb_raise(rb_eArgError, "could not get shared memory (%s)", strerror(errno));
25+
}
26+
27+
dprintf("Got shared memory (key:%lu sem_id:%d)", key, shmid);
28+
shmem = shmat(shmid, NULL, 0);
29+
if (shmem == (void*)-1) {
30+
rb_raise(rb_eArgError, "could not get shared memory (%s)", strerror(errno));
31+
}
4432
}
4533

4634
return shmem;

ext/semian/sysv_shared_memory.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88

99
// Default permissions for shared memory
1010
#define SHM_DEFAULT_PERMISSIONS 0660
11-
#define SHM_DEFAULT_SIZE 1024
12-
13-
typedef void (*shared_memory_init_fn)(void*);
11+
#define SHM_DEFAULT_SIZE 4096
1412

1513
void*
16-
get_or_create_shared_memory(uint64_t key, shared_memory_init_fn fn);
14+
get_or_create_shared_memory(uint64_t key);
1715

1816
void
1917
free_shared_memory(void* key);

ext/semian/test/Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Makefile
2+
3+
CC := gcc -std=gnu99 -I. -I..
4+
TESTS := types_test
5+
6+
all : test
7+
8+
test : types_test
9+
10+
types_test : types_test.c
11+
$(CC) -o $@ $<
12+
./$@
13+
14+
clean :
15+
rm -f *.o
16+
rm -f $(TESTS)
17+
18+
.PHONY : all test types_test clean

ext/semian/test/types_test.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#include "types.h"
5+
6+
void assert_equal_int(int actual, int expected, const char *message)
7+
{
8+
if (actual != expected) {
9+
fprintf(stderr, "Error: got %d, expected %d (%s)\n", actual, expected, message);
10+
exit(EXIT_FAILURE);
11+
}
12+
}
13+
14+
void assert_le_int(int actual, int expected, const char *message)
15+
{
16+
if (actual > expected) {
17+
fprintf(stderr, "Error: got %d, which is greater than %d (%s)\n", actual, expected, message);
18+
exit(EXIT_FAILURE);
19+
}
20+
}
21+
22+
void test_sliding_window()
23+
{
24+
semian_simple_sliding_window_shared_t window;
25+
assert_le_int(sizeof(window), 4096, "window size is greater than a page");
26+
assert_equal_int(sizeof(window.data), SLIDING_WINDOW_MAX_SIZE * sizeof(int), "window data size");
27+
}
28+
29+
int main(int argc, char **argv)
30+
{
31+
printf("Info: Running test\n");
32+
33+
test_sliding_window();
34+
35+
return EXIT_SUCCESS;
36+
}

ext/semian/types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ For custom type definitions specific to semian
1010
#include <sys/sem.h>
1111
#include <sys/time.h>
1212

13-
#define SLIDING_WINDOW_MAX_SIZE 4096
13+
#define SLIDING_WINDOW_MAX_SIZE 1000
1414

1515
// For sysV semop syscals
1616
// see man semop

test/simple_sliding_window_test.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,18 @@ def test_scale_factor
218218
end
219219
end
220220

221-
def test_max_size
222-
8192.times do |i|
223-
@sliding_window << i
221+
def test_huge_sliding_window
222+
id = Time.now.strftime('%H:%M:%S.%N')
223+
window = ::Semian::ThreadSafe::SlidingWindow.new(id, max_size: 1000)
224+
4000.times do |i|
225+
window << i
226+
end
227+
end
228+
229+
def test_huge_sliding_window_fails
230+
id = Time.now.strftime('%H:%M:%S.%N')
231+
assert_raises ArgumentError do
232+
::Semian::ThreadSafe::SlidingWindow.new(id, max_size: 1001)
224233
end
225234
end
226235

0 commit comments

Comments
 (0)