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[]) {
char *filename = argv[1];
printf("C decoder reading from %s\n", filename);
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) {
printf("Failed to allocate decoder %s:%d\n", __FUNCTION__, __LINE__);
return 1;
......
......@@ -12,7 +12,7 @@ int main(int argc, char *argv[]) {
char *filename = argv[1];
printf("C encoder writing to %s\n", filename);
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_anotherTwoInts(encoder);
labcomm_encoder_register_simple_IntString(encoder);
......
......@@ -381,8 +381,10 @@ no_end:
}
labcomm_encoder_t *labcomm_encoder_new(
const struct labcomm_writer_action action,
void *writer_context)
const struct labcomm_writer_action writer,
void *writer_context,
const struct labcomm_lock_action *lock,
void *lock_context)
{
labcomm_encoder_t *result = malloc(sizeof(labcomm_encoder_t));
if (result) {
......@@ -396,13 +398,15 @@ labcomm_encoder_t *labcomm_encoder_new(
context->by_section = NULL;
#endif
result->context = context;
result->writer.context = writer_context;
result->writer.data = 0;
result->writer.data_size = 0;
result->writer.count = 0;
result->writer.pos = 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->do_register = do_encoder_register;
result->do_encode = do_encode;
......@@ -584,7 +588,7 @@ static int do_decode_one(labcomm_decoder_t *d)
/* TODO: should the labcomm_dynamic_buffer_writer be
a permanent part of labcomm_decoder? */
labcomm_encoder_t *e = labcomm_encoder_new(
labcomm_dynamic_buffer_writer, NULL);
labcomm_dynamic_buffer_writer, NULL, NULL, NULL);
labcomm_signature_t signature;
labcomm_sample_entry_t *entry = NULL;
int index, err;
......@@ -650,8 +654,10 @@ static int do_decode_one(labcomm_decoder_t *d)
}
labcomm_decoder_t *labcomm_decoder_new(
const struct labcomm_reader_action action,
void *reader_context)
const struct labcomm_reader_action reader,
void *reader_context,
const struct labcomm_lock_action *lock,
void *lock_context)
{
labcomm_decoder_t *result = malloc(sizeof(labcomm_decoder_t));
if (result) {
......@@ -659,13 +665,15 @@ labcomm_decoder_t *labcomm_decoder_new(
(labcomm_decoder_context_t*)malloc(sizeof(labcomm_decoder_context_t));
context->sample = 0;
result->context = context;
result->reader.context = reader_context;
result->reader.data = 0;
result->reader.data_size = 0;
result->reader.count = 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->lock.action = lock;
result->lock.context = lock_context;
result->do_register = do_decoder_register;
result->do_decode_one = do_decode_one;
result->on_error = on_error_fprintf;
......
......@@ -76,6 +76,18 @@ typedef int (*labcomm_handle_new_datatype_callback)(
void labcomm_decoder_register_new_datatype_handler(struct labcomm_decoder *d,
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
......@@ -84,12 +96,12 @@ void labcomm_decoder_register_new_datatype_handler(struct labcomm_decoder *d,
struct labcomm_reader;
struct labcomm_reader_action {
int (*alloc)(struct labcomm_reader *, char *labcomm_version);
int (*free)(struct labcomm_reader *);
int (*start)(struct labcomm_reader *);
int (*end)(struct labcomm_reader *);
int (*fill)(struct labcomm_reader *);
int (*ioctl)(struct labcomm_reader *, int, labcomm_signature_t *, va_list);
int (*alloc)(struct labcomm_reader *r, char *labcomm_version);
int (*free)(struct labcomm_reader *r);
int (*start)(struct labcomm_reader *r);
int (*end)(struct labcomm_reader *r);
int (*fill)(struct labcomm_reader *r);
int (*ioctl)(struct labcomm_reader *r, int, labcomm_signature_t *, va_list);
};
typedef struct labcomm_reader {
......@@ -104,8 +116,10 @@ typedef struct labcomm_reader {
} labcomm_reader_t;
struct labcomm_decoder *labcomm_decoder_new(
const struct labcomm_reader_action action,
void *reader_context);
const struct labcomm_reader_action reader,
void *reader_context,
const struct labcomm_lock_action *lock,
void *lock_context);
int labcomm_decoder_decode_one(
struct labcomm_decoder *decoder);
void labcomm_decoder_run(
......@@ -148,8 +162,10 @@ typedef struct labcomm_writer {
} labcomm_writer_t;
struct labcomm_encoder *labcomm_encoder_new(
const struct labcomm_writer_action action,
void *writer_context);
const struct labcomm_writer_action writer,
void *writer_context,
const struct labcomm_lock_action *lock,
void *lock_context);
void labcomm_encoder_free(
struct labcomm_encoder *encoder);
......
......@@ -57,6 +57,10 @@ typedef void (*labcomm_decoder_typecast_t)(
typedef struct labcomm_decoder {
void *context;
labcomm_reader_t reader;
struct {
void *context;
const struct labcomm_lock_action *action;
} lock;
void (*do_register)(struct labcomm_decoder *,
labcomm_signature_t *,
labcomm_decoder_typecast_t,
......@@ -187,6 +191,10 @@ typedef int (*labcomm_encoder_function)(
typedef struct labcomm_encoder {
void *context;
labcomm_writer_t writer;
struct {
void *context;
const struct labcomm_lock_action *action;
} lock;
void (*do_register)(struct labcomm_encoder *encoder,
labcomm_signature_t *signature,
labcomm_encoder_function encode);
......
......@@ -17,6 +17,7 @@ static labcomm_encoder_t encoder = {
.action = { NULL, NULL, NULL, NULL, NULL, NULL },
.on_error = NULL,
},
.lock = { NULL, NULL },
.do_register = NULL,
.do_encode = NULL,
.on_error = NULL,
......@@ -33,6 +34,7 @@ static labcomm_decoder_t decoder = {
.action = { NULL, NULL, NULL, NULL, NULL, NULL },
.on_error = NULL,
},
.lock = { NULL, NULL },
.do_register = NULL,
.on_error = NULL,
};
......
......@@ -132,7 +132,8 @@ int main(void)
generated_encoding_V V;
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_register_generated_encoding_V(encoder);
......
......@@ -50,8 +50,8 @@ if __name__ == '__main__':
| if (in < 0) { return 1; }
| out = open(argv[2], O_WRONLY);
| if (out < 0) { return 1; }
| e = labcomm_encoder_new(labcomm_fd_writer, &out);
| d = labcomm_decoder_new(labcomm_fd_reader, &in);
| e = labcomm_encoder_new(labcomm_fd_writer, &out, NULL, NULL);
| d = labcomm_decoder_new(labcomm_fd_reader, &in, NULL, NULL);
"""))
for func,arg in sample:
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