Commit 8dfb066b authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Added locking to C interface (using it remains on the TODO list)

parent a8c10f92
...@@ -56,7 +56,7 @@ int main(int argc, char *argv[]) { ...@@ -56,7 +56,7 @@ int main(int argc, char *argv[]) {
char *filename = argv[1]; char *filename = argv[1];
printf("C decoder reading from %s\n", filename); printf("C decoder reading from %s\n", filename);
fd = open(filename, O_RDONLY); fd = open(filename, O_RDONLY);
decoder = labcomm_decoder_new(labcomm_fd_reader, &fd); decoder = labcomm_decoder_new(labcomm_fd_reader, &fd, NULL, NULL);
if (!decoder) { if (!decoder) {
printf("Failed to allocate decoder %s:%d\n", __FUNCTION__, __LINE__); printf("Failed to allocate decoder %s:%d\n", __FUNCTION__, __LINE__);
return 1; return 1;
......
...@@ -12,7 +12,7 @@ int main(int argc, char *argv[]) { ...@@ -12,7 +12,7 @@ int main(int argc, char *argv[]) {
char *filename = argv[1]; char *filename = argv[1];
printf("C encoder writing to %s\n", filename); printf("C encoder writing to %s\n", filename);
fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644); fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
encoder = labcomm_encoder_new(labcomm_fd_writer, &fd); encoder = labcomm_encoder_new(labcomm_fd_writer, &fd, NULL, NULL);
labcomm_encoder_register_simple_theTwoInts(encoder); labcomm_encoder_register_simple_theTwoInts(encoder);
labcomm_encoder_register_simple_anotherTwoInts(encoder); labcomm_encoder_register_simple_anotherTwoInts(encoder);
labcomm_encoder_register_simple_IntString(encoder); labcomm_encoder_register_simple_IntString(encoder);
......
...@@ -381,8 +381,10 @@ no_end: ...@@ -381,8 +381,10 @@ no_end:
} }
labcomm_encoder_t *labcomm_encoder_new( labcomm_encoder_t *labcomm_encoder_new(
const struct labcomm_writer_action action, const struct labcomm_writer_action writer,
void *writer_context) void *writer_context,
const struct labcomm_lock_action *lock,
void *lock_context)
{ {
labcomm_encoder_t *result = malloc(sizeof(labcomm_encoder_t)); labcomm_encoder_t *result = malloc(sizeof(labcomm_encoder_t));
if (result) { if (result) {
...@@ -396,13 +398,15 @@ labcomm_encoder_t *labcomm_encoder_new( ...@@ -396,13 +398,15 @@ labcomm_encoder_t *labcomm_encoder_new(
context->by_section = NULL; context->by_section = NULL;
#endif #endif
result->context = context; result->context = context;
result->writer.context = writer_context;
result->writer.data = 0; result->writer.data = 0;
result->writer.data_size = 0; result->writer.data_size = 0;
result->writer.count = 0; result->writer.count = 0;
result->writer.pos = 0; result->writer.pos = 0;
result->writer.error = 0; result->writer.error = 0;
result->writer.action = action; result->writer.action = writer;
result->writer.context = writer_context;
result->lock.action = lock;
result->lock.context = lock_context;
result->writer.on_error = on_error_fprintf; result->writer.on_error = on_error_fprintf;
result->do_register = do_encoder_register; result->do_register = do_encoder_register;
result->do_encode = do_encode; result->do_encode = do_encode;
...@@ -584,7 +588,7 @@ static int do_decode_one(labcomm_decoder_t *d) ...@@ -584,7 +588,7 @@ static int do_decode_one(labcomm_decoder_t *d)
/* TODO: should the labcomm_dynamic_buffer_writer be /* TODO: should the labcomm_dynamic_buffer_writer be
a permanent part of labcomm_decoder? */ a permanent part of labcomm_decoder? */
labcomm_encoder_t *e = labcomm_encoder_new( labcomm_encoder_t *e = labcomm_encoder_new(
labcomm_dynamic_buffer_writer, NULL); labcomm_dynamic_buffer_writer, NULL, NULL, NULL);
labcomm_signature_t signature; labcomm_signature_t signature;
labcomm_sample_entry_t *entry = NULL; labcomm_sample_entry_t *entry = NULL;
int index, err; int index, err;
...@@ -650,8 +654,10 @@ static int do_decode_one(labcomm_decoder_t *d) ...@@ -650,8 +654,10 @@ static int do_decode_one(labcomm_decoder_t *d)
} }
labcomm_decoder_t *labcomm_decoder_new( labcomm_decoder_t *labcomm_decoder_new(
const struct labcomm_reader_action action, const struct labcomm_reader_action reader,
void *reader_context) void *reader_context,
const struct labcomm_lock_action *lock,
void *lock_context)
{ {
labcomm_decoder_t *result = malloc(sizeof(labcomm_decoder_t)); labcomm_decoder_t *result = malloc(sizeof(labcomm_decoder_t));
if (result) { if (result) {
...@@ -659,13 +665,15 @@ labcomm_decoder_t *labcomm_decoder_new( ...@@ -659,13 +665,15 @@ labcomm_decoder_t *labcomm_decoder_new(
(labcomm_decoder_context_t*)malloc(sizeof(labcomm_decoder_context_t)); (labcomm_decoder_context_t*)malloc(sizeof(labcomm_decoder_context_t));
context->sample = 0; context->sample = 0;
result->context = context; result->context = context;
result->reader.context = reader_context;
result->reader.data = 0; result->reader.data = 0;
result->reader.data_size = 0; result->reader.data_size = 0;
result->reader.count = 0; result->reader.count = 0;
result->reader.pos = 0; result->reader.pos = 0;
result->reader.action = action; result->reader.action = reader;
result->reader.context = reader_context;
result->reader.on_error = on_error_fprintf; result->reader.on_error = on_error_fprintf;
result->lock.action = lock;
result->lock.context = lock_context;
result->do_register = do_decoder_register; result->do_register = do_decoder_register;
result->do_decode_one = do_decode_one; result->do_decode_one = do_decode_one;
result->on_error = on_error_fprintf; result->on_error = on_error_fprintf;
......
...@@ -76,6 +76,18 @@ typedef int (*labcomm_handle_new_datatype_callback)( ...@@ -76,6 +76,18 @@ typedef int (*labcomm_handle_new_datatype_callback)(
void labcomm_decoder_register_new_datatype_handler(struct labcomm_decoder *d, void labcomm_decoder_register_new_datatype_handler(struct labcomm_decoder *d,
labcomm_handle_new_datatype_callback on_new_datatype); labcomm_handle_new_datatype_callback on_new_datatype);
/*
* Locking support (optional)
*/
struct labcomm_lock_action {
int (*alloc)(void *context);
int (*free)(void *context);
int (*read_lock)(void *context);
int (*read_unlock)(void *context);
int (*write_lock)(void *context);
int (*write_unlock)(void *context);
};
/* /*
* Decoder * Decoder
...@@ -84,12 +96,12 @@ void labcomm_decoder_register_new_datatype_handler(struct labcomm_decoder *d, ...@@ -84,12 +96,12 @@ void labcomm_decoder_register_new_datatype_handler(struct labcomm_decoder *d,
struct labcomm_reader; struct labcomm_reader;
struct labcomm_reader_action { struct labcomm_reader_action {
int (*alloc)(struct labcomm_reader *, char *labcomm_version); int (*alloc)(struct labcomm_reader *r, char *labcomm_version);
int (*free)(struct labcomm_reader *); int (*free)(struct labcomm_reader *r);
int (*start)(struct labcomm_reader *); int (*start)(struct labcomm_reader *r);
int (*end)(struct labcomm_reader *); int (*end)(struct labcomm_reader *r);
int (*fill)(struct labcomm_reader *); int (*fill)(struct labcomm_reader *r);
int (*ioctl)(struct labcomm_reader *, int, labcomm_signature_t *, va_list); int (*ioctl)(struct labcomm_reader *r, int, labcomm_signature_t *, va_list);
}; };
typedef struct labcomm_reader { typedef struct labcomm_reader {
...@@ -104,8 +116,10 @@ typedef struct labcomm_reader { ...@@ -104,8 +116,10 @@ typedef struct labcomm_reader {
} labcomm_reader_t; } labcomm_reader_t;
struct labcomm_decoder *labcomm_decoder_new( struct labcomm_decoder *labcomm_decoder_new(
const struct labcomm_reader_action action, const struct labcomm_reader_action reader,
void *reader_context); void *reader_context,
const struct labcomm_lock_action *lock,
void *lock_context);
int labcomm_decoder_decode_one( int labcomm_decoder_decode_one(
struct labcomm_decoder *decoder); struct labcomm_decoder *decoder);
void labcomm_decoder_run( void labcomm_decoder_run(
...@@ -148,8 +162,10 @@ typedef struct labcomm_writer { ...@@ -148,8 +162,10 @@ typedef struct labcomm_writer {
} labcomm_writer_t; } labcomm_writer_t;
struct labcomm_encoder *labcomm_encoder_new( struct labcomm_encoder *labcomm_encoder_new(
const struct labcomm_writer_action action, const struct labcomm_writer_action writer,
void *writer_context); void *writer_context,
const struct labcomm_lock_action *lock,
void *lock_context);
void labcomm_encoder_free( void labcomm_encoder_free(
struct labcomm_encoder *encoder); struct labcomm_encoder *encoder);
......
...@@ -57,6 +57,10 @@ typedef void (*labcomm_decoder_typecast_t)( ...@@ -57,6 +57,10 @@ typedef void (*labcomm_decoder_typecast_t)(
typedef struct labcomm_decoder { typedef struct labcomm_decoder {
void *context; void *context;
labcomm_reader_t reader; labcomm_reader_t reader;
struct {
void *context;
const struct labcomm_lock_action *action;
} lock;
void (*do_register)(struct labcomm_decoder *, void (*do_register)(struct labcomm_decoder *,
labcomm_signature_t *, labcomm_signature_t *,
labcomm_decoder_typecast_t, labcomm_decoder_typecast_t,
...@@ -187,6 +191,10 @@ typedef int (*labcomm_encoder_function)( ...@@ -187,6 +191,10 @@ typedef int (*labcomm_encoder_function)(
typedef struct labcomm_encoder { typedef struct labcomm_encoder {
void *context; void *context;
labcomm_writer_t writer; labcomm_writer_t writer;
struct {
void *context;
const struct labcomm_lock_action *action;
} lock;
void (*do_register)(struct labcomm_encoder *encoder, void (*do_register)(struct labcomm_encoder *encoder,
labcomm_signature_t *signature, labcomm_signature_t *signature,
labcomm_encoder_function encode); labcomm_encoder_function encode);
......
...@@ -17,6 +17,7 @@ static labcomm_encoder_t encoder = { ...@@ -17,6 +17,7 @@ static labcomm_encoder_t encoder = {
.action = { NULL, NULL, NULL, NULL, NULL, NULL }, .action = { NULL, NULL, NULL, NULL, NULL, NULL },
.on_error = NULL, .on_error = NULL,
}, },
.lock = { NULL, NULL },
.do_register = NULL, .do_register = NULL,
.do_encode = NULL, .do_encode = NULL,
.on_error = NULL, .on_error = NULL,
...@@ -33,6 +34,7 @@ static labcomm_decoder_t decoder = { ...@@ -33,6 +34,7 @@ static labcomm_decoder_t decoder = {
.action = { NULL, NULL, NULL, NULL, NULL, NULL }, .action = { NULL, NULL, NULL, NULL, NULL, NULL },
.on_error = NULL, .on_error = NULL,
}, },
.lock = { NULL, NULL },
.do_register = NULL, .do_register = NULL,
.on_error = NULL, .on_error = NULL,
}; };
......
...@@ -132,7 +132,8 @@ int main(void) ...@@ -132,7 +132,8 @@ int main(void)
generated_encoding_V V; generated_encoding_V V;
generated_encoding_B B = 1; generated_encoding_B B = 1;
labcomm_encoder_t *encoder = labcomm_encoder_new(buffer_writer, buffer); labcomm_encoder_t *encoder = labcomm_encoder_new(buffer_writer, buffer,
NULL, NULL);
labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET); labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
labcomm_encoder_register_generated_encoding_V(encoder); labcomm_encoder_register_generated_encoding_V(encoder);
......
...@@ -50,8 +50,8 @@ if __name__ == '__main__': ...@@ -50,8 +50,8 @@ if __name__ == '__main__':
| if (in < 0) { return 1; } | if (in < 0) { return 1; }
| out = open(argv[2], O_WRONLY); | out = open(argv[2], O_WRONLY);
| if (out < 0) { return 1; } | if (out < 0) { return 1; }
| e = labcomm_encoder_new(labcomm_fd_writer, &out); | e = labcomm_encoder_new(labcomm_fd_writer, &out, NULL, NULL);
| d = labcomm_decoder_new(labcomm_fd_reader, &in); | d = labcomm_decoder_new(labcomm_fd_reader, &in, NULL, NULL);
""")) """))
for func,arg in sample: for func,arg in sample:
result.extend(split_match('^[^|]*\|(.*)$', """ result.extend(split_match('^[^|]*\|(.*)$', """
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment